]> git.argeo.org Git - cc0/argeo-build.git/blobdiff - src/org/argeo/build/Repackage.java
Also remove DSA signatures
[cc0/argeo-build.git] / src / org / argeo / build / Repackage.java
index db323279257077b0a0f11eb718f51b71931758eb..88b2f37a44fbd81854344baa11b05639c56bb6af 100644 (file)
@@ -40,6 +40,7 @@ import java.nio.file.Path;
 import java.nio.file.PathMatcher;
 import java.nio.file.Paths;
 import java.nio.file.SimpleFileVisitor;
+import java.nio.file.StandardCopyOption;
 import java.nio.file.StandardOpenOption;
 import java.nio.file.attribute.BasicFileAttributes;
 import java.util.ArrayList;
@@ -122,22 +123,22 @@ public class Repackage {
        final static String A2_ORIGIN = "A2-ORIGIN";
 
        /** Directory where to download archives */
-       Path originBase;
+       final Path originBase;
        /** Directory where to download Maven artifacts */
-       Path mavenBase;
+       final Path mavenBase;
 
        /** A2 repository base for binary bundles */
-       Path a2Base;
+       final Path a2Base;
        /** A2 repository base for source bundles */
-       Path a2SrcBase;
+       final Path a2SrcBase;
        /** A2 base for native components */
-       Path a2LibBase;
+       final Path a2LibBase;
        /** Location of the descriptors driving the packaging */
-       Path descriptorsBase;
+       final Path descriptorsBase;
        /** URIs of archives to download */
-       Properties uris = new Properties();
+       final Properties uris = new Properties();
        /** Mirrors for archive download. Key is URI prefix, value list of base URLs */
-       Map<String, List<String>> mirrors = new HashMap<String, List<String>>();
+       final Map<String, List<String>> mirrors = new HashMap<String, List<String>>();
 
        /** Whether sources should be packaged separately */
        final boolean sourceBundles;
@@ -244,12 +245,30 @@ public class Repackage {
                        URL url = M2ConventionsUtils.mavenRepoUrl(repoStr, artifact);
                        Path downloaded = downloadMaven(url, artifact);
 
-                       A2Origin origin = new A2Origin();
-                       Path targetBundleDir = processBndJar(downloaded, targetCategoryBase, fileProps, artifact, origin);
-
-                       downloadAndProcessM2Sources(repoStr, artifact, targetBundleDir, false);
+                       // some proprietary artifacts do not allow any modification
+                       // when releasing (with separate sources) we just copy it
+                       boolean doNotModify = Boolean.parseBoolean(
+                                       fileProps.getOrDefault(ManifestConstants.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(repoStr, artifact, bundleDir, false);
+                               Manifest manifest;
+                               try (JarInputStream jarIn = new JarInputStream(Files.newInputStream(unmodifiedTarget))) {
+                                       manifest = jarIn.getManifest();
+                               }
+                               createSourceJar(unmodifiedTarget, manifest);
+                               return;
+                       }
 
-                       createJar(targetBundleDir, origin);
+                       // regular processing
+                       A2Origin origin = new A2Origin();
+                       Path bundleDir = processBndJar(downloaded, targetCategoryBase, fileProps, artifact, origin);
+                       downloadAndProcessM2Sources(repoStr, artifact, bundleDir, false);
+                       createJar(bundleDir, origin);
                } catch (Exception e) {
                        throw new RuntimeException("Cannot process " + bndFile, e);
                }
@@ -413,6 +432,10 @@ public class Repackage {
                                                origin.deleted.add("cache directory (.cache) from " + artifact);
                                                continue entries;
                                        }
+                                       if (entry.getName().equals("META-INF/DEPENDENCIES")) {
+                                               origin.deleted.add("Dependencies (META-INF/DEPENDENCIES) from " + artifact);
+                                               continue entries;
+                                       }
                                        if (entry.getName().equals("META-INF/MANIFEST.MF")) {
                                                Path originalManifest = bundleDir.resolve(A2_ORIGIN).resolve(artifact.getGroupId())
                                                                .resolve(artifact.getArtifactId()).resolve("MANIFEST.MF");
@@ -522,14 +545,13 @@ public class Repackage {
        /** Generate MANIFEST using BND. */
        Path processBndJar(Path downloaded, Path targetCategoryBase, Properties fileProps, M2Artifact artifact,
                        A2Origin origin) {
-
                try {
                        Map<String, String> additionalEntries = new TreeMap<>();
-                       boolean doNotModify = Boolean.parseBoolean(
+                       boolean doNotModifyManifest = Boolean.parseBoolean(
                                        fileProps.getOrDefault(ARGEO_ORIGIN_MANIFEST_NOT_MODIFIED.toString(), "false").toString());
 
                        // Note: we always force the symbolic name
-                       if (doNotModify) {
+                       if (doNotModifyManifest) {
                                fileEntries: for (Object key : fileProps.keySet()) {
                                        if (ARGEO_ORIGIN_M2.toString().equals(key))
                                                continue fileEntries;
@@ -878,7 +900,8 @@ public class Repackage {
                                entries: while ((entry = jarIn.getNextJarEntry()) != null) {
                                        if (entry.isDirectory())
                                                continue entries;
-                                       if (entry.getName().endsWith(".RSA") || entry.getName().endsWith(".SF")) {
+                                       if (entry.getName().endsWith(".RSA") || entry.getName().endsWith(".DSA")
+                                                       || entry.getName().endsWith(".SF")) {
                                                origin.deleted.add("cryptographic signatures");
                                                continue entries;
                                        }
@@ -1162,52 +1185,55 @@ 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;
+               if (sourceBundles)
+                       createSourceJar(bundleDir, manifest);
 
-                       }
+               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(MANIFEST_VERSION, "1.0");
-                       srcManifest.getMainAttributes().putValue(BUNDLE_SYMBOLICNAME.toString(), bundleSymbolicName + ".src");
-                       srcManifest.getMainAttributes().putValue(BUNDLE_VERSION.toString(),
-                                       manifest.getMainAttributes().getValue(BUNDLE_VERSION.toString()).toString());
-                       srcManifest.getMainAttributes().putValue(ECLIPSE_SOURCE_BUNDLE.toString(), bundleSymbolicName
-                                       + ";version=\"" + manifest.getMainAttributes().getValue(BUNDLE_VERSION.toString()));
-
-                       try (JarOutputStream srcJarOut = new JarOutputStream(Files.newOutputStream(srcJarP), srcManifest)) {
-                               srcJarOut.setLevel(Deflater.BEST_COMPRESSION);
-                               Files.walkFileTree(sourceDir, new SimpleFileVisitor<Path>() {
-
-                                       @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);
-                                       }
+       void createSourceJar(Path bundleDir, Manifest manifest) 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;
 
-                               });
-                       }
-                       deleteDirectory(sourceDir);
                }
 
-               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(MANIFEST_VERSION, "1.0");
+               srcManifest.getMainAttributes().putValue(BUNDLE_SYMBOLICNAME.toString(), bundleSymbolicName + ".src");
+               srcManifest.getMainAttributes().putValue(BUNDLE_VERSION.toString(),
+                               manifest.getMainAttributes().getValue(BUNDLE_VERSION.toString()).toString());
+               srcManifest.getMainAttributes().putValue(ECLIPSE_SOURCE_BUNDLE.toString(),
+                               bundleSymbolicName + ";version=\"" + manifest.getMainAttributes().getValue(BUNDLE_VERSION.toString()));
+
+               try (JarOutputStream srcJarOut = new JarOutputStream(Files.newOutputStream(srcJarP), srcManifest)) {
+                       srcJarOut.setLevel(Deflater.BEST_COMPRESSION);
+                       Files.walkFileTree(sourceDir, new SimpleFileVisitor<Path>() {
+
+                               @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);
        }
 
        /** MANIFEST headers. */
@@ -1258,6 +1284,11 @@ public class Repackage {
                 * proprietary licenses, such as JCR Day License).
                 */
                ARGEO_ORIGIN_EMBED("Argeo-Origin-Embed"), //
+               /**
+                * Do not modify original jar (may be required by some proprietary licenses,
+                * such as JCR Day License).
+                */
+               ARGEO_DO_NOT_MODIFY("Argeo-Origin-Do-Not-Modify"), //
                /**
                 * Origin (non-Maven) URI of the component. It may be anything (jar, archive,
                 * etc.).