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

[sling-slingfeature-maven-plugin] branch master updated: SLING-9347 : Allow to specify includes/excludes for contents report

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

cziegeler pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-slingfeature-maven-plugin.git


The following commit(s) were added to refs/heads/master by this push:
     new f0f7ca1  SLING-9347 : Allow to specify includes/excludes for contents report
f0f7ca1 is described below

commit f0f7ca16544ae841b5a42856915019dbb75f1808
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Thu Apr 9 13:09:09 2020 +0200

    SLING-9347 : Allow to specify includes/excludes for contents report
---
 .../apache/sling/feature/maven/ProjectHelper.java  |   2 +-
 .../sling/feature/maven/mojos/ApisJarContext.java  |  76 +------------
 .../apache/sling/feature/maven/mojos/InfoMojo.java |  35 ++++++
 .../maven/mojos/reports/ContentsReporter.java      |   8 +-
 .../feature/maven/mojos/reports/ReportContext.java |   3 +
 .../mojos/selection/IncludeExcludeMatcher.java     | 123 +++++++++++++++++++++
 .../mojos/selection/TestIncludeExcludeMatcher.java |  53 +++++++++
 7 files changed, 225 insertions(+), 75 deletions(-)

diff --git a/src/main/java/org/apache/sling/feature/maven/ProjectHelper.java b/src/main/java/org/apache/sling/feature/maven/ProjectHelper.java
index 4c45663..c6b17fd 100644
--- a/src/main/java/org/apache/sling/feature/maven/ProjectHelper.java
+++ b/src/main/java/org/apache/sling/feature/maven/ProjectHelper.java
@@ -244,7 +244,7 @@ public abstract class ProjectHelper {
                    && artifact.getVersion().equals(id.getVersion())
                    && artifact.getType().equals(id.getType())
                    && ((id.getClassifier() == null && artifact.getClassifier() == null) || (id.getClassifier() != null && id.getClassifier().equals(artifact.getClassifier()))) ) {
-                    return artifact;
+                    return artifact.getFile() == null ? null : artifact;
                 }
             }
         }
diff --git a/src/main/java/org/apache/sling/feature/maven/mojos/ApisJarContext.java b/src/main/java/org/apache/sling/feature/maven/mojos/ApisJarContext.java
index 6df40b1..09587e7 100644
--- a/src/main/java/org/apache/sling/feature/maven/mojos/ApisJarContext.java
+++ b/src/main/java/org/apache/sling/feature/maven/mojos/ApisJarContext.java
@@ -33,6 +33,7 @@ import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.sling.feature.ArtifactId;
 import org.apache.sling.feature.extension.apiregions.api.ApiRegion;
 import org.apache.sling.feature.extension.apiregions.api.ApiRegions;
+import org.apache.sling.feature.maven.mojos.selection.IncludeExcludeMatcher;
 
 /**
  * Context for creating the api jars
@@ -160,7 +161,7 @@ class ApisJarContext {
 
     private final Map<ArtifactId, Model> modelCache = new HashMap<>();
 
-    private List<String[]> licenseDefaultMatches;
+    private IncludeExcludeMatcher licenseDefaultMatcher;
 
     public ApisJarContext(final File mainDir, final ArtifactId featureId, final ApiRegions regions) {
         this.featureId = featureId;
@@ -243,79 +244,10 @@ class ApisJarContext {
 
 
     public void setLicenseDefaults(final List<String> licenseDefaults) throws MojoExecutionException {
-        this.licenseDefaultMatches = parseMatches(licenseDefaults);
+        this.licenseDefaultMatcher = new IncludeExcludeMatcher(licenseDefaults, null, "=", true);
     }
 
     public String getLicenseDefault(final ArtifactId id) {
-        String result = null;
-        if (this.licenseDefaultMatches != null) {
-            result = match(id);
-        }
-        return result;
-    }
-    private List<String[]> parseMatches(final List<String> licenseDefaults) throws MojoExecutionException {
-        List<String[]> matches = null;
-        if (licenseDefaults != null && !licenseDefaults.isEmpty()) {
-            matches = new ArrayList<>();
-            for (final String t : licenseDefaults) {
-                final String[] parts;
-                if ( t.endsWith("=") ) {
-                    parts = new String[] {t.substring(0, t.length() - 1), ""};
-                } else {
-                    parts = t.split("=");
-                }
-                if ( parts.length != 2 ) {
-                    throw new MojoExecutionException("Illegal license default: " + t);
-                }
-                final String[] val = parts[0].split(":");
-                if (val.length > 5) {
-                    throw new MojoExecutionException("Illegal license default: " + t);
-                }
-                final String[] result = new String[val.length + 1];
-                System.arraycopy(val, 0, result, 1, val.length);
-                result[0] = parts[1];
-                matches.add(result);
-            }
-        }
-        return matches;
-    }
-
-    private boolean match(final String value, final String matcher) {
-        if (matcher.endsWith("*")) {
-            return value.startsWith(matcher.substring(0, matcher.length() - 1));
-        }
-        return matcher.equals(value);
-    }
-
-    private String match(final ArtifactId id) {
-        boolean match = false;
-
-        for(final String[] m : this.licenseDefaultMatches) {
-            match = match(id.getGroupId(), m[1]);
-            if (match && m.length > 2) {
-                match = match(id.getArtifactId(), m[2]);
-            }
-            if (match && m.length == 4) {
-                match = match(id.getVersion(), m[3]);
-            } else if (match && m.length == 5) {
-                match = match(id.getVersion(), m[4]);
-                if (match) {
-                    match = match(id.getType(), m[3]);
-                }
-            } else if (match && m.length == 6) {
-                match = match(id.getVersion(), m[5]);
-                if (match) {
-                    match = match(id.getType(), m[3]);
-                    if (match) {
-                        match = match(id.getClassifier(), m[4]);
-                    }
-                }
-            }
-            if (match) {
-                return m[0];
-            }
-        }
-
-        return null;
+        return this.licenseDefaultMatcher.matches(id);
     }
 }
diff --git a/src/main/java/org/apache/sling/feature/maven/mojos/InfoMojo.java b/src/main/java/org/apache/sling/feature/maven/mojos/InfoMojo.java
index 488846b..7b87c63 100644
--- a/src/main/java/org/apache/sling/feature/maven/mojos/InfoMojo.java
+++ b/src/main/java/org/apache/sling/feature/maven/mojos/InfoMojo.java
@@ -28,6 +28,8 @@ import java.util.ArrayList;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
@@ -43,6 +45,7 @@ import org.apache.sling.feature.maven.mojos.reports.DuplicatesReporter;
 import org.apache.sling.feature.maven.mojos.reports.ExportPackagesReporter;
 import org.apache.sling.feature.maven.mojos.reports.ReportContext;
 import org.apache.sling.feature.maven.mojos.reports.Reporter;
+import org.apache.sling.feature.maven.mojos.selection.IncludeExcludeMatcher;
 import org.apache.sling.feature.scanner.Scanner;
 
 
@@ -92,6 +95,24 @@ public class InfoMojo extends AbstractIncludingFeatureMojo {
     @Parameter(property = "outputDirectory")
     private File outputDirectory;
 
+    /**
+     * A comma separated list of artifact patterns to include. Follows the pattern
+     * "groupId:artifactId:type:classifier:version". Designed to allow specifying
+     * the set of includes from the command line.
+     * @since 1.2.0
+     */
+    @Parameter(property = "includes")
+    private String artifactIncludesList;
+
+    /**
+     * A comma separated list of artifact patterns to exclude. Follows the pattern
+     * "groupId:artifactId:type:classifier:version". Designed to allow specifying
+     * the set of excludes from the command line.
+     * @since 1.2.0
+     */
+    @Parameter(property = "excludes")
+    private String artifactExcludesList;
+
     @Deprecated
     @Parameter(property = "featureFile")
     private File featureFile;
@@ -136,6 +157,14 @@ public class InfoMojo extends AbstractIncludingFeatureMojo {
 
         // setup scanner
         final Scanner scanner = setupScanner();
+        final IncludeExcludeMatcher matcher;
+        if ( this.artifactIncludesList != null && !this.artifactIncludesList.isEmpty()) {
+            matcher = new IncludeExcludeMatcher(Stream.of(this.artifactIncludesList.split(",")).map(v -> v.trim()).collect(Collectors.toList()),
+                    this.artifactExcludesList == null ? null : Stream.of(this.artifactExcludesList.split(",")).map(v -> v.trim()).collect(Collectors.toList()),
+                    null, false);
+        } else {
+            matcher = null;
+        }
         final Map<String, List<String>> reports = new LinkedHashMap<>();
         final ReportContext ctx = new ReportContext() {
 
@@ -153,6 +182,11 @@ public class InfoMojo extends AbstractIncludingFeatureMojo {
             public void addReport(final String key, final List<String> output) {
                 reports.put(key, output);
             }
+
+            @Override
+            public boolean matches(final ArtifactId id) {
+                return matcher == null || matcher.matches(id) != null;
+            }
         };
         for(final Reporter reporter : reporters) {
             getLog().info("Generating report ".concat(reporter.getName().concat("...")));
@@ -239,6 +273,7 @@ public class InfoMojo extends AbstractIncludingFeatureMojo {
 
             @Override
             public URL provide(final ArtifactId id) {
+                getLog().info("Searching " + id.toMvnId());
                 try {
                     return ProjectHelper
                             .getOrResolveArtifact(project, mavenSession, artifactHandlerManager, artifactResolver, id)
diff --git a/src/main/java/org/apache/sling/feature/maven/mojos/reports/ContentsReporter.java b/src/main/java/org/apache/sling/feature/maven/mojos/reports/ContentsReporter.java
index d3892d9..fa6c4fa 100644
--- a/src/main/java/org/apache/sling/feature/maven/mojos/reports/ContentsReporter.java
+++ b/src/main/java/org/apache/sling/feature/maven/mojos/reports/ContentsReporter.java
@@ -45,12 +45,16 @@ public class ContentsReporter implements Reporter {
 
         for (final Feature feature : ctx.getFeatures()) {
             for (final Artifact bundle : feature.getBundles()) {
-                 bundles.add(bundle.getId());
+                if ( ctx.matches(bundle.getId())) {
+                    bundles.add(bundle.getId());
+                }
             }
             for (final Extension ext : feature.getExtensions()) {
                 if (ext.getType() == ExtensionType.ARTIFACTS) {
                     for (final Artifact artifact : ext.getArtifacts()) {
-                        artifacts.add(artifact.getId());
+                        if ( ctx.matches(artifact.getId())) {
+                            artifacts.add(artifact.getId());
+                        }
                     }
                 }
             }
diff --git a/src/main/java/org/apache/sling/feature/maven/mojos/reports/ReportContext.java b/src/main/java/org/apache/sling/feature/maven/mojos/reports/ReportContext.java
index 97f716f..579a1c6 100644
--- a/src/main/java/org/apache/sling/feature/maven/mojos/reports/ReportContext.java
+++ b/src/main/java/org/apache/sling/feature/maven/mojos/reports/ReportContext.java
@@ -18,6 +18,7 @@ package org.apache.sling.feature.maven.mojos.reports;
 
 import java.util.List;
 
+import org.apache.sling.feature.ArtifactId;
 import org.apache.sling.feature.Feature;
 import org.apache.sling.feature.scanner.Scanner;
 
@@ -28,4 +29,6 @@ public interface ReportContext {
     List<Feature> getFeatures();
 
     void addReport(String key, List<String> output);
+
+    boolean matches(ArtifactId id);
 }
diff --git a/src/main/java/org/apache/sling/feature/maven/mojos/selection/IncludeExcludeMatcher.java b/src/main/java/org/apache/sling/feature/maven/mojos/selection/IncludeExcludeMatcher.java
new file mode 100644
index 0000000..a0de334
--- /dev/null
+++ b/src/main/java/org/apache/sling/feature/maven/mojos/selection/IncludeExcludeMatcher.java
@@ -0,0 +1,123 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package org.apache.sling.feature.maven.mojos.selection;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.sling.feature.ArtifactId;
+
+public class IncludeExcludeMatcher {
+
+    private List<String[]> includeMatches;
+
+    private List<String[]> excludeMatches;
+
+    public IncludeExcludeMatcher(final List<String> incudes,
+            final List<String> excludes,
+            final String separator,
+            final boolean separatorRequired) throws MojoExecutionException {
+        this.includeMatches = parseMatches(incudes, separator, separatorRequired);
+        this.excludeMatches = parseMatches(excludes, null, false);
+    }
+
+    public String matches(final ArtifactId id) {
+        String result = null;
+        if ( this.includeMatches != null ) {
+            result = this.match(this.includeMatches, id);
+            if ( this.excludeMatches != null && this.match(this.excludeMatches, id) != null) {
+                result = null;
+            }
+        }
+        return result;
+    }
+
+    private List<String[]> parseMatches(final List<String> patterns,
+            final String separator,
+            final boolean separatorRequired) throws MojoExecutionException {
+        List<String[]> matches = null;
+        if (patterns != null && !patterns.isEmpty()) {
+            matches = new ArrayList<>();
+            for (final String t : patterns) {
+                final String[] parts;
+                if (separator == null ) {
+                    parts = new String[] {t, ""};
+                } else if (t.endsWith(separator)) {
+                    parts = new String[] {t.substring(0, t.length() - 1), ""};
+                } else if ( t.contains(separator) ) {
+                    parts = t.split(separator);
+                } else if ( separatorRequired ) {
+                    throw new MojoExecutionException("Illegal pattern: " + t);
+                } else {
+                    parts = new String[] {t, ""};
+                }
+                if ( parts.length != 2 ) {
+                    throw new MojoExecutionException("Illegal pattern: " + t);
+                }
+                final String[] val = parts[0].split(":");
+                if (val.length > 5) {
+                    throw new MojoExecutionException("Illegal pattern: " + t);
+                }
+                final String[] result = new String[val.length + 1];
+                System.arraycopy(val, 0, result, 1, val.length);
+                result[0] = parts[1];
+                matches.add(result);
+            }
+        }
+        return matches;
+    }
+
+    private boolean match(final String value, final String matcher) {
+        if (matcher.endsWith("*")) {
+            return value.startsWith(matcher.substring(0, matcher.length() - 1));
+        }
+        return matcher.equals(value);
+    }
+
+    private String match(final List<String[]> matches, final ArtifactId id) {
+        boolean match = false;
+
+        for(final String[] m : matches) {
+            match = match(id.getGroupId(), m[1]);
+            if (match && m.length > 2) {
+                match = match(id.getArtifactId(), m[2]);
+            }
+            if (match && m.length == 4) {
+                match = match(id.getVersion(), m[3]);
+            } else if (match && m.length == 5) {
+                match = match(id.getVersion(), m[4]);
+                if (match) {
+                    match = match(id.getType(), m[3]);
+                }
+            } else if (match && m.length == 6) {
+                match = match(id.getVersion(), m[5]);
+                if (match) {
+                    match = match(id.getType(), m[3]);
+                    if (match) {
+                        match = match(id.getClassifier(), m[4]);
+                    }
+                }
+            }
+            if (match) {
+                return m[0];
+            }
+        }
+
+        return null;
+    }
+}
diff --git a/src/test/java/org/apache/sling/feature/maven/mojos/selection/TestIncludeExcludeMatcher.java b/src/test/java/org/apache/sling/feature/maven/mojos/selection/TestIncludeExcludeMatcher.java
new file mode 100644
index 0000000..c13aff6
--- /dev/null
+++ b/src/test/java/org/apache/sling/feature/maven/mojos/selection/TestIncludeExcludeMatcher.java
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package org.apache.sling.feature.maven.mojos.selection;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.fail;
+
+import java.util.Arrays;
+
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.sling.feature.ArtifactId;
+import org.junit.Test;
+
+public class TestIncludeExcludeMatcher {
+
+    @Test public void testLicenseDefaultsMatcher() throws MojoExecutionException {
+        IncludeExcludeMatcher m = new IncludeExcludeMatcher(Arrays.asList("org.apache.sling=ASL2", "org.apache="),
+                null, "=", true);
+        assertEquals("ASL2", m.matches(ArtifactId.parse("org.apache.sling:artifact:1.0")));
+        assertEquals("", m.matches(ArtifactId.parse("org.apache:artifact:1.0")));
+        assertNull(m.matches(ArtifactId.parse("org.apache.felix:artifact:1.0")));
+
+        try {
+            new IncludeExcludeMatcher(Arrays.asList("org.apache.sling"),
+                    null, "=", true);
+            fail();
+        } catch ( MojoExecutionException e) {
+            // expected
+        }
+    }
+
+    @Test public void testInfoMatcher() throws MojoExecutionException {
+        IncludeExcludeMatcher m = new IncludeExcludeMatcher(Arrays.asList("org.apache.sling"),
+                Arrays.asList("org.apache.sling:api"), null, false);
+        assertEquals("", m.matches(ArtifactId.parse("org.apache.sling:artifact:1.0")));
+        assertNull(m.matches(ArtifactId.parse("org.apache.sling:api:1.0")));
+    }
+}