jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From k...@apache.org
Subject svn commit: r1868852 - in /jackrabbit/commons/filevault-package-maven-plugin/trunk/src: main/java/org/apache/jackrabbit/filevault/maven/packaging/ test/java/org/apache/jackrabbit/filevault/maven/packaging/
Date Thu, 24 Oct 2019 08:05:15 GMT
Author: kwin
Date: Thu Oct 24 08:05:15 2019
New Revision: 1868852

URL: http://svn.apache.org/viewvc?rev=1868852&view=rev
Log:
Merge branch 'feature/JCRVLT-370_enhanced-filters-for-embeds' into trunk

Modified:
    jackrabbit/commons/filevault-package-maven-plugin/trunk/src/main/java/org/apache/jackrabbit/filevault/maven/packaging/GenerateMetadataMojo.java
    jackrabbit/commons/filevault-package-maven-plugin/trunk/src/main/java/org/apache/jackrabbit/filevault/maven/packaging/SimpleEmbedded.java
    jackrabbit/commons/filevault-package-maven-plugin/trunk/src/test/java/org/apache/jackrabbit/filevault/maven/packaging/GenerateMetadataMojoTest.java

Modified: jackrabbit/commons/filevault-package-maven-plugin/trunk/src/main/java/org/apache/jackrabbit/filevault/maven/packaging/GenerateMetadataMojo.java
URL: http://svn.apache.org/viewvc/jackrabbit/commons/filevault-package-maven-plugin/trunk/src/main/java/org/apache/jackrabbit/filevault/maven/packaging/GenerateMetadataMojo.java?rev=1868852&r1=1868851&r2=1868852&view=diff
==============================================================================
--- jackrabbit/commons/filevault-package-maven-plugin/trunk/src/main/java/org/apache/jackrabbit/filevault/maven/packaging/GenerateMetadataMojo.java
(original)
+++ jackrabbit/commons/filevault-package-maven-plugin/trunk/src/main/java/org/apache/jackrabbit/filevault/maven/packaging/GenerateMetadataMojo.java
Thu Oct 24 08:05:15 2019
@@ -22,7 +22,6 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.net.URI;
 import java.net.URISyntaxException;
 import java.nio.file.Files;
 import java.text.DateFormat;
@@ -40,13 +39,17 @@ import java.util.TreeMap;
 import java.util.TreeSet;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.jar.Manifest;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 import java.util.stream.Collectors;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipFile;
 
+import org.apache.commons.io.FilenameUtils;
 import org.apache.jackrabbit.vault.fs.api.PathFilterSet;
 import org.apache.jackrabbit.vault.fs.config.ConfigurationException;
 import org.apache.jackrabbit.vault.fs.config.DefaultWorkspaceFilter;
+import org.apache.jackrabbit.vault.fs.filter.DefaultPathFilter;
 import org.apache.jackrabbit.vault.fs.io.AccessControlHandling;
 import org.apache.jackrabbit.vault.packaging.Dependency;
 import org.apache.jackrabbit.vault.packaging.PackageId;
@@ -288,6 +291,7 @@ public class GenerateMetadataMojo extend
      * <tr><td>type</td><td>String</td><td>Filter criterion
against the type of a project dependency. A pattern as described below.</td></tr>
      * <tr><td>classifier</td><td>String</td><td>Filter
criterion against the classifier of a project dependency. A pattern as described below.</td></tr>
      * <tr><td>filter</td><td>Boolean</td><td>If set
to {@code true} adds the embedded artifact location to the package's filter.</td></tr>
+     * <tr><td>isAllVersionsFilter</td><td>Boolean</td><td>If
{@code filter} is {@code true} and this is {@code true} as well, the filter entry will contain
all versions of the same artifact (by creating an according filter pattern).</td></tr>
      * <tr><td>target</td><td>String</td><td>The parent
folder location in the package where to place the embedded artifact. Falls back to {@link
#embeddedTarget} if not set.</td></tr>
      * </table>
      * </pre>
@@ -320,6 +324,7 @@ public class GenerateMetadataMojo extend
      * <tr><td>scope</td><td>ScopeArtifactFilter</td><td>Filter
criterion against the <a href="https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope">scope
of a project dependency</a>. Possible values are <ul><li>{@code test}, which
allows every scope</li><li>{@code compile+runtime} which allows every scope except
{@code test}</li><li>{@code runtime+system} which allows every scope except {@code
test} and {@code provided}</li><li>{@code compile} which allows only scope {@code
compile}, {@code provided} and {@code system}</li><li>{@code runtime} which only
allows scope {@code runtime} and {@code compile}.</td></tr>
      * <tr><td>type</td><td>String</td><td>Filter criterion
against the type of a project dependency.A pattern as described below.</td></tr>
      * <tr><td>classifier</td><td>String</td><td>Filter
criterion against the classifier of a project dependency. A pattern as described below.</td></tr>
+     * <tr><td>isAllVersionsFilter</td><td>Boolean</td><td>If
{@code filter} is {@code true} and this is {@code true} as well, the filter entry will contain
all versions of the same artifact (by creating an according filter pattern).</td></tr>
      * <tr><td>filter</td><td>Boolean</td><td>If set
to {@code true} adds the embedded artifact location to the package's filter</td></tr>
      * </table>
      * </pre>
@@ -388,6 +393,9 @@ public class GenerateMetadataMojo extend
             required = true)
     boolean allowIndexDefinitions;
 
+    // take the first "-" followed by a digit as separator between version suffix and rest
+    private static final Pattern FILENAME_PATTERN_WITHOUT_VERSION_IN_GROUP1 = Pattern.compile("((?!-\\d).*-)\\d.*");
+
     /**
      * Sets the package type.
      * @param type the string representation of the package type
@@ -454,8 +462,10 @@ public class GenerateMetadataMojo extend
 
             // this must happen before the filter rules are extended 
             // but after filters have been consolidated
-            computePackageTypeIfNotSet();
-            
+            if (packageType == null) {
+                packageType = computePackageType();
+            }
+
             // calculate the embeddeds and subpackages
             Map<String, File> embeddedFiles = getEmbeddeds();
             embeddedFiles.putAll(getSubPackages());
@@ -489,7 +499,7 @@ public class GenerateMetadataMojo extend
             try (OutputStream out = new FileOutputStream(getGeneratedManifestFile())) {
                 manifest.write(out);
             }
-        } catch (IOException | ManifestException | DependencyResolutionRequiredException
e) {
+        } catch (IOException | ManifestException | DependencyResolutionRequiredException
| ConfigurationException e) {
             throw new MojoExecutionException(e.toString(), e);
         }
         buildContext.refresh(vaultDir);
@@ -573,7 +583,6 @@ public class GenerateMetadataMojo extend
             // there is no suitable clone nor constructor, therefore use a serialization/deserialization
approach
             try (InputStream serializedFilters = sourceFilters.getSource()) {
                 filters.load(serializedFilters);
-                //filters.resetSource();
             } catch (ConfigurationException e) {
                 throw new IllegalStateException("cloning filters failed.", e);
             }
@@ -590,7 +599,7 @@ public class GenerateMetadataMojo extend
 
         // if the prefix property is set, it should be used if no filter is set
         if (filters.getFilterSets().isEmpty() && prefix.length() > 0) {
-            addWorkspaceFilter(prefix);
+            filters.add(new PathFilterSet(prefix));
         }
 
         return sourceFilters.getSourceAsString();
@@ -851,7 +860,7 @@ public class GenerateMetadataMojo extend
     }
     
 
-    private Map<String, File> getEmbeddeds() throws MojoFailureException {
+    private Map<String, File> getEmbeddeds() throws MojoFailureException, ConfigurationException
{
         Map<String, File> fileMap = new HashMap<>();
         for (Embedded emb : embeddeds) {
             final Collection<Artifact> artifacts = emb.getMatchingArtifacts(project);
@@ -927,14 +936,14 @@ public class GenerateMetadataMojo extend
                 fileMap.put(targetPathName, source);
 
                 if (emb.isFilter()) {
-                    addWorkspaceFilter(targetNodePathName);
+                    addEmbeddedFileToFilter(targetNodePathName, emb.isAllVersionsFilter());
                 }
             }
         }
         return fileMap;
     }
 
-    private Map<String, File> getSubPackages() throws MojoFailureException {
+    private Map<String, File> getSubPackages() throws MojoFailureException, ConfigurationException
{
         final String propsRelPath = Constants.META_DIR + "/" + Constants.PROPERTIES_XML;
         Map<String, File> fileMap = new HashMap<>();
         for (SubPackage pack : subPackages) {
@@ -945,7 +954,7 @@ public class GenerateMetadataMojo extend
             }
 
             // get the package path
-            getLog().info("Embedding --- " + pack + " ---");
+            getLog().info("Embedding subpackage --- " + pack + " ---");
             for (Artifact artifact : artifacts) {
                 final Properties props = new Properties();
 
@@ -1002,7 +1011,7 @@ public class GenerateMetadataMojo extend
                 getLog().info(String.format("Embedding %s (from %s) -> %s", artifact.getId(),
source.getAbsolutePath(), targetPathName));
                 fileMap.put(targetPathName, source);
                 if (pack.isFilter()) {
-                    addWorkspaceFilter(targetNodePathName);
+                    addEmbeddedFileToFilter(targetNodePathName, pack.isAllVersionsFilter());
                 }
             }
         }
@@ -1057,8 +1066,39 @@ public class GenerateMetadataMojo extend
         return this.embedArtifactLayout.pathOf(artifact);
     }
 
-    private void addWorkspaceFilter(final String filterRoot) {
-        filters.add(new PathFilterSet(filterRoot));
+    private void addEmbeddedFileToFilter(String embeddedFile, boolean includeAllVersions)
throws ConfigurationException {
+        filters.add(getPathFilterSetForEmbeddedFile(embeddedFile, includeAllVersions));
+    }
+
+    static PathFilterSet getPathFilterSetForEmbeddedFile(final String embeddedFile, boolean
includeAllVersions)
+            throws ConfigurationException {
+        final PathFilterSet pathFilterSet;
+        if (includeAllVersions) {
+            String filename = FilenameUtils.getName(embeddedFile);
+            // shorten the filter root by one level
+            String rootName = StringUtils.chomp(embeddedFile, "/");
+            String extension = FilenameUtils.getExtension(filename);
+
+            // now find a mattern which should apply to embeddedFile and all similar artifacts
with different versions from format:
+            // ${artifactId}-${version})
+            Matcher matcher = FILENAME_PATTERN_WITHOUT_VERSION_IN_GROUP1.matcher(filename);
+            if (!matcher.matches()) {
+                throw new IllegalArgumentException("Could not figure out version part in
filename '" + filename
+                        + "'. For this artifact you cannot use 'isAllVersionsFilter=true'");
+            }
+
+            // create new pattern which matches the same artifacts in all versions
+            String pattern = Pattern.quote(matcher.group(1)) + ".*" + "\\." + extension +
"(/.*)?";
+            if (!filename.matches(pattern)) {
+                throw new IllegalArgumentException("Detected pattern '" + pattern + "' does
not even match given filename '" + filename
+                        + "'. For this artifact you cannot use 'isAllVersionsFilter=true'");
+            }
+            pathFilterSet = new PathFilterSet(rootName);
+            pathFilterSet.addInclude(new DefaultPathFilter(pattern));
+        } else {
+            pathFilterSet = new PathFilterSet(embeddedFile);
+        }
+        return pathFilterSet;
     }
 
     private String makeAbsolutePath(final String relPath) {
@@ -1073,43 +1113,43 @@ public class GenerateMetadataMojo extend
         return absPath;
     }
 
-    private void computePackageTypeIfNotSet() {
-        if (packageType == null) {
-            // auto detect...
-            boolean hasApps = false;
-            boolean hasOther = false;
-            for (PathFilterSet p: filters.getFilterSets()) {
-                if (PathFilterSet.TYPE_CLEANUP.equals(p.getType())) {
-                    continue;
-                }
-                String root = p.getRoot();
-                if ("/apps".equals(root) || root.startsWith("/apps/") || "/libs".equals(root)
|| root.startsWith("/libs/")) {
-                    hasApps = true;
-                    getLog().debug("Detected /apps or /libs filter entry: " + p);
-                } else {
-                    hasOther = true;
-                    getLog().debug("Detected filter entry outside /apps and /libs: " + p);
-                }
+    private PackageType computePackageType() {
+        final PackageType packageType;
+        // auto detect...
+        boolean hasApps = false;
+        boolean hasOther = false;
+        for (PathFilterSet p : filters.getFilterSets()) {
+            if (PathFilterSet.TYPE_CLEANUP.equals(p.getType())) {
+                continue;
             }
-            // no embeds and subpackages?
-            getLog().debug("Detected " + embeddeds.length + " bundle(s) and " + subPackages.length
+ " sub package(s).");
-            if (embeddeds.length == 0 && subPackages.length == 0) {
-                if (hasApps && !hasOther) {
-                    packageType = PackageType.APPLICATION;
-                } else if (hasOther && !hasApps) {
-                    packageType = PackageType.CONTENT;
-                } else {
-                    packageType = PackageType.MIXED;
-                }
+            String root = p.getRoot();
+            if ("/apps".equals(root) || root.startsWith("/apps/") || "/libs".equals(root)
|| root.startsWith("/libs/")) {
+                hasApps = true;
+                getLog().debug("Detected /apps or /libs filter entry: " + p);
             } else {
-                if (!hasApps && !hasOther) {
-                    packageType = PackageType.CONTAINER;
-                } else {
-                    packageType = PackageType.MIXED;
-                }
+                hasOther = true;
+                getLog().debug("Detected filter entry outside /apps and /libs: " + p);
+            }
+        }
+        // no embeds and subpackages?
+        getLog().debug("Detected " + embeddeds.length + " bundle(s) and " + subPackages.length
+ " sub package(s).");
+        if (embeddeds.length == 0 && subPackages.length == 0) {
+            if (hasApps && !hasOther) {
+                packageType = PackageType.APPLICATION;
+            } else if (hasOther && !hasApps) {
+                packageType = PackageType.CONTENT;
+            } else {
+                packageType = PackageType.MIXED;
+            }
+        } else {
+            if (!hasApps && !hasOther) {
+                packageType = PackageType.CONTAINER;
+            } else {
+                packageType = PackageType.MIXED;
             }
-            getLog().info("Auto-detected package type: " + packageType.toString().toLowerCase());
         }
+        getLog().info("Auto-detected package type: " + packageType.toString().toLowerCase());
+        return packageType;
     }
 
     /**

Modified: jackrabbit/commons/filevault-package-maven-plugin/trunk/src/main/java/org/apache/jackrabbit/filevault/maven/packaging/SimpleEmbedded.java
URL: http://svn.apache.org/viewvc/jackrabbit/commons/filevault-package-maven-plugin/trunk/src/main/java/org/apache/jackrabbit/filevault/maven/packaging/SimpleEmbedded.java?rev=1868852&r1=1868851&r2=1868852&view=diff
==============================================================================
--- jackrabbit/commons/filevault-package-maven-plugin/trunk/src/main/java/org/apache/jackrabbit/filevault/maven/packaging/SimpleEmbedded.java
(original)
+++ jackrabbit/commons/filevault-package-maven-plugin/trunk/src/main/java/org/apache/jackrabbit/filevault/maven/packaging/SimpleEmbedded.java
Thu Oct 24 08:05:15 2019
@@ -30,6 +30,9 @@ import org.apache.maven.artifact.Artifac
 import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
 import org.apache.maven.project.MavenProject;
 
+/**
+ * Abstract base class for all kinds of embeds (both OSGi bundles as well as subpackages)
+ */
 public class SimpleEmbedded {
 
     /**
@@ -59,6 +62,12 @@ public class SimpleEmbedded {
      */
     private boolean filter;
 
+    /**
+     * If {@link #filter} is {@code true} and this is {@code true} as well, the filter entry
will contain
+     * all versions of the same artifact.
+     */
+    private boolean isAllVersionsFilter;
+
     private boolean excludeTransitive;
     
     public void setGroupId(String groupId) throws ConfigurationException {
@@ -88,6 +97,11 @@ public class SimpleEmbedded {
     public boolean isFilter() {
         return filter;
     }
+
+    public boolean isAllVersionsFilter() {
+        return isAllVersionsFilter;
+        
+    }
     
     public void setExcludeTransitive(boolean excludeTransitive) {
         this.excludeTransitive = excludeTransitive;

Modified: jackrabbit/commons/filevault-package-maven-plugin/trunk/src/test/java/org/apache/jackrabbit/filevault/maven/packaging/GenerateMetadataMojoTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/commons/filevault-package-maven-plugin/trunk/src/test/java/org/apache/jackrabbit/filevault/maven/packaging/GenerateMetadataMojoTest.java?rev=1868852&r1=1868851&r2=1868852&view=diff
==============================================================================
--- jackrabbit/commons/filevault-package-maven-plugin/trunk/src/test/java/org/apache/jackrabbit/filevault/maven/packaging/GenerateMetadataMojoTest.java
(original)
+++ jackrabbit/commons/filevault-package-maven-plugin/trunk/src/test/java/org/apache/jackrabbit/filevault/maven/packaging/GenerateMetadataMojoTest.java
Thu Oct 24 08:05:15 2019
@@ -21,7 +21,11 @@ import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.util.jar.Attributes;
 import java.util.jar.Manifest;
+import java.util.regex.Pattern;
 
+import org.apache.jackrabbit.vault.fs.api.PathFilterSet;
+import org.apache.jackrabbit.vault.fs.config.ConfigurationException;
+import org.apache.jackrabbit.vault.fs.filter.DefaultPathFilter;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -39,6 +43,31 @@ public class GenerateMetadataMojoTest {
         assertEscapedValueWorksInManifest("some very long line above 72 chars. some very
long line above 72 chars. some very long line above 72 chars\r\rAnother paragraph");
     }
 
+    @Test
+    public void testGetPathFilterSetForEmbeddedFile() throws ConfigurationException {
+        // TODO: check filter
+        // use OSGi bundle filename patterns first
+        PathFilterSet expectedPathFilter = new PathFilterSet("/apps/install/jcr-2.0.jar");
+        Assert.assertEquals(expectedPathFilter, GenerateMetadataMojo.getPathFilterSetForEmbeddedFile("/apps/install/jcr-2.0.jar",
false));
+        expectedPathFilter = new PathFilterSet("/apps/install");
+        expectedPathFilter.addInclude(new DefaultPathFilter(Pattern.quote("jcr-") + ".*\\.jar(/.*)?"));
+        Assert.assertEquals(expectedPathFilter, GenerateMetadataMojo.getPathFilterSetForEmbeddedFile("/apps/install/jcr-2.0.jar",
true));
+        Assert.assertEquals(expectedPathFilter, GenerateMetadataMojo.getPathFilterSetForEmbeddedFile("/apps/install/jcr-3.0.jar",
true));
+
+        expectedPathFilter = new PathFilterSet("/apps/some/other/install");
+        expectedPathFilter.addInclude(new DefaultPathFilter(Pattern.quote("jcr-") + ".*\\.jar(/.*)?"));
+        Assert.assertEquals(expectedPathFilter, GenerateMetadataMojo.getPathFilterSetForEmbeddedFile("/apps/some/other/install/jcr-2.0-alpha1.jar",
true));
+
+        // then test against some sub package names
+        // look at PackageId.getInstallationPath for patterns ("/etc/packages/<group>/<name>-<version>.zip")
+        expectedPathFilter = new PathFilterSet("/etc/packages/some/weird/group/name-1.0.zip");
+        Assert.assertEquals(expectedPathFilter, GenerateMetadataMojo.getPathFilterSetForEmbeddedFile("/etc/packages/some/weird/group/name-1.0.zip",
false));
+        
+        expectedPathFilter = new PathFilterSet("/etc/packages/some/weird/group");
+        expectedPathFilter.addInclude(new DefaultPathFilter(Pattern.quote("name-") + ".*\\.zip(/.*)?"));
+        Assert.assertEquals(expectedPathFilter, GenerateMetadataMojo.getPathFilterSetForEmbeddedFile("/etc/packages/some/weird/group/name-1.0.zip",
true));
+    }
+
     private void assertEscapedValueWorksInManifest(String value) throws IOException {
         String escapedValue = GenerateMetadataMojo.escapeManifestValue(value);
         Manifest manifest = new Manifest();
@@ -56,7 +85,6 @@ public class GenerateMetadataMojoTest {
                 Assert.assertEquals(removeNewlines(value), unescapeContinuations(actualValue));
             }
         };
-        
     }
 
     private static String removeNewlines(String value) {



Mime
View raw message