You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by jb...@apache.org on 2020/09/28 11:53:27 UTC

[karaf] branch karaf-4.2.x updated: Enable to drop some files from the assembly in the mojo of the same name (#1205)

This is an automated email from the ASF dual-hosted git repository.

jbonofre pushed a commit to branch karaf-4.2.x
in repository https://gitbox.apache.org/repos/asf/karaf.git


The following commit(s) were added to refs/heads/karaf-4.2.x by this push:
     new 372c479  Enable to drop some files from the assembly in the mojo of the same name (#1205)
372c479 is described below

commit 372c47994055317fe4c6fdaa389425e49e00faf5
Author: Romain Manni-Bucau <rm...@gmail.com>
AuthorDate: Mon Sep 28 13:52:40 2020 +0200

    Enable to drop some files from the assembly in the mojo of the same name (#1205)
    
    (cherry picked from commit 7073eb3403466a5e3ccd8310cf8c03ab91fa282f)
---
 .../org/apache/karaf/tooling/AssemblyMojo.java     | 55 ++++++++++++++++++++++
 1 file changed, 55 insertions(+)

diff --git a/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/AssemblyMojo.java b/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/AssemblyMojo.java
index 141d0ba..69a1bcb 100644
--- a/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/AssemblyMojo.java
+++ b/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/AssemblyMojo.java
@@ -27,6 +27,7 @@ import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.nio.file.Files;
+import java.nio.file.Path;
 import java.nio.file.attribute.PosixFilePermissions;
 import java.util.ArrayList;
 import java.util.Collections;
@@ -36,6 +37,7 @@ import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
+import java.util.function.Predicate;
 import java.util.jar.JarFile;
 import java.util.jar.Manifest;
 import java.util.stream.Collectors;
@@ -62,6 +64,8 @@ import org.eclipse.aether.repository.WorkspaceReader;
 import org.osgi.framework.Constants;
 import org.osgi.framework.launch.FrameworkFactory;
 
+import static java.util.stream.Collectors.toList;
+
 /**
  * Creates a customized Karaf distribution by installing features and setting up
  * configuration files.
@@ -453,6 +457,14 @@ public class AssemblyMojo extends MojoSupport {
     @Parameter
     protected Map<String, String> system;
 
+
+    /**
+     * List of files to delete from the source assembly.
+     * Note that it is done after the target assembly is done so it can also remove side effects of the configuration.
+     */
+    @Parameter
+    protected List<String> filesToRemove;
+
     @Component(role = WorkspaceReader.class, hint = "reactor")
     protected WorkspaceReader reactor;
 
@@ -586,6 +598,49 @@ public class AssemblyMojo extends MojoSupport {
                 }
             }
         }
+
+        if (filesToRemove != null) {
+            final Path base = workDirectory.toPath();
+            filesToRemove.forEach(toDrop -> {
+                final int lastSep = Math.max(toDrop.lastIndexOf('/'), toDrop.lastIndexOf(File.separatorChar));
+                final boolean startsWithDir = toDrop.contains(File.separator) || toDrop.contains("/");
+                final String name = !startsWithDir ? toDrop : toDrop.substring(lastSep + 1);
+                final Path dir = !startsWithDir ? base : base.resolve(toDrop.substring(0, lastSep));
+                final int wildcard = name.lastIndexOf('*');
+                final Predicate<String> matcher;
+                if (wildcard >= 0) {
+                    final String suffix = name.substring(wildcard + 1);
+                    final String prefix = name.substring(0, wildcard);
+                    matcher = n -> n.startsWith(prefix) && n.endsWith(suffix);
+                } else {
+                    // we likely bet this case will not happen often (to ignore the version at least)
+                    // so we don't optimize this branch by deleting directly the file
+                    matcher = name::equals;
+                }
+                try {
+                    final List<Path> toDelete = Files.list(dir)
+                            .filter(it -> matcher.test(it.getFileName().toString()))
+                            .collect(toList());
+                    if (toDelete.isEmpty()) {
+                        getLog().info("File deletion '" + toDrop + "' ignored (not found)");
+                    } else {
+                        toDelete.stream().peek(it -> getLog().info("Deleting '" + base.relativize(it) + "'")).forEach(it -> {
+                            try {
+                                if (Files.isDirectory(it)) {
+                                    IoUtils.deleteRecursive(it.toFile());
+                                } else {
+                                    Files.delete(it);
+                                }
+                            } catch (final IOException e) {
+                                throw new IllegalStateException(e);
+                            }
+                        });
+                    }
+                } catch (final IOException e) {
+                    throw new IllegalStateException(e);
+                }
+            });
+        }
     }
 
     private void configureWorkDirectory() {