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
tcollignon committed Jan 1, 2019
1 parent 42ff8bc commit 1c66df9
Showing 6 changed files with 187 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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;
@@ -150,5 +148,10 @@ public InputStreamTransformer getStreamTransformer()
{
return fileSet.getStreamTransformer();
}


@Override
public FileMapper[] getFileMappers()
{
return fileSet.getFileMappers();
}
}
Original file line number Diff line number Diff line change
@@ -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;
@@ -179,6 +177,7 @@ public InputStreamTransformer getStreamTransformer()
{
return fileSet.getStreamTransformer();
}

@Override
public FileMapper[] getFileMappers()
{
Original file line number Diff line number Diff line change
@@ -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<org.apache.maven.plugins.assembly.model.FileMapper> 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<org.apache.maven.plugins.assembly.model.FileMapper> fileMappers )
{
this.fileMappers = fileMappers;
}

}
Original file line number Diff line number Diff line change
@@ -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 );
}
64 changes: 64 additions & 0 deletions src/main/mdo/assembly-component.mdo
Original file line number Diff line number Diff line change
@@ -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>
@@ -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
@@ -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>
@@ -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 1c66df9

Please sign in to comment.