+ public void processM2BasedDistributionUnit(Path duDir) {
+ try {
+ String category = duDir.getParent().getFileName().toString();
+ Path targetCategoryBase = factoryBase.resolve(category);
+ Path commonBnd = duDir.resolve(COMMON_BND);
+ Properties commonProps = new Properties();
+ try (InputStream in = Files.newInputStream(commonBnd)) {
+ commonProps.load(in);
+ }
+
+ String m2Version = commonProps.getProperty(SLC_ORIGIN_M2.toString());
+ if (!m2Version.startsWith(":")) {
+ throw new IllegalStateException("Only the M2 version can be specified: " + m2Version);
+ }
+ m2Version = m2Version.substring(1);
+
+ // String license = commonProps.getProperty(BUNDLE_LICENSE.toString());
+
+ DirectoryStream<Path> ds = Files.newDirectoryStream(duDir,
+ (p) -> p.getFileName().toString().endsWith(".bnd")
+ && !p.getFileName().toString().equals(COMMON_BND));
+ for (Path p : ds) {
+ Properties fileProps = new Properties();
+ try (InputStream in = Files.newInputStream(p)) {
+ fileProps.load(in);
+ }
+ String m2Coordinates = fileProps.getProperty(SLC_ORIGIN_M2.toString());
+ DefaultArtifact artifact = new DefaultArtifact(m2Coordinates);
+
+ // temporary rewrite, for migration
+ String localLicense = fileProps.getProperty(BUNDLE_LICENSE.toString());
+ if (localLicense != null || artifact.getVersion() != null) {
+ fileProps.remove(BUNDLE_LICENSE.toString());
+ fileProps.put(SLC_ORIGIN_M2.toString(), artifact.getGroupId() + ":" + artifact.getArtifactId());
+ try (Writer writer = Files.newBufferedWriter(p)) {
+ for (Object key : fileProps.keySet()) {
+ String value = fileProps.getProperty(key.toString());
+ writer.write(key + ": " + value + '\n');
+ }
+ logger.log(Level.DEBUG, () -> "Migrated " + p);
+ }
+ }
+
+ artifact.setVersion(m2Version);
+ URL url = MavenConventionsUtils.mavenCentralUrl(artifact);
+ Path downloaded = download(url, originBase, artifact.toM2Coordinates() + ".jar");
+
+ // prepare manifest entries
+ Map<String, String> entries = new HashMap<>();
+ for (Object key : commonProps.keySet()) {
+ entries.put(key.toString(), commonProps.getProperty(key.toString()));
+ }
+ fileEntries: for (Object key : fileProps.keySet()) {
+ if (ManifestConstants.SLC_ORIGIN_M2.toString().equals(key))
+ continue fileEntries;
+ String value = fileProps.getProperty(key.toString());
+ String previousValue = entries.put(key.toString(), value);
+ if (previousValue != null) {
+ logger.log(Level.WARNING,
+ downloaded + ": " + key + " was " + previousValue + ", overridden with " + value);
+ }
+ }
+ entries.put(ManifestConstants.SLC_ORIGIN_M2.toString(), artifact.toM2Coordinates());
+ Path targetBundleDir = processBundleJar(downloaded, targetCategoryBase, entries);
+ logger.log(Level.DEBUG, () -> "Processed " + downloaded);
+
+ // sources
+ DefaultArtifact sourcesArtifact = new DefaultArtifact(artifact.toM2Coordinates(), "sources");
+ URL sourcesUrl = MavenConventionsUtils.mavenCentralUrl(sourcesArtifact);
+ Path sourcesDownloaded = download(sourcesUrl, originBase, artifact.toM2Coordinates() + ".sources.jar");
+ processM2SourceJar(sourcesDownloaded, targetBundleDir);
+ logger.log(Level.DEBUG, () -> "Processed " + sourcesDownloaded);
+ }
+ } catch (IOException e) {
+ throw new RuntimeException("Cannot process " + duDir, e);
+ }
+
+ }
+
+ protected void processM2SourceJar(Path file, Path targetBundleDir) throws IOException {
+ try (JarInputStream jarIn = new JarInputStream(Files.newInputStream(file), false)) {
+ Path targetSourceDir = targetBundleDir.resolve("OSGI-OPT/src");
+
+ // TODO make it less dangerous?
+ if (Files.exists(targetSourceDir)) {
+ deleteDirectory(targetSourceDir);
+ } else {
+ Files.createDirectories(targetSourceDir);
+ }
+
+ // copy entries
+ JarEntry entry;
+ entries: while ((entry = jarIn.getNextJarEntry()) != null) {
+ if (entry.isDirectory())
+ continue entries;
+ if (entry.getName().startsWith("META-INF"))// skip META-INF entries
+ continue entries;
+ Path target = targetSourceDir.resolve(entry.getName());
+ Files.createDirectories(target.getParent());
+ Files.copy(jarIn, target);
+ logger.log(Level.TRACE, () -> "Copied source " + target);
+ }
+ }
+
+ }
+