]> git.argeo.org Git - cc0/argeo-build.git/blobdiff - src/org/argeo/build/Make.java
Fix unmodified jars with separate sources
[cc0/argeo-build.git] / src / org / argeo / build / Make.java
index 8e178cd52630b2c62895276653184956f449a0ef..be3764c29dda69abaab8e2868bdab0e3cce50bec 100644 (file)
@@ -13,6 +13,7 @@ import java.io.PrintWriter;
 import java.lang.System.Logger;
 import java.lang.System.Logger.Level;
 import java.lang.management.ManagementFactory;
+import java.nio.file.DirectoryStream;
 import java.nio.file.FileVisitResult;
 import java.nio.file.Files;
 import java.nio.file.Path;
@@ -61,6 +62,14 @@ public class Make {
         */
        private final static String ENV_SOURCE_BUNDLES = "SOURCE_BUNDLES";
 
+       /**
+        * Environment variable on whether legal files at the root of the sources should
+        * be included in the generated bundles. Should be set to true when building
+        * third-party software in order no to include the build harness license into
+        * the generated bundles.
+        */
+       private final static String ENV_NO_SDK_LEGAL = "NO_SDK_LEGAL";
+
        /**
         * Environment variable to override the default location for the Argeo Build
         * configuration files. Typically used if Argeo Build has been compiled and
@@ -78,7 +87,7 @@ public class Make {
        /** Base of the source code, typically the cloned git repository. */
        final Path sdkSrcBase;
        /**
-        * The base of the builder, typically a submodule pointing to the public
+        * The base of the builder, typically a submodule pointing to the INCLUDEpublic
         * argeo-build directory.
         */
        final Path argeoBuildBase;
@@ -91,14 +100,19 @@ public class Make {
        /** The base of the a2 sources when packages separately. */
        final Path a2srcOutput;
 
-       /** Whether sources should be packaged separately */
+       /** Whether sources should be packaged separately. */
        final boolean sourceBundles;
+       /** Whether common legal files should be included. */
+       final boolean noSdkLegal;
 
        /** Constructor initialises the base directories. */
        public Make() throws IOException {
                sourceBundles = Boolean.parseBoolean(System.getenv(ENV_SOURCE_BUNDLES));
                if (sourceBundles)
                        logger.log(Level.INFO, "Sources will be packaged separately");
+               noSdkLegal = Boolean.parseBoolean(System.getenv(ENV_NO_SDK_LEGAL));
+               if (noSdkLegal)
+                       logger.log(Level.INFO, "SDK legal files will NOT be included");
 
                execDirectory = Paths.get(System.getProperty("user.dir"));
                Path sdkMkP = findSdkMk(execDirectory);
@@ -406,6 +420,12 @@ public class Make {
                                }
                        });
 
+                       // add legal notices and licenses
+                       for (Path p : listLegalFilesToInclude(source).values()) {
+                               jarOut.putNextEntry(new JarEntry(p.getFileName().toString()));
+                               Files.copy(p, jarOut);
+                       }
+
                        // add sources
                        // TODO add effective BND, Eclipse project file, etc., in order to be able to
                        // repackage
@@ -425,6 +445,11 @@ public class Make {
 
                                try (JarOutputStream srcJarOut = new JarOutputStream(Files.newOutputStream(srcJarP), srcManifest)) {
                                        copySourcesToJar(srcP, srcJarOut, "");
+                                       // add legal notices and licenses
+                                       for (Path p : listLegalFilesToInclude(source).values()) {
+                                               jarOut.putNextEntry(new JarEntry(p.getFileName().toString()));
+                                               Files.copy(p, jarOut);
+                                       }
                                }
                        } else {
                                copySourcesToJar(srcP, jarOut, "OSGI-OPT/src/");
@@ -432,6 +457,37 @@ public class Make {
                }
        }
 
+       /** List the relevant legal files to include, from the SDK source base. */
+       Map<String, Path> listLegalFilesToInclude(Path bundleBase) throws IOException {
+               Map<String, Path> toInclude = new HashMap<>();
+               if (!noSdkLegal) {
+                       DirectoryStream<Path> sdkSrcLegal = Files.newDirectoryStream(sdkSrcBase, (p) -> {
+                               String fileName = p.getFileName().toString();
+                               return switch (fileName) {
+                               case "NOTICE":
+                               case "LICENSE":
+                               case "COPYING":
+                               case "COPYING.LESSER":
+                                       yield true;
+                               default:
+                                       yield false;
+                               };
+                       });
+                       for (Path p : sdkSrcLegal)
+                               toInclude.put(p.getFileName().toString(), p);
+               }
+               for(Iterator<Map.Entry<String, Path>> entries=toInclude.entrySet().iterator();entries.hasNext();) {
+                       Map.Entry<String, Path> entry= entries.next();
+                       Path inBundle = bundleBase.resolve(entry.getValue().getFileName());
+                       // remove file if it is also defined at bundle level
+                       // since it has already been copied
+                       // and has priority
+                       if(Files.exists(inBundle)) 
+                               entries.remove();
+               }               
+               return toInclude;
+       }
+
        /*
         * UTILITIES
         */