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>3.6.1</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.