From edeae795495794a2261b1c71a638171357cd7ebd Mon Sep 17 00:00:00 2001 From: Thomas COLLIGNON Date: Thu, 26 Jul 2018 21:51:41 +0200 Subject: [PATCH] MASSEMBLY-617 : add ability to set fileMappers in FileSet in maven assembly plugin --- .../archiver/PrefixedArchivedFileSet.java | 8 +-- .../archive/archiver/PrefixedFileSet.java | 4 +- .../archive/task/AddDirectoryTask.java | 50 +++++++++++++++ .../archive/task/AddFileSetsTask.java | 1 + src/main/mdo/assembly-component.mdo | 64 +++++++++++++++++++ src/main/mdo/assembly.mdo | 64 +++++++++++++++++++ 6 files changed, 183 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/apache/maven/plugins/assembly/archive/archiver/PrefixedArchivedFileSet.java b/src/main/java/org/apache/maven/plugins/assembly/archive/archiver/PrefixedArchivedFileSet.java index 2f1d12e79..b73608f4b 100644 --- a/src/main/java/org/apache/maven/plugins/assembly/archive/archiver/PrefixedArchivedFileSet.java +++ b/src/main/java/org/apache/maven/plugins/assembly/archive/archiver/PrefixedArchivedFileSet.java @@ -20,11 +20,11 @@ */ import org.codehaus.plexus.archiver.ArchivedFileSet; +import org.codehaus.plexus.components.io.filemappers.FileMapper; import org.codehaus.plexus.components.io.fileselectors.FileSelector; import org.codehaus.plexus.components.io.functions.InputStreamTransformer; import java.io.File; -import org.codehaus.plexus.components.io.filemappers.FileMapper; /** * @@ -32,8 +32,6 @@ class PrefixedArchivedFileSet implements ArchivedFileSet { - private final static FileMapper[] EMPTY_FILE_MAPPERS_ARRAY = new FileMapper[0]; - private final String rootPrefix; private final ArchivedFileSet fileSet; @@ -150,10 +148,10 @@ public InputStreamTransformer getStreamTransformer() { return fileSet.getStreamTransformer(); } - + @Override public FileMapper[] getFileMappers() { - return EMPTY_FILE_MAPPERS_ARRAY; + return fileSet.getFileMappers(); } } diff --git a/src/main/java/org/apache/maven/plugins/assembly/archive/archiver/PrefixedFileSet.java b/src/main/java/org/apache/maven/plugins/assembly/archive/archiver/PrefixedFileSet.java index 86d8071dd..162580e1f 100644 --- a/src/main/java/org/apache/maven/plugins/assembly/archive/archiver/PrefixedFileSet.java +++ b/src/main/java/org/apache/maven/plugins/assembly/archive/archiver/PrefixedFileSet.java @@ -32,8 +32,6 @@ class PrefixedFileSet implements FileSet { - private final static FileMapper[] EMPTY_FILE_MAPPERS_ARRAY = new FileMapper[0]; - private final String rootPrefix; private final FileSet fileSet; @@ -183,6 +181,6 @@ public InputStreamTransformer getStreamTransformer() @Override public FileMapper[] getFileMappers() { - return EMPTY_FILE_MAPPERS_ARRAY; + return fileSet.getFileMappers(); } } \ No newline at end of file diff --git a/src/main/java/org/apache/maven/plugins/assembly/archive/task/AddDirectoryTask.java b/src/main/java/org/apache/maven/plugins/assembly/archive/task/AddDirectoryTask.java index 591b50069..e5f2d7df6 100644 --- a/src/main/java/org/apache/maven/plugins/assembly/archive/task/AddDirectoryTask.java +++ b/src/main/java/org/apache/maven/plugins/assembly/archive/task/AddDirectoryTask.java @@ -24,6 +24,12 @@ import org.codehaus.plexus.archiver.Archiver; import org.codehaus.plexus.archiver.ArchiverException; import org.codehaus.plexus.archiver.util.DefaultFileSet; +import org.codehaus.plexus.components.io.filemappers.RegExpFileMapper; +import org.codehaus.plexus.components.io.filemappers.FlattenFileMapper; +import org.codehaus.plexus.components.io.filemappers.MergeFileMapper; +import org.codehaus.plexus.components.io.filemappers.PrefixFileMapper; +import org.codehaus.plexus.components.io.filemappers.SuffixFileMapper; +import org.codehaus.plexus.components.io.filemappers.FileMapper; import org.codehaus.plexus.components.io.functions.InputStreamTransformer; import java.io.File; @@ -51,6 +57,8 @@ public class AddDirectoryTask private int directoryMode = -1; private int fileMode = -1; + + private List fileMappers = new ArrayList<>(); public AddDirectoryTask( final File directory, InputStreamTransformer transformers ) { @@ -144,6 +152,43 @@ else if ( "..".equals( outputDirectory ) ) fs.setStreamTransformer( transformer ); } + FileMapper[] filesMappersArray = new FileMapper[ fileMappers.size() ]; + + int index = 0; + for ( org.apache.maven.plugins.assembly.model.FileMapper fileMapperAssembly : fileMappers ) + { + FileMapper fileMapper = null; + if ( fileMapperAssembly.getType().equals( FlattenFileMapper.ROLE_HINT ) ) + { + fileMapper = new FlattenFileMapper(); + } + else if ( fileMapperAssembly.getType().equals( RegExpFileMapper.ROLE_HINT ) ) + { + fileMapper = new RegExpFileMapper(); + ( (RegExpFileMapper) fileMapper ).setPattern( fileMapperAssembly.getFrom() ); + ( (RegExpFileMapper) fileMapper ).setReplacement( fileMapperAssembly.getTo() ); + } + else if ( fileMapperAssembly.getType().equals( MergeFileMapper.ROLE_HINT ) ) + { + fileMapper = new MergeFileMapper(); + ( (MergeFileMapper) fileMapper ).setTargetName( fileMapperAssembly.getTo() ); + } + else if ( fileMapperAssembly.getType().equals( PrefixFileMapper.ROLE_HINT ) ) + { + fileMapper = new PrefixFileMapper(); + ( (PrefixFileMapper) fileMapper ).setPrefix( fileMapperAssembly.getTo() ); + } + else if ( fileMapperAssembly.getType().equals( SuffixFileMapper.ROLE_HINT ) ) + { + fileMapper = new SuffixFileMapper(); + ( (SuffixFileMapper) fileMapper ).setSuffix( fileMapperAssembly.getTo() ); + } + filesMappersArray[index] = fileMapper; + index++; + } + + fs.setFileMappers( filesMappersArray ); + archiver.addFileSet( fs ); } catch ( final ArchiverException e ) @@ -207,4 +252,9 @@ public void setUseDefaultExcludes( final boolean useDefaultExcludes ) this.useDefaultExcludes = useDefaultExcludes; } + public void setFileMappers( final List fileMappers ) + { + this.fileMappers = fileMappers; + } + } diff --git a/src/main/java/org/apache/maven/plugins/assembly/archive/task/AddFileSetsTask.java b/src/main/java/org/apache/maven/plugins/assembly/archive/task/AddFileSetsTask.java index 933f3e5ae..d69d2e7b0 100644 --- a/src/main/java/org/apache/maven/plugins/assembly/archive/task/AddFileSetsTask.java +++ b/src/main/java/org/apache/maven/plugins/assembly/archive/task/AddFileSetsTask.java @@ -165,6 +165,7 @@ void addFileSet( final FileSet fileSet, final Archiver archiver, final Assembler task.setExcludes( fileSet.getExcludes() ); task.setIncludes( fileSet.getIncludes() ); task.setOutputDirectory( destDirectory ); + task.setFileMappers( fileSet.getFileMappers() ); task.execute( archiver ); } diff --git a/src/main/mdo/assembly-component.mdo b/src/main/mdo/assembly-component.mdo index 18ae461c9..d91bc751b 100644 --- a/src/main/mdo/assembly-component.mdo +++ b/src/main/mdo/assembly-component.mdo @@ -283,6 +283,20 @@ false boolean + + fileMappers + 2.0.0+ + + FileMapper + * + + + + + @@ -1298,5 +1312,55 @@ + + FileMapper + 2.0.0+ + + Interface of a component, which may be used to map file names. + + + + type + 2.0.0+ + + +
  • "flatten" - The target file name is identical to the source file name, with all leading directory information stripped off. Both to and from will be ignored
  • +
  • "regexp" - Both to and from define regular expressions. If the source file name matches the from pattern, the target file name will be constructed from the to pattern, using \0 to \9 as back-references for the full match (\0) or the matches of the subexpressions in parentheses. Source files not matching the from pattern will be ignored.
  • +
  • "merge" - The target file name will always be the same, as defined by to. from will be ignored.
  • +
  • "prefix" - The target file name will be constructed by adding a prefix as defined by to. from will be ignored.
  • +
  • "suffix" - The target file name will be constructed by adding a suffix as defined by to. from will be ignored.
  • + + ]]> +
    + String + true +
    + + from + 2.0.0+ + + + + String + false + + + to + 2.0.0+ + + + + String + false + +
    +
    diff --git a/src/main/mdo/assembly.mdo b/src/main/mdo/assembly.mdo index b8d584d49..19cb1b721 100644 --- a/src/main/mdo/assembly.mdo +++ b/src/main/mdo/assembly.mdo @@ -403,6 +403,20 @@ false boolean + + fileMappers + 2.0.0+ + + FileMapper + * + + + + + @@ -1410,5 +1424,55 @@ + + FileMapper + 2.0.0+ + + Interface of a component, which may be used to map file names. + + + + type + 2.0.0+ + + +
  • "flatten" - The target file name is identical to the source file name, with all leading directory information stripped off. Both to and from will be ignored
  • +
  • "regexp" - Both to and from define regular expressions. If the source file name matches the from pattern, the target file name will be constructed from the to pattern, using \0 to \9 as back-references for the full match (\0) or the matches of the subexpressions in parentheses. Source files not matching the from pattern will be ignored.
  • +
  • "merge" - The target file name will always be the same, as defined by to. from will be ignored.
  • +
  • "prefix" - The target file name will be constructed by adding a prefix as defined by to. from will be ignored.
  • +
  • "suffix" - The target file name will be constructed by adding a suffix as defined by to. from will be ignored.
  • + + ]]> +
    + String + true +
    + + from + 2.0.0+ + + + + String + false + + + to + 2.0.0+ + + + + String + false + +
    +