Source bundle creation for unmodified jars
authorMathieu Baudier <mbaudier@argeo.org>
Sun, 19 Mar 2023 08:48:31 +0000 (09:48 +0100)
committerMathieu Baudier <mbaudier@argeo.org>
Sun, 19 Mar 2023 08:48:31 +0000 (09:48 +0100)
src/org/argeo/build/Repackage.java

index d67a6759ee0f812e7d3551b62c8573a7ea911ca7..01a84cf4ce932d44b50be2c7e95523ea6a02383e 100644 (file)
@@ -253,20 +253,22 @@ public class Repackage {
                                Path unmodifiedTarget = targetCategoryBase.resolve(
                                                fileProps.getProperty(BUNDLE_SYMBOLICNAME.toString()) + "." + artifact.getBranch() + ".jar");
                                Files.copy(downloaded, unmodifiedTarget, StandardCopyOption.REPLACE_EXISTING);
-                               downloadAndProcessM2Sources(repoStr, artifact,
-                                               targetCategoryBase.resolve(
-                                                               fileProps.getProperty(BUNDLE_SYMBOLICNAME.toString()) + "." + artifact.getBranch()),
-                                               false);
+                               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;
                        }
-                       
-                       // normal processing
-                       A2Origin origin = new A2Origin();
-                       Path targetBundleDir = processBndJar(downloaded, targetCategoryBase, fileProps, artifact, origin);
 
-                       downloadAndProcessM2Sources(repoStr, artifact, targetBundleDir, false);
-
-                       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);
                }
@@ -1182,52 +1184,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. */