+ protected void mergeM2Artifacts(Path mergeBnd) throws IOException {
+ Path duDir = mergeBnd.getParent();
+ String category = duDir.getParent().getFileName().toString();
+ Path targetCategoryBase = a2Base.resolve(category);
+
+ Properties mergeProps = new Properties();
+ try (InputStream in = Files.newInputStream(mergeBnd)) {
+ mergeProps.load(in);
+ }
+ String m2Version = mergeProps.getProperty(SLC_ORIGIN_M2.toString());
+ if (m2Version == null) {
+ logger.log(Level.WARNING, "Ignoring " + duDir + " as it is not an M2-based distribution unit");
+ return;// ignore, this is probably an Eclipse archive
+ }
+ if (!m2Version.startsWith(":")) {
+ throw new IllegalStateException("Only the M2 version can be specified: " + m2Version);
+ }
+ m2Version = m2Version.substring(1);
+
+ String artifactsStr = mergeProps.getProperty(ManifestConstants.SLC_ORIGIN_M2_MERGE.toString());
+ String repoStr = mergeProps.containsKey(SLC_ORIGIN_M2_REPO.toString())
+ ? mergeProps.getProperty(SLC_ORIGIN_M2_REPO.toString())
+ : null;
+
+ String bundleSymbolicName = mergeProps.getProperty(ManifestConstants.BUNDLE_SYMBOLICNAME.toString());
+ DefaultCategoryNameVersion nameVersion = new DefaultArtifact(
+ category + ":" + bundleSymbolicName + ":" + m2Version);
+ Path targetBundleDir = targetCategoryBase.resolve(bundleSymbolicName + "." + nameVersion.getBranch());
+
+ String[] artifacts = artifactsStr.split(",");
+ artifacts: for (String str : artifacts) {
+ String m2Coordinates = str.trim();
+ if ("".equals(m2Coordinates))
+ continue artifacts;
+ DefaultArtifact artifact = new DefaultArtifact(m2Coordinates.trim());
+ artifact.setVersion(m2Version);
+ URL url = MavenConventionsUtils.mavenRepoUrl(repoStr, artifact);
+ Path downloaded = download(url, originBase, artifact.toM2Coordinates() + ".jar");
+ JarEntry entry;
+ try (JarInputStream jarIn = new JarInputStream(Files.newInputStream(downloaded), false)) {
+ entries: while ((entry = jarIn.getNextJarEntry()) != null) {
+ if (entry.isDirectory())
+ continue entries;
+ if (entry.getName().endsWith(".RSA") || entry.getName().endsWith(".SF"))
+ continue entries;
+ Path target = targetBundleDir.resolve(entry.getName());
+ Files.createDirectories(target.getParent());
+ if (!Files.exists(target)) {
+ Files.copy(jarIn, target);
+ } else {
+ if (entry.getName().startsWith("META-INF/services/")) {
+ try (OutputStream out = Files.newOutputStream(target, StandardOpenOption.APPEND)) {
+ jarIn.transferTo(out);
+ }
+ } else {
+ throw new IllegalStateException("File " + target + " already exists");
+ }
+ }
+ logger.log(Level.TRACE, () -> "Copied " + target);
+ }
+
+ }
+ downloadAndProcessM2Sources(repoStr, artifact, targetBundleDir);
+ }
+
+ Map<String, String> entries = new TreeMap<>();
+ try (Analyzer bndAnalyzer = new Analyzer()) {
+ bndAnalyzer.setProperties(mergeProps);
+ Jar jar = new Jar(targetBundleDir.toFile());
+ bndAnalyzer.setJar(jar);
+ Manifest manifest = bndAnalyzer.calcManifest();
+
+ keys: for (Object key : manifest.getMainAttributes().keySet()) {
+ Object value = manifest.getMainAttributes().get(key);
+
+ switch (key.toString()) {
+ case "Tool":
+ case "Bnd-LastModified":
+ case "Created-By":
+ continue keys;
+ }
+ if ("Require-Capability".equals(key.toString())
+ && value.toString().equals("osgi.ee;filter:=\"(&(osgi.ee=JavaSE)(version=1.1))\""))
+ continue keys;// hack for very old classes
+ entries.put(key.toString(), value.toString());
+ logger.log(DEBUG, () -> key + "=" + value);
+
+ }
+ } catch (Exception e) {
+ throw new RuntimeException("Cannot process " + mergeBnd, e);
+ }
+
+ Manifest manifest = new Manifest();
+ Path manifestPath = targetBundleDir.resolve("META-INF/MANIFEST.MF");
+ Files.createDirectories(manifestPath.getParent());
+ for (String key : entries.keySet()) {
+ String value = entries.get(key);
+ manifest.getMainAttributes().putValue(key, value);
+ }
+ try (OutputStream out = Files.newOutputStream(manifestPath)) {
+ manifest.write(out);
+ }
+
+ createJar(targetBundleDir);
+
+ }
+
+ protected void downloadAndProcessM2Sources(String repoStr, DefaultArtifact artifact, Path targetBundleDir)
+ throws IOException {