Fix clean of failed builds
[cc0/argeo-build.git] / src / org / argeo / build / Repackage.java
index d90375e3f442aae12b868223e82b52024c1566a5..d33a55eea1f6aa35aedc9e8e7e1ceee583e5e89d 100644 (file)
@@ -98,13 +98,14 @@ public class Repackage {
 
                List<CompletableFuture<Void>> toDos = new ArrayList<>();
                for (int i = 1; i < args.length; i++) {
-                       Path p = Paths.get(args[i]);
-                       if (sequential)
-                               factory.processCategory(p);
+                       Path categoryPath = Paths.get(args[i]);
+                       factory.cleanPreviousFailedBuild(categoryPath);
+                       if (sequential) // sequential processing happens here
+                               factory.processCategory(categoryPath);
                        else
-                               toDos.add(CompletableFuture.runAsync(() -> factory.processCategory(p)));
+                               toDos.add(CompletableFuture.runAsync(() -> factory.processCategory(categoryPath)));
                }
-               if (!sequential)
+               if (!sequential)// parallel processing
                        CompletableFuture.allOf(toDos.toArray(new CompletableFuture[toDos.size()])).join();
 
                // Summary
@@ -115,6 +116,23 @@ public class Repackage {
                logger.log(INFO, "# License summary:\n" + sb);
        }
 
+       /** Deletes remaining sub directories. */
+       void cleanPreviousFailedBuild(Path categoryPath) {
+               Path outputCategoryPath = a2Base.resolve(categoryPath);
+               // clean previous failed build
+               try {
+                       for (Path subDir : Files.newDirectoryStream(outputCategoryPath, (d) -> Files.isDirectory(d))) {
+                               if (Files.exists(subDir)) {
+                                       logger.log(WARNING, "Bundle dir " + subDir
+                                                       + " already exists, probably from a previous failed build, deleting it...");
+                                       deleteDirectory(subDir);
+                               }
+                       }
+               } catch (IOException e) {
+                       logger.log(ERROR, "Cannot clean previous build", e);
+               }
+       }
+
        /** MANIFEST headers. */
        enum ManifestHeader {
                // OSGi
@@ -585,7 +603,7 @@ public class Repackage {
                                                        logger.log(TRACE, "Skip " + entry.getName());
                                                        continue entries;
                                                } else if (entry.getName().startsWith("META-INF/NOTICE")) {
-                                                       logger.log(TRACE, "Skip " + entry.getName());
+                                                       logger.log(WARNING, "Skip " + entry.getName() + " from " + artifact);
                                                        // TODO merge them?
                                                        continue entries;
                                                } else {
@@ -687,8 +705,7 @@ public class Repackage {
                                }
 
                                if (!fileProps.containsKey(EXPORT_PACKAGE.toString())) {
-                                       fileProps.put(EXPORT_PACKAGE.toString(),
-                                                       "*;version=\"" + fileProps.getProperty(BUNDLE_VERSION.toString()) + "\"");
+                                       fileProps.put(EXPORT_PACKAGE.toString(), "*");
                                }
 
                                // BND analysis
@@ -1029,6 +1046,7 @@ public class Repackage {
                                        nameVersion.setName(ourSymbolicName);
                                }
                        }
+
                        bundleDir = targetBase.resolve(nameVersion.getName() + "." + nameVersion.getBranch());
 
                        // copy original MANIFEST
@@ -1054,7 +1072,6 @@ public class Repackage {
                                arch = libRelativePath.getName(1).toString();
                        }
 
-//                     if (!embed) {
                        // copy entries
                        JarEntry entry;
                        entries: while ((entry = jarIn.getNextJarEntry()) != null) {
@@ -1121,7 +1138,6 @@ public class Repackage {
                                        origin.deleted.add(bundleDir.relativize(target).toString());
                                }
                                logger.log(TRACE, () -> "Copied " + target);
-//                             }
                        }
                }
 
@@ -1134,11 +1150,6 @@ public class Repackage {
                                        entries.get(BUNDLE_SYMBOLICNAME.toString()) + ";singleton:=true");
                }
 
-//             if (embed) {// copy embedded jar
-//                     Files.copy(file, bundleDir.resolve(file.getFileName()));
-//                     entries.put(ManifestHeader.BUNDLE_CLASSPATH.toString(), file.getFileName().toString());
-//             }
-
                // Final MANIFEST decisions
                // We also check the original OSGi metadata and compare with our changes
                for (String key : entries.keySet()) {