X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=src%2Forg%2Fargeo%2Fbuild%2FRepackage.java;h=193d13426bef01494c9561e3bce25c831d6e8b01;hb=1d28b46e23fabe3bdd0ee17e27ba6160ed002b3f;hp=05ea8c8c4fa598acb9ea26d68d49a3aa9860dfb5;hpb=cb6fcab3d5d5ac44dc42cdc792e66f547da09f29;p=cc0%2Fargeo-build.git diff --git a/src/org/argeo/build/Repackage.java b/src/org/argeo/build/Repackage.java index 05ea8c8..193d134 100644 --- a/src/org/argeo/build/Repackage.java +++ b/src/org/argeo/build/Repackage.java @@ -1,6 +1,7 @@ package org.argeo.build; import static java.lang.System.Logger.Level.DEBUG; +import static java.lang.System.Logger.Level.WARNING; import static org.argeo.build.Repackage.ManifestConstants.BUNDLE_SYMBOLICNAME; import static org.argeo.build.Repackage.ManifestConstants.BUNDLE_VERSION; import static org.argeo.build.Repackage.ManifestConstants.EXPORT_PACKAGE; @@ -53,6 +54,8 @@ public class Repackage { private final static String ENV_BUILD_SOURCE_BUNDLES = "BUILD_SOURCE_BUNDLES"; + private final static boolean parallel = true; + /** Main entry point. */ public static void main(String[] args) { if (args.length < 2) { @@ -66,7 +69,10 @@ public class Repackage { List> toDos = new ArrayList<>(); for (int i = 1; i < args.length; i++) { Path p = Paths.get(args[i]); - toDos.add(CompletableFuture.runAsync(() -> factory.processCategory(p))); + if (parallel) + toDos.add(CompletableFuture.runAsync(() -> factory.processCategory(p))); + else + factory.processCategory(p); } CompletableFuture.allOf(toDos.toArray(new CompletableFuture[toDos.size()])).join(); } @@ -75,7 +81,10 @@ public class Repackage { private final static String MERGE_BND = "merge.bnd"; private Path originBase; + private Path mavenBase; + private Path a2Base; + private Path a2SrcBase; private Path a2LibBase; private Path descriptorsBase; @@ -94,8 +103,11 @@ public class Repackage { Objects.requireNonNull(a2Base); Objects.requireNonNull(descriptorsBase); this.originBase = Paths.get(System.getProperty("user.home"), ".cache", "argeo/build/origin"); + this.mavenBase = Paths.get(System.getProperty("user.home"), ".m2", "repository"); + // TODO define and use a build base this.a2Base = a2Base; + this.a2SrcBase = a2Base.getParent().resolve(a2Base.getFileName() + ".src"); this.a2LibBase = a2Base.resolve("lib"); this.descriptorsBase = descriptorsBase; if (!Files.exists(this.descriptorsBase)) @@ -185,7 +197,7 @@ public class Repackage { throw new IllegalArgumentException("No M2 coordinates available for " + bndFile); M2Artifact artifact = new M2Artifact(m2Coordinates); URL url = M2ConventionsUtils.mavenRepoUrl(repoStr, artifact); - Path downloaded = downloadMaven(url, originBase, artifact); + Path downloaded = downloadMaven(url, artifact); Path targetBundleDir = processBndJar(downloaded, targetCategoryBase, fileProps, artifact); @@ -200,7 +212,6 @@ public class Repackage { /** Process multiple Maven artifacts. */ public void processM2BasedDistributionUnit(Path duDir) { try { - // String category = duDir.getParent().getFileName().toString(); Path categoryRelativePath = descriptorsBase.relativize(duDir.getParent()); Path targetCategoryBase = a2Base.resolve(categoryRelativePath); @@ -208,7 +219,6 @@ public class Repackage { Path mergeBnd = duDir.resolve(MERGE_BND); if (Files.exists(mergeBnd)) { mergeM2Artifacts(mergeBnd); -// return; } Path commonBnd = duDir.resolve(COMMON_BND); @@ -271,7 +281,7 @@ public class Repackage { // download URL url = M2ConventionsUtils.mavenRepoUrl(repoStr, artifact); - Path downloaded = downloadMaven(url, originBase, artifact); + Path downloaded = downloadMaven(url, artifact); Path targetBundleDir = processBndJar(downloaded, targetCategoryBase, mergeProps, artifact); // logger.log(Level.DEBUG, () -> "Processed " + downloaded); @@ -323,7 +333,14 @@ public class Repackage { if (bundleSymbolicName == null) throw new IllegalArgumentException("Bundle-SymbolicName must be set in " + mergeBnd); CategoryNameVersion nameVersion = new M2Artifact(category + ":" + bundleSymbolicName + ":" + m2Version); + Path targetBundleDir = targetCategoryBase.resolve(bundleSymbolicName + "." + nameVersion.getBranch()); + if (Files.exists(targetBundleDir)) { + logger.log(WARNING, targetBundleDir + " exists, deleting it..."); + deleteDirectory(targetBundleDir); + } else { + Files.createDirectories(targetBundleDir); + } String[] artifacts = artifactsStr.split(","); artifacts: for (String str : artifacts) { @@ -334,7 +351,7 @@ public class Repackage { if (artifact.getVersion() == null) artifact.setVersion(m2Version); URL url = M2ConventionsUtils.mavenRepoUrl(repoStr, artifact); - Path downloaded = downloadMaven(url, originBase, artifact); + Path downloaded = downloadMaven(url, artifact); JarEntry entry; try (JarInputStream jarIn = new JarInputStream(Files.newInputStream(downloaded), false)) { entries: while ((entry = jarIn.getNextJarEntry()) != null) { @@ -510,12 +527,12 @@ public class Repackage { /** Download and integrates sources for a single Maven artifact. */ protected void downloadAndProcessM2Sources(String repoStr, M2Artifact artifact, Path targetBundleDir) throws IOException { - if (sourceBundles) - return; +// if (sourceBundles) +// return; try { M2Artifact sourcesArtifact = new M2Artifact(artifact.toM2Coordinates(), "sources"); URL sourcesUrl = M2ConventionsUtils.mavenRepoUrl(repoStr, sourcesArtifact); - Path sourcesDownloaded = downloadMaven(sourcesUrl, originBase, artifact, true); + Path sourcesDownloaded = downloadMaven(sourcesUrl, artifact, true); processM2SourceJar(sourcesDownloaded, targetBundleDir); logger.log(Level.TRACE, () -> "Processed source " + sourcesDownloaded); } catch (Exception e) { @@ -527,11 +544,13 @@ public class Repackage { /** Integrate sources from a downloaded jar file. */ 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"); + Path targetSourceDir = sourceBundles + ? targetBundleDir.getParent().resolve(targetBundleDir.toString() + ".src") + : targetBundleDir.resolve("OSGI-OPT/src"); - // TODO make it less dangerous? if (Files.exists(targetSourceDir)) { -// deleteDirectory(targetSourceDir); + logger.log(WARNING, targetSourceDir + " exists, deleting it..."); + deleteDirectory(targetSourceDir); } else { Files.createDirectories(targetSourceDir); } @@ -561,14 +580,15 @@ public class Repackage { } /** Download a Maven artifact. */ - protected Path downloadMaven(URL url, Path dir, M2Artifact artifact) throws IOException { - return downloadMaven(url, dir, artifact, false); + protected Path downloadMaven(URL url, M2Artifact artifact) throws IOException { + return downloadMaven(url, artifact, false); } /** Download a Maven artifact. */ - protected Path downloadMaven(URL url, Path dir, M2Artifact artifact, boolean sources) throws IOException { - return download(url, dir, artifact.getGroupId() + '/' + artifact.getArtifactId() + "-" + artifact.getVersion() - + (sources ? "-sources" : "") + ".jar"); + protected Path downloadMaven(URL url, M2Artifact artifact, boolean sources) throws IOException { + return download(url, mavenBase, artifact.getGroupId().replace(".", "/") // + + '/' + artifact.getArtifactId() + '/' + artifact.getVersion() // + + '/' + artifact.getArtifactId() + "-" + artifact.getVersion() + (sources ? "-sources" : "") + ".jar"); } /* @@ -582,9 +602,8 @@ public class Repackage { Path targetCategoryBase = a2Base.resolve(categoryRelativePath); Files.createDirectories(targetCategoryBase); // first delete all directories from previous builds - for (Path dir : Files.newDirectoryStream(targetCategoryBase, (p) -> Files.isDirectory(p))) { + for (Path dir : Files.newDirectoryStream(targetCategoryBase, (p) -> Files.isDirectory(p))) deleteDirectory(dir); - } Files.createDirectories(originBase); @@ -641,10 +660,10 @@ public class Repackage { } } if (file.getFileName().toString().contains(".source_")) { - if (!sourceBundles) { - processEclipseSourceJar(file, targetCategoryBase); - logger.log(Level.DEBUG, () -> "Processed source " + file); - } +// if (!sourceBundles) { + processEclipseSourceJar(file, targetCategoryBase); + logger.log(Level.DEBUG, () -> "Processed source " + file); +// } } else { Map map = new HashMap<>(); @@ -660,8 +679,8 @@ public class Repackage { } }); - DirectoryStream dirs = Files.newDirectoryStream(targetCategoryBase, - (p) -> Files.isDirectory(p) && p.getFileName().toString().indexOf('.') >= 0); + DirectoryStream dirs = Files.newDirectoryStream(targetCategoryBase, (p) -> Files.isDirectory(p) + && p.getFileName().toString().indexOf('.') >= 0 && !p.getFileName().toString().endsWith(".src")); for (Path dir : dirs) { createJar(dir); } @@ -684,11 +703,13 @@ public class Repackage { NameVersion nameVersion = new NameVersion(relatedBundle[0], version); targetBundleDir = targetBase.resolve(nameVersion.getName() + "." + nameVersion.getBranch()); - Path targetSourceDir = targetBundleDir.resolve("OSGI-OPT/src"); + Path targetSourceDir = sourceBundles + ? targetBundleDir.getParent().resolve(targetBundleDir.toString() + ".src") + : targetBundleDir.resolve("OSGI-OPT/src"); - // TODO make it less dangerous? if (Files.exists(targetSourceDir)) { -// deleteDirectory(targetSourceDir); + logger.log(WARNING, targetSourceDir + " exists, deleting it..."); + deleteDirectory(targetSourceDir); } else { Files.createDirectories(targetSourceDir); } @@ -761,6 +782,12 @@ public class Repackage { } } targetBundleDir = targetBase.resolve(nameVersion.getName() + "." + nameVersion.getBranch()); + if (Files.exists(targetBundleDir)) { + logger.log(WARNING, targetBundleDir + " exists, deleting it..."); + deleteDirectory(targetBundleDir); + } else { + Files.createDirectories(targetBundleDir); + } // force Java 9 module name entries.put(ManifestConstants.AUTOMATIC_MODULE_NAME.toString(), nameVersion.getName()); @@ -949,8 +976,9 @@ public class Repackage { Path dest; if (name == null) { // We use also use parent directory in case the archive itself has a fixed name - name = url.getPath().substring(url.getPath().lastIndexOf('/') + 1) - .substring(url.getPath().lastIndexOf('/') + 1); + String[] segments = url.getPath().split("/"); + name = segments.length > 1 ? segments[segments.length - 2] + '-' + segments[segments.length - 1] + : segments[segments.length - 1]; } dest = dir.resolve(name); @@ -995,6 +1023,52 @@ public class Repackage { }); } deleteDirectory(bundleDir); + + if (sourceBundles) { + Path bundleCategoryDir = bundleDir.getParent(); + Path sourceDir = bundleCategoryDir.resolve(bundleDir.toString() + ".src"); + if (!Files.exists(sourceDir)) { + logger.log(WARNING, sourceDir + " does not exist, skipping..."); + return jarPath; + + } + + Path relPath = a2Base.relativize(bundleCategoryDir); + Path srcCategoryDir = a2SrcBase.resolve(relPath); + Path srcJarP = srcCategoryDir.resolve(sourceDir.getFileName() + ".jar"); + Files.createDirectories(srcJarP.getParent()); + + String bundleSymbolicName = manifest.getMainAttributes().getValue("Bundle-SymbolicName").toString(); + // in case there are additional directives + bundleSymbolicName = bundleSymbolicName.split(";")[0]; + Manifest srcManifest = new Manifest(); + srcManifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); + srcManifest.getMainAttributes().putValue("Bundle-SymbolicName", bundleSymbolicName + ".src"); + srcManifest.getMainAttributes().putValue("Bundle-Version", + manifest.getMainAttributes().getValue("Bundle-Version").toString()); + srcManifest.getMainAttributes().putValue("Eclipse-SourceBundle", + bundleSymbolicName + ";version=\"" + manifest.getMainAttributes().getValue("Bundle-Version")); + + try (JarOutputStream srcJarOut = new JarOutputStream(Files.newOutputStream(srcJarP), srcManifest)) { + srcJarOut.setLevel(Deflater.BEST_COMPRESSION); + Files.walkFileTree(sourceDir, new SimpleFileVisitor() { + + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + if (file.getFileName().toString().equals("MANIFEST.MF")) + return super.visitFile(file, attrs); + JarEntry entry = new JarEntry( + sourceDir.relativize(file).toString().replace(File.separatorChar, '/')); + srcJarOut.putNextEntry(entry); + Files.copy(file, srcJarOut); + return super.visitFile(file, attrs); + } + + }); + } + deleteDirectory(sourceDir); + } + return jarPath; }