You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by GitBox <gi...@apache.org> on 2018/11/29 19:53:35 UTC

[GitHub] rfscholte closed pull request #5: [MDEP-628] Parameter `fileMappers` for unpack path rewriting

rfscholte closed pull request #5: [MDEP-628] Parameter `fileMappers` for unpack path rewriting
URL: https://github.com/apache/maven-dependency-plugin/pull/5
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/pom.xml b/pom.xml
index c43e0fc..a90a917 100644
--- a/pom.xml
+++ b/pom.xml
@@ -179,7 +179,7 @@ under the License.
     <dependency>
       <groupId>org.codehaus.plexus</groupId>
       <artifactId>plexus-archiver</artifactId>
-      <version>3.6.0</version>
+      <version>3.7.0</version>
     </dependency>
     <dependency>
       <groupId>org.codehaus.plexus</groupId>
diff --git a/src/it/projects/unpack-dependencies-with-filemappers/invoker.properties b/src/it/projects/unpack-dependencies-with-filemappers/invoker.properties
new file mode 100644
index 0000000..2e199f7
--- /dev/null
+++ b/src/it/projects/unpack-dependencies-with-filemappers/invoker.properties
@@ -0,0 +1,18 @@
+# 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.
+
+invoker.goals = clean package
diff --git a/src/it/projects/unpack-dependencies-with-filemappers/pom.xml b/src/it/projects/unpack-dependencies-with-filemappers/pom.xml
new file mode 100644
index 0000000..addcf71
--- /dev/null
+++ b/src/it/projects/unpack-dependencies-with-filemappers/pom.xml
@@ -0,0 +1,76 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+  ~ 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.
+  -->
+  
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+
+  <modelVersion>4.0.0</modelVersion>
+  
+  <groupId>org.apache.maven.its.dependency</groupId>
+  <artifactId>unpack-dependencies</artifactId>
+  <version>1.0-SNAPSHOT</version>
+
+  <name>Test</name>
+  <description>
+    Test dependency:unpack-dependencies using fileMappers
+  </description>
+  
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+
+  <dependencies>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>4.10</version>
+    </dependency>
+  </dependencies>
+
+  <build>
+
+    
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-dependency-plugin</artifactId>
+        <version>@project.version@</version>
+        <executions>
+          <execution>
+            <id>unpack</id>
+            <goals>
+              <goal>unpack-dependencies</goal>
+            </goals>
+            <configuration>
+              <excludes>*.txt,META-INF/**</excludes>
+              <fileMappers>
+                <org.codehaus.plexus.components.io.filemappers.PrefixFileMapper>
+                  <prefix>mapped/</prefix>
+                </org.codehaus.plexus.components.io.filemappers.PrefixFileMapper>
+              </fileMappers>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+</project>
diff --git a/src/it/projects/unpack-dependencies-with-filemappers/verify.groovy b/src/it/projects/unpack-dependencies-with-filemappers/verify.groovy
new file mode 100644
index 0000000..de665cc
--- /dev/null
+++ b/src/it/projects/unpack-dependencies-with-filemappers/verify.groovy
@@ -0,0 +1,42 @@
+/*
+ * 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.
+ */
+
+expected = ['org','junit']
+
+for (item in expected)
+{
+    def file = new File(basedir, 'target/dependency/mapped/' + item)
+    if (!file.exists())
+    {
+       throw new RuntimeException("Missing "+file.name);
+    }
+}
+
+notExpected = ['META-INF','LICENSE.TXT']
+
+for (item in notExpected)
+{
+    def file = new File(basedir, 'target/dependency/' + item)
+    if (file.exists())
+    {
+       throw new RuntimeException("This file shouldn't be here: "+file.name);
+    }
+}
+
+return true;
diff --git a/src/main/java/org/apache/maven/plugins/dependency/AbstractDependencyMojo.java b/src/main/java/org/apache/maven/plugins/dependency/AbstractDependencyMojo.java
index 43a0f6f..3d4ba58 100644
--- a/src/main/java/org/apache/maven/plugins/dependency/AbstractDependencyMojo.java
+++ b/src/main/java/org/apache/maven/plugins/dependency/AbstractDependencyMojo.java
@@ -41,6 +41,7 @@
 import org.codehaus.plexus.archiver.manager.ArchiverManager;
 import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
 import org.codehaus.plexus.archiver.zip.ZipUnArchiver;
+import org.codehaus.plexus.components.io.filemappers.FileMapper;
 import org.codehaus.plexus.components.io.fileselectors.IncludeExcludeFileSelector;
 import org.codehaus.plexus.util.FileUtils;
 import org.codehaus.plexus.util.ReflectionUtils;
@@ -193,12 +194,14 @@ protected void copyFile( File artifact, File destFile )
      * @param artifact {@link Artifact}
      * @param location The location.
      * @param encoding The encoding.
+     * @param fileMappers {@link FileMapper}s to be used for rewriting each target path, or {@code null} if no rewriting
+     *                    shall happen.
      * @throws MojoExecutionException in case of an error.
      */
-    protected void unpack( Artifact artifact, File location, String encoding )
+    protected void unpack( Artifact artifact, File location, String encoding, FileMapper[] fileMappers )
         throws MojoExecutionException
     {
-        unpack( artifact, location, null, null, encoding );
+        unpack( artifact, location, null, null, encoding, fileMappers );
     }
 
     /**
@@ -211,12 +214,14 @@ protected void unpack( Artifact artifact, File location, String encoding )
      * @param excludes Comma separated list of file patterns to exclude i.e. <code>**&#47;*.xml,
      *                 **&#47;*.properties</code>
      * @param encoding Encoding of artifact. Set {@code null} for default encoding.
+     * @param fileMappers {@link FileMapper}s to be used for rewriting each target path, or {@code null} if no rewriting
+     *                    shall happen.
      * @throws MojoExecutionException In case of errors.
      */
-    protected void unpack( Artifact artifact, File location, String includes, String excludes, String encoding )
-        throws MojoExecutionException
+    protected void unpack( Artifact artifact, File location, String includes, String excludes, String encoding,
+                           FileMapper[] fileMappers ) throws MojoExecutionException
     {
-        unpack( artifact, artifact.getType(), location, includes, excludes, encoding );
+        unpack( artifact, artifact.getType(), location, includes, excludes, encoding, fileMappers );
     }
 
     /**
@@ -226,10 +231,12 @@ protected void unpack( Artifact artifact, File location, String includes, String
      * @param includes includes list.
      * @param excludes excludes list.
      * @param encoding the encoding.
+     * @param fileMappers {@link FileMapper}s to be used for rewriting each target path, or {@code null} if no rewriting
+     *                    shall happen.
      * @throws MojoExecutionException in case of an error.
      */
     protected void unpack( Artifact artifact, String type, File location, String includes, String excludes,
-                           String encoding )
+                           String encoding, FileMapper[] fileMappers )
         throws MojoExecutionException
     {
         File file = artifact.getFile();
@@ -303,6 +310,8 @@ protected void unpack( Artifact artifact, String type, File location, String inc
                 silenceUnarchiver( unArchiver );
             }
 
+            unArchiver.setFileMappers( fileMappers );
+
             unArchiver.extract();
         }
         catch ( NoSuchArchiverException e )
diff --git a/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/ArtifactItem.java b/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/ArtifactItem.java
index db215ce..1967998 100644
--- a/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/ArtifactItem.java
+++ b/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/ArtifactItem.java
@@ -25,6 +25,7 @@
 import org.apache.maven.artifact.ArtifactUtils;
 import org.apache.maven.plugins.dependency.utils.DependencyUtil;
 import org.apache.maven.shared.dependencies.DependableCoordinate;
+import org.codehaus.plexus.components.io.filemappers.FileMapper;
 import org.codehaus.plexus.util.StringUtils;
 
 /**
@@ -120,6 +121,15 @@
      */
     private String excludes;
 
+    /**
+     * {@link FileMapper}s to be used for rewriting each target path, or {@code null} if no rewriting shall happen.
+     *
+     * @since 3.1.2
+     *
+     * @parameter
+     */
+    private FileMapper[] fileMappers;
+
     /**
      * Default ctor.
      */
@@ -381,4 +391,26 @@ public void setIncludes( String includes )
     {
         this.includes = includes;
     }
+
+    /**
+     * @return {@link FileMapper}s to be used for rewriting each target path, or {@code null} if no rewriting shall
+     *         happen.
+     *
+     * @since 3.1.2
+     */
+    public FileMapper[] getFileMappers()
+    {
+        return this.fileMappers;
+    }
+
+    /**
+     * @param fileMappers {@link FileMapper}s to be used for rewriting each target path, or {@code null} if no
+     * rewriting shall happen.
+     *
+     * @since 3.1.2
+     */
+    public void setFileMappers( FileMapper[] fileMappers )
+    {
+        this.fileMappers = fileMappers;
+    }
 }
diff --git a/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/UnpackMojo.java b/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/UnpackMojo.java
index 3cc3d66..8f85276 100644
--- a/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/UnpackMojo.java
+++ b/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/UnpackMojo.java
@@ -28,6 +28,7 @@
 import org.apache.maven.plugins.annotations.LifecyclePhase;
 import org.apache.maven.plugins.annotations.Mojo;
 import org.apache.maven.plugins.annotations.Parameter;
+import org.codehaus.plexus.components.io.filemappers.FileMapper;
 import org.codehaus.plexus.util.StringUtils;
 
 import java.io.File;
@@ -70,6 +71,14 @@
     @Parameter( property = "mdep.unpack.excludes" )
     private String excludes;
 
+    /**
+     * {@link FileMapper} to be used for rewriting each target path, or {@code null} if no rewriting shall happen.
+     *
+     * @since 3.1.2
+     */
+    @Parameter( property = "mdep.unpack.filemappers" )
+    private FileMapper[] fileMappers;
+
     /**
      * The artifact to unpack from command line. A string of the form
      * <code>groupId:artifactId:version[:packaging[:classifier]]</code>. Use {@link #artifactItems} within the POM
@@ -126,7 +135,8 @@ private void unpackArtifact( ArtifactItem artifactItem )
         MarkerHandler handler = new UnpackFileMarkerHandler( artifactItem, this.markersDirectory );
 
         unpack( artifactItem.getArtifact(), artifactItem.getType(), artifactItem.getOutputDirectory(),
-                artifactItem.getIncludes(), artifactItem.getExcludes(), artifactItem.getEncoding() );
+                artifactItem.getIncludes(), artifactItem.getExcludes(), artifactItem.getEncoding(),
+                artifactItem.getFileMappers() );
         handler.setMarker();
     }
 
@@ -210,4 +220,26 @@ public void setIncludes( String includes )
     {
         this.includes = includes;
     }
+
+    /**
+     * @return {@link FileMapper}s to be used for rewriting each target path, or {@code null} if no rewriting shall
+     *         happen.
+     *
+     * @since 3.1.2
+     */
+    public FileMapper[] getFileMappers()
+    {
+        return this.fileMappers;
+    }
+
+    /**
+     * @param fileMappers {@link FileMapper}s to be used for rewriting each target path, or {@code null} if no
+     * rewriting shall happen.
+     *
+     * @since 3.1.2
+     */
+    public void setFileMappers( FileMapper[] fileMappers )
+    {
+        this.fileMappers = fileMappers;
+    }
 }
diff --git a/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/UnpackDependenciesMojo.java b/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/UnpackDependenciesMojo.java
index 85c0c27..9c90512 100644
--- a/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/UnpackDependenciesMojo.java
+++ b/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/UnpackDependenciesMojo.java
@@ -30,6 +30,7 @@
 import org.apache.maven.plugins.annotations.Parameter;
 import org.apache.maven.plugins.annotations.ResolutionScope;
 import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter;
+import org.codehaus.plexus.components.io.filemappers.FileMapper;
 
 import java.io.File;
 
@@ -73,6 +74,14 @@
     @Parameter( property = "mdep.unpack.encoding" )
     private String encoding;
 
+    /**
+     * {@link FileMapper}s to be used for rewriting each target path, or {@code null} if no rewriting shall happen.
+     *
+     * @since 3.1.2
+     */
+    @Parameter( property = "mdep.unpack.filemappers" )
+    private FileMapper[] fileMappers;
+
     /**
      * Main entry into mojo. This method gets the dependencies and iterates through each one passing it to
      * DependencyUtil.unpackFile().
@@ -93,7 +102,7 @@ protected void doExecute()
             destDir = DependencyUtil.getFormattedOutputDirectory( useSubDirectoryPerScope, useSubDirectoryPerType,
                                                                   useSubDirectoryPerArtifact, useRepositoryLayout,
                                                                   stripVersion, outputDirectory, artifact );
-            unpack( artifact, destDir, getIncludes(), getExcludes(), getEncoding() );
+            unpack( artifact, destDir, getIncludes(), getExcludes(), getEncoding(), getFileMappers() );
             DefaultFileMarkerHandler handler = new DefaultFileMarkerHandler( artifact, this.markersDirectory );
             handler.setMarker();
         }
@@ -160,4 +169,26 @@ public String getEncoding()
     {
         return this.encoding;
     }
+
+    /**
+     * @return {@link FileMapper}s to be used for rewriting each target path, or {@code null} if no rewriting shall
+     *         happen.
+     *
+     * @since 3.1.2
+     */
+    public FileMapper[] getFileMappers()
+    {
+        return this.fileMappers;
+    }
+
+    /**
+     * @param fileMappers {@link FileMapper}s to be used for rewriting each target path, or {@code null} if no
+     *                   rewriting shall happen.
+     *
+     * @since 3.1.2
+     */
+    public void setFileMappers( FileMapper[] fileMappers )
+    {
+        this.fileMappers = fileMappers;
+    }
 }
diff --git a/src/site/apt/examples/unpacking-filemapper.apt.vm b/src/site/apt/examples/unpacking-filemapper.apt.vm
new file mode 100644
index 0000000..4af6571
--- /dev/null
+++ b/src/site/apt/examples/unpacking-filemapper.apt.vm
@@ -0,0 +1,111 @@
+~~ 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.
+
+  ------
+  Rewriting target path and file name
+  ------
+  Markus KARG
+  ------
+  2018-09-24
+  ------
+
+Rewriting target path and file name
+
+  When unpacking files, it is possible to rewrite each unpacked file's target path and file name within the target folder.
+  This is useful e. g. to add or remove prefixes (like cutting away top level folders etc.):
+
++---+
+<project>
+   [...]
+   <build>
+     <plugins>
+       <plugin>
+         <groupId>org.apache.maven.plugins</groupId>
+         <artifactId>maven-dependency-plugin</artifactId>
+         <version>${project.version}</version>
+         <executions>
+           <execution>
+             <id>unpack</id>
+             <phase>package</phase>
+             <goals>
+               <goal>unpack</goal>
+             </goals>
+             <configuration>
+               [...]
+               <artifactItems>
+                 <artifactItem>
+                   [...]
+                   <fileMappers>
+                     <org.codehaus.plexus.components.io.filemappers.RegExpFileMapper>
+                       <pattern>^\Qx.y/z/\E</pattern>
+                       <replacement>./</replacement>
+                     </org.codehaus.plexus.components.io.filemappers.RegExpFileMapper>
+                   </fileMappers>
+                   [...]
+                 </artifactItem>
+               </artifactItems>
+               [...]
+             </configuration>
+           </execution>
+         </executions>
+       </plugin>
+     </plugins>
+   </build>
+   [...]
+ </project>
++---+
+
+  The above example effectively cuts away the prefix <<<x.y/z/>>> of each unpacked file's path, using a regular
+  expression and a replacement expression. Two tricks need to be used here: First, the regular expression uses <<<\Q>>>
+  and <<<\E>>> to demarcate pattern quoting, as the dot (<<<.>>>) is special character in a reqular expression. Second,
+  due to a restriction of Maven's POM syntax, the replacement cannot be empty (this would skip the file). Instead a
+  dot-slash (<<<./>>>) replacement is used, which is effectively a no-op for paths segments.
+
+  Any Plexus File Mapper (i. e. implementation of the interface
+  <<<org.codehaus.plexus.components.io.filemappers.FileMapper>>>) can be used, but there are three particularly useful.
+
+  The Regular Expression Mapper is useful to modify or cut away prefixes or suffixes, or to rewrite file extensions:
+
++---+
+ [...]
+ <org.codehaus.plexus.components.io.filemappers.RegExpFileMapper>
+   <pattern>[...]</pattern>
+   <replacement>[...]</replacement>
+ </org.codehaus.plexus.components.io.filemappers.RegExpFileMapper>
+ [...]
++---+
+
+  The Prefix File Mapper is useful to add prefixes:
+
++---+
+ [...]
+ <org.codehaus.plexus.components.io.filemappers.PrefixFileMapper>
+   <prefix>[...]</prefix>
+ </org.codehaus.plexus.components.io.filemappers.PrefixFileMapper>
+ [...]
++---+
+
+  The Flatten File Mapper removes the path completey, effectively putting all files in the same folder:
+
++---+
+ [...]
+ <org.codehaus.plexus.components.io.filemappers.FlattenFileMapper/>
+ [...]
++---+
+
+  When multiple file mappers are given, the list gets executed from top to bottom. This might improve readability of
+  the overal rewriting action compared to using a singular (typically complex) regular expression.
diff --git a/src/site/apt/index.apt.vm b/src/site/apt/index.apt.vm
index f61cecc..fc7dca2 100644
--- a/src/site/apt/index.apt.vm
+++ b/src/site/apt/index.apt.vm
@@ -127,6 +127,8 @@ ${project.name}
 
   * {{{./examples/unpacking-project-dependencies.html}Unpacking the Project Dependencies}}
 
+  * {{{./examples/unpacking-filemapper.html}Rewriting target path and file name}}
+
   * {{{./examples/using-dependencies-sources.html}Using Project Dependencies' Sources}}
 
   * {{{./examples/failing-the-build-on-dependency-analysis-warnings.html}Failing the Build on Dependency Analysis Warnings}}
diff --git a/src/site/site.xml b/src/site/site.xml
index a1ac703..7cc2944 100644
--- a/src/site/site.xml
+++ b/src/site/site.xml
@@ -37,6 +37,7 @@ under the License.
       <item name="Copying project dependencies" href="examples/copying-project-dependencies.html"/>
       <item name="Unpacking specific artifacts" href="examples/unpacking-artifacts.html"/>
       <item name="Unpacking the project dependencies" href="examples/unpacking-project-dependencies.html"/>
+      <item name="Rewriting target path and file name" href="examples/unpacking-filemapper.html"/>
       <item name="Using project dependencies' sources" href="examples/using-dependencies-sources.html"/>
       <item name="Failing the build on dependency analysis warnings" href="examples/failing-the-build-on-dependency-analysis-warnings.html"/>
       <item name="Exclude Dependencies from Dependency Analysis" href="examples/exclude-dependencies-from-dependency-analysis.html"/>


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services