Skip to content

Commit

Permalink
MASSEMBLY-617 : add ability to set fileMappers in FileSet in maven as…
Browse files Browse the repository at this point in the history
…sembly plugin
  • Loading branch information
tcollignon committed Jan 1, 2019
1 parent cffdb0c commit edeae79
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,18 @@
*/

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;

/**
*
*/
class PrefixedArchivedFileSet
implements ArchivedFileSet
{
private final static FileMapper[] EMPTY_FILE_MAPPERS_ARRAY = new FileMapper[0];

private final String rootPrefix;

private final ArchivedFileSet fileSet;
Expand Down Expand Up @@ -150,10 +148,10 @@ public InputStreamTransformer getStreamTransformer()
{
return fileSet.getStreamTransformer();
}

@Override
public FileMapper[] getFileMappers()
{
return EMPTY_FILE_MAPPERS_ARRAY;
return fileSet.getFileMappers();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -183,6 +181,6 @@ public InputStreamTransformer getStreamTransformer()
@Override
public FileMapper[] getFileMappers()
{
return EMPTY_FILE_MAPPERS_ARRAY;
return fileSet.getFileMappers();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,6 +57,8 @@ public class AddDirectoryTask
private int directoryMode = -1;

private int fileMode = -1;

private List<org.apache.maven.plugins.assembly.model.FileMapper> fileMappers = new ArrayList<>();

public AddDirectoryTask( final File directory, InputStreamTransformer transformers )
{
Expand Down Expand Up @@ -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 )
Expand Down Expand Up @@ -207,4 +252,9 @@ public void setUseDefaultExcludes( final boolean useDefaultExcludes )
this.useDefaultExcludes = useDefaultExcludes;
}

public void setFileMappers( final List<org.apache.maven.plugins.assembly.model.FileMapper> fileMappers )
{
this.fileMappers = fileMappers;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Expand Down
64 changes: 64 additions & 0 deletions src/main/mdo/assembly-component.mdo
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,20 @@
<defaultValue>false</defaultValue>
<type>boolean</type>
</field>
<field>
<name>fileMappers</name>
<version>2.0.0+</version>
<association>
<type>FileMapper</type>
<multiplicity>*</multiplicity>
</association>
<description>
<![CDATA[
When &lt;exclude&gt; subelements are present, they define a set of
file mappers, which will transform the filename of included elements.
]]>
</description>
</field>
</fields>
</class>
<class>
Expand Down Expand Up @@ -1298,5 +1312,55 @@
</field>
</fields>
</class>
<class>
<name>FileMapper</name>
<version>2.0.0+</version>
<description>
Interface of a component, which may be used to map file names.
</description>
<fields>
<field>
<name>type</name>
<version>2.0.0+</version>
<description>
<![CDATA[
Type of mapper.
Valid values:
<ul>
<li><b>"flatten"</b> - 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</li>
<li><b>"regexp"</b> - 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.</li>
<li><b>"merge"</b> - The target file name will always be the same, as defined by to. from will be ignored.</li>
<li><b>"prefix"</b> - The target file name will be constructed by adding a prefix as defined by to. from will be ignored.</li>
<li><b>"suffix"</b> - The target file name will be constructed by adding a suffix as defined by to. from will be ignored.</li>
</ul>
]]>
</description>
<type>String</type>
<required>true</required>
</field>
<field>
<name>from</name>
<version>2.0.0+</version>
<description>
<![CDATA[
Specifies a type-specific pattern for matching source paths which should be mapped
]]>
</description>
<type>String</type>
<required>false</required>
</field>
<field>
<name>to</name>
<version>2.0.0+</version>
<description>
<![CDATA[
Specifies a type-specific pattern for producing paths based on source paths
]]>
</description>
<type>String</type>
<required>false</required>
</field>
</fields>
</class>
</classes>
</model>
64 changes: 64 additions & 0 deletions src/main/mdo/assembly.mdo
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,20 @@
<defaultValue>false</defaultValue>
<type>boolean</type>
</field>
<field>
<name>fileMappers</name>
<version>2.0.0+</version>
<association>
<type>FileMapper</type>
<multiplicity>*</multiplicity>
</association>
<description>
<![CDATA[
When &lt;exclude&gt; subelements are present, they define a set of
file mappers, which will transform the filename of included elements.
]]>
</description>
</field>
</fields>
</class>
<class>
Expand Down Expand Up @@ -1410,5 +1424,55 @@
</field>
</fields>
</class>
<class>
<name>FileMapper</name>
<version>2.0.0+</version>
<description>
Interface of a component, which may be used to map file names.
</description>
<fields>
<field>
<name>type</name>
<version>2.0.0+</version>
<description>
<![CDATA[
Type of mapper.
Valid values:
<ul>
<li><b>"flatten"</b> - 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</li>
<li><b>"regexp"</b> - 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.</li>
<li><b>"merge"</b> - The target file name will always be the same, as defined by to. from will be ignored.</li>
<li><b>"prefix"</b> - The target file name will be constructed by adding a prefix as defined by to. from will be ignored.</li>
<li><b>"suffix"</b> - The target file name will be constructed by adding a suffix as defined by to. from will be ignored.</li>
</ul>
]]>
</description>
<type>String</type>
<required>true</required>
</field>
<field>
<name>from</name>
<version>2.0.0+</version>
<description>
<![CDATA[
Specifies a type-specific pattern for matching source paths which should be mapped
]]>
</description>
<type>String</type>
<required>false</required>
</field>
<field>
<name>to</name>
<version>2.0.0+</version>
<description>
<![CDATA[
Specifies a type-specific pattern for producing paths based on source paths
]]>
</description>
<type>String</type>
<required>false</required>
</field>
</fields>
</class>
</classes>
</model>

0 comments on commit edeae79

Please sign in to comment.