]> git.argeo.org Git - cc0/argeo-build.git/blobdiff - src/org/argeo/build/Repackage.java
Always package sources separately for unmodified jars
[cc0/argeo-build.git] / src / org / argeo / build / Repackage.java
index 607a9ab1c583528ac529c4dfb8a0fe716463f78e..0eec4fad7d7b57d5b6d30838e711771cadc3397c 100644 (file)
@@ -9,6 +9,7 @@ import static java.nio.file.FileVisitResult.CONTINUE;
 import static java.nio.file.StandardOpenOption.APPEND;
 import static java.nio.file.StandardOpenOption.CREATE;
 import static java.util.jar.Attributes.Name.MANIFEST_VERSION;
+import static org.argeo.build.Repackage.ManifestHeader.ARGEO_DO_NOT_MODIFY;
 import static org.argeo.build.Repackage.ManifestHeader.ARGEO_ORIGIN_M2;
 import static org.argeo.build.Repackage.ManifestHeader.ARGEO_ORIGIN_M2_MERGE;
 import static org.argeo.build.Repackage.ManifestHeader.ARGEO_ORIGIN_M2_REPO;
@@ -231,12 +232,12 @@ public class Repackage {
        final Map<String, List<String>> mirrors = new HashMap<String, List<String>>();
 
        /** Whether sources should be packaged separately */
-       final boolean sourceBundles;
+       final boolean separateSources;
 
        /** Constructor initialises the various variables */
        public Repackage(Path a2Base, Path descriptorsBase) {
-               sourceBundles = Boolean.parseBoolean(System.getenv(ENV_SOURCE_BUNDLES));
-               if (sourceBundles)
+               separateSources = Boolean.parseBoolean(System.getenv(ENV_SOURCE_BUNDLES));
+               if (separateSources)
                        logger.log(INFO, "Sources will be packaged separately");
 
                Objects.requireNonNull(a2Base);
@@ -246,7 +247,7 @@ public class Repackage {
 
                // TODO define and use a build base
                this.a2Base = a2Base;
-               this.a2SrcBase = a2Base.getParent().resolve(a2Base.getFileName() + ".src");
+               this.a2SrcBase = separateSources ? a2Base.getParent().resolve(a2Base.getFileName() + ".src") : a2Base;
                this.a2LibBase = a2Base.resolve("lib");
                this.descriptorsBase = descriptorsBase;
                if (!Files.exists(this.descriptorsBase))
@@ -331,22 +332,10 @@ public class Repackage {
 
                        Path downloaded = downloadMaven(fileProps, artifact);
 
-                       // some proprietary artifacts do not allow any modification
-                       // when releasing (with separate sources) we just copy it
-                       boolean doNotModify = Boolean.parseBoolean(
-                                       fileProps.getOrDefault(ManifestHeader.ARGEO_DO_NOT_MODIFY.toString(), "false").toString());
-                       if (doNotModify && sourceBundles) {
-                               Path unmodifiedTarget = targetCategoryBase.resolve(
-                                               fileProps.getProperty(BUNDLE_SYMBOLICNAME.toString()) + "." + artifact.getBranch() + ".jar");
-                               Files.copy(downloaded, unmodifiedTarget, StandardCopyOption.REPLACE_EXISTING);
-                               Path bundleDir = targetCategoryBase
-                                               .resolve(fileProps.getProperty(BUNDLE_SYMBOLICNAME.toString()) + "." + artifact.getBranch());
-                               downloadAndProcessM2Sources(fileProps, artifact, bundleDir, false);
-                               Manifest manifest;
-                               try (JarInputStream jarIn = new JarInputStream(Files.newInputStream(unmodifiedTarget))) {
-                                       manifest = jarIn.getManifest();
-                               }
-                               createSourceJar(bundleDir, manifest);
+                       boolean doNotModify = Boolean
+                                       .parseBoolean(fileProps.getOrDefault(ARGEO_DO_NOT_MODIFY.toString(), "false").toString());
+                       if (doNotModify) {
+                               processNotModified(targetCategoryBase, downloaded, fileProps, artifact);
                                return;
                        }
 
@@ -438,10 +427,16 @@ public class Repackage {
                                // download
                                Path downloaded = downloadMaven(mergedProps, artifact);
 
-                               A2Origin origin = new A2Origin();
-                               Path targetBundleDir = processBndJar(downloaded, targetCategoryBase, mergedProps, artifact, origin);
-                               downloadAndProcessM2Sources(mergedProps, artifact, targetBundleDir, false);
-                               createJar(targetBundleDir, origin);
+                               boolean doNotModify = Boolean
+                                               .parseBoolean(mergedProps.getOrDefault(ARGEO_DO_NOT_MODIFY.toString(), "false").toString());
+                               if (doNotModify) {
+                                       processNotModified(targetCategoryBase, downloaded, mergedProps, artifact);
+                               } else {
+                                       A2Origin origin = new A2Origin();
+                                       Path targetBundleDir = processBndJar(downloaded, targetCategoryBase, mergedProps, artifact, origin);
+                                       downloadAndProcessM2Sources(mergedProps, artifact, targetBundleDir, false);
+                                       createJar(targetBundleDir, origin);
+                               }
                        }
                } catch (IOException e) {
                        throw new RuntimeException("Cannot process " + duDir, e);
@@ -706,6 +701,24 @@ public class Repackage {
 
        }
 
+       /** Process an artifact that should not be modified. */
+       void processNotModified(Path targetCategoryBase, Path downloaded, Properties fileProps, M2Artifact artifact)
+                       throws IOException {
+               // Some proprietary or signed artifacts do not allow any modification
+               // When releasing (with separate sources), we just copy it
+               Path unmodifiedTarget = targetCategoryBase
+                               .resolve(fileProps.getProperty(BUNDLE_SYMBOLICNAME.toString()) + "." + artifact.getBranch() + ".jar");
+               Files.copy(downloaded, unmodifiedTarget, StandardCopyOption.REPLACE_EXISTING);
+               Path bundleDir = targetCategoryBase
+                               .resolve(fileProps.getProperty(BUNDLE_SYMBOLICNAME.toString()) + "." + artifact.getBranch());
+               downloadAndProcessM2Sources(fileProps, artifact, bundleDir, false);
+               Manifest manifest;
+               try (JarInputStream jarIn = new JarInputStream(Files.newInputStream(unmodifiedTarget))) {
+                       manifest = jarIn.getManifest();
+               }
+               createSourceJar(bundleDir, manifest, true);
+       }
+
        /** Download and integrates sources for a single Maven artifact. */
        void downloadAndProcessM2Sources(Properties props, M2Artifact artifact, Path targetBundleDir, boolean merging)
                        throws IOException {
@@ -729,7 +742,7 @@ public class Repackage {
        /** Integrate sources from a downloaded jar file. */
        void processM2SourceJar(Path file, Path bundleDir, M2Artifact mergingFrom) throws IOException {
                A2Origin origin = new A2Origin();
-               Path sourceDir = sourceBundles ? bundleDir.getParent().resolve(bundleDir.toString() + ".src")
+               Path sourceDir = separateSources ? bundleDir.getParent().resolve(bundleDir.toString() + ".src")
                                : bundleDir.resolve("OSGI-OPT/src");
                try (JarInputStream jarIn = new JarInputStream(Files.newInputStream(file), false)) {
 
@@ -773,7 +786,7 @@ public class Repackage {
                        }
                }
                // write the changes
-               if (sourceBundles) {
+               if (separateSources) {
                        origin.appendChanges(sourceDir);
                } else {
                        origin.added.add("source code under OSGI-OPT/src");
@@ -794,10 +807,6 @@ public class Repackage {
        /** Download a Maven artifact. */
        Path downloadMaven(URL url, M2Artifact artifact) throws IOException {
                return download(url, mavenBase, M2ConventionsUtils.artifactPath("", artifact));
-//             return download(url, mavenBase, artifact.getGroupId().replace(".", "/") //
-//                             + '/' + artifact.getArtifactId() + '/' + artifact.getVersion() //
-//                             + '/' + artifact.getArtifactId() + "-" + artifact.getVersion()
-//                             + (artifact.getClassifier() != null ? "-" + artifact.getClassifier() : "") + ".jar");
        }
 
        /*
@@ -916,7 +925,7 @@ public class Repackage {
                                NameVersion nameVersion = new NameVersion(relatedBundle[0], version);
                                bundleDir = targetBase.resolve(nameVersion.getName() + "." + nameVersion.getBranch());
 
-                               Path sourceDir = sourceBundles ? bundleDir.getParent().resolve(bundleDir.toString() + ".src")
+                               Path sourceDir = separateSources ? bundleDir.getParent().resolve(bundleDir.toString() + ".src")
                                                : bundleDir.resolve("OSGI-OPT/src");
 
                                Files.createDirectories(sourceDir);
@@ -933,7 +942,7 @@ public class Repackage {
                                }
 
                                // write the changes
-                               if (sourceBundles) {
+                               if (separateSources) {
                                        origin.appendChanges(sourceDir);
                                } else {
                                        origin.added.add("source code under OSGI-OPT/src");
@@ -1350,21 +1359,23 @@ public class Repackage {
                }
                deleteDirectory(bundleDir);
 
-               if (sourceBundles)
-                       createSourceJar(bundleDir, manifest);
+               if (separateSources)
+                       createSourceJar(bundleDir, manifest, false);
 
                return jarPath;
        }
 
        /** Package sources separately, in the Eclipse-SourceBundle format. */
-       void createSourceJar(Path bundleDir, Manifest manifest) throws IOException {
+       void createSourceJar(Path bundleDir, Manifest manifest, boolean notModified) throws IOException {
                Path bundleCategoryDir = bundleDir.getParent();
                Path sourceDir = bundleCategoryDir.resolve(bundleDir.toString() + ".src");
                if (!Files.exists(sourceDir)) {
                        logger.log(WARNING, sourceDir + " does not exist, skipping...");
                        return;
                }
-               createReadMe(sourceDir, manifest);
+
+               if (!notModified)
+                       createReadMe(sourceDir, manifest);
 
                Path relPath = a2Base.relativize(bundleCategoryDir);
                Path srcCategoryDir = a2SrcBase.resolve(relPath);
@@ -1460,7 +1471,7 @@ public class Repackage {
 
                        writer.append("\nA detailed list of changes is available under " + CHANGES + ".\n");
                        if (!jarDir.getFileName().endsWith(".src")) {// binary archive
-                               if (sourceBundles)
+                               if (separateSources)
                                        writer.append("Corresponding sources are available in the related archive named "
                                                        + jarDir.toString() + ".src.jar.\n");
                                else
@@ -1578,7 +1589,8 @@ class M2ConventionsUtils {
 
        /** Absolute path to the directories where the files will be stored */
        static String artifactParentPath(String artifactBasePath, M2Artifact artifact) {
-               return artifactBasePath + (artifactBasePath.endsWith("/") ? "" : "/") + artifactParentPath(artifact);
+               return artifactBasePath + (artifactBasePath.endsWith("/") || artifactBasePath.equals("") ? "" : "/")
+                               + artifactParentPath(artifact);
        }
 
        /** Relative path to the directories where the files will be stored */