Skip to content

Commit edeae79

Browse files
committed
MASSEMBLY-617 : add ability to set fileMappers in FileSet in maven assembly plugin
1 parent cffdb0c commit edeae79

File tree

6 files changed

+183
-8
lines changed

6 files changed

+183
-8
lines changed

src/main/java/org/apache/maven/plugins/assembly/archive/archiver/PrefixedArchivedFileSet.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,18 @@
2020
*/
2121

2222
import org.codehaus.plexus.archiver.ArchivedFileSet;
23+
import org.codehaus.plexus.components.io.filemappers.FileMapper;
2324
import org.codehaus.plexus.components.io.fileselectors.FileSelector;
2425
import org.codehaus.plexus.components.io.functions.InputStreamTransformer;
2526

2627
import java.io.File;
27-
import org.codehaus.plexus.components.io.filemappers.FileMapper;
2828

2929
/**
3030
*
3131
*/
3232
class PrefixedArchivedFileSet
3333
implements ArchivedFileSet
3434
{
35-
private final static FileMapper[] EMPTY_FILE_MAPPERS_ARRAY = new FileMapper[0];
36-
3735
private final String rootPrefix;
3836

3937
private final ArchivedFileSet fileSet;
@@ -150,10 +148,10 @@ public InputStreamTransformer getStreamTransformer()
150148
{
151149
return fileSet.getStreamTransformer();
152150
}
153-
151+
154152
@Override
155153
public FileMapper[] getFileMappers()
156154
{
157-
return EMPTY_FILE_MAPPERS_ARRAY;
155+
return fileSet.getFileMappers();
158156
}
159157
}

src/main/java/org/apache/maven/plugins/assembly/archive/archiver/PrefixedFileSet.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232
class PrefixedFileSet
3333
implements FileSet
3434
{
35-
private final static FileMapper[] EMPTY_FILE_MAPPERS_ARRAY = new FileMapper[0];
36-
3735
private final String rootPrefix;
3836

3937
private final FileSet fileSet;
@@ -183,6 +181,6 @@ public InputStreamTransformer getStreamTransformer()
183181
@Override
184182
public FileMapper[] getFileMappers()
185183
{
186-
return EMPTY_FILE_MAPPERS_ARRAY;
184+
return fileSet.getFileMappers();
187185
}
188186
}

src/main/java/org/apache/maven/plugins/assembly/archive/task/AddDirectoryTask.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
import org.codehaus.plexus.archiver.Archiver;
2525
import org.codehaus.plexus.archiver.ArchiverException;
2626
import org.codehaus.plexus.archiver.util.DefaultFileSet;
27+
import org.codehaus.plexus.components.io.filemappers.RegExpFileMapper;
28+
import org.codehaus.plexus.components.io.filemappers.FlattenFileMapper;
29+
import org.codehaus.plexus.components.io.filemappers.MergeFileMapper;
30+
import org.codehaus.plexus.components.io.filemappers.PrefixFileMapper;
31+
import org.codehaus.plexus.components.io.filemappers.SuffixFileMapper;
32+
import org.codehaus.plexus.components.io.filemappers.FileMapper;
2733
import org.codehaus.plexus.components.io.functions.InputStreamTransformer;
2834

2935
import java.io.File;
@@ -51,6 +57,8 @@ public class AddDirectoryTask
5157
private int directoryMode = -1;
5258

5359
private int fileMode = -1;
60+
61+
private List<org.apache.maven.plugins.assembly.model.FileMapper> fileMappers = new ArrayList<>();
5462

5563
public AddDirectoryTask( final File directory, InputStreamTransformer transformers )
5664
{
@@ -144,6 +152,43 @@ else if ( "..".equals( outputDirectory ) )
144152
fs.setStreamTransformer( transformer );
145153
}
146154

155+
FileMapper[] filesMappersArray = new FileMapper[ fileMappers.size() ];
156+
157+
int index = 0;
158+
for ( org.apache.maven.plugins.assembly.model.FileMapper fileMapperAssembly : fileMappers )
159+
{
160+
FileMapper fileMapper = null;
161+
if ( fileMapperAssembly.getType().equals( FlattenFileMapper.ROLE_HINT ) )
162+
{
163+
fileMapper = new FlattenFileMapper();
164+
}
165+
else if ( fileMapperAssembly.getType().equals( RegExpFileMapper.ROLE_HINT ) )
166+
{
167+
fileMapper = new RegExpFileMapper();
168+
( (RegExpFileMapper) fileMapper ).setPattern( fileMapperAssembly.getFrom() );
169+
( (RegExpFileMapper) fileMapper ).setReplacement( fileMapperAssembly.getTo() );
170+
}
171+
else if ( fileMapperAssembly.getType().equals( MergeFileMapper.ROLE_HINT ) )
172+
{
173+
fileMapper = new MergeFileMapper();
174+
( (MergeFileMapper) fileMapper ).setTargetName( fileMapperAssembly.getTo() );
175+
}
176+
else if ( fileMapperAssembly.getType().equals( PrefixFileMapper.ROLE_HINT ) )
177+
{
178+
fileMapper = new PrefixFileMapper();
179+
( (PrefixFileMapper) fileMapper ).setPrefix( fileMapperAssembly.getTo() );
180+
}
181+
else if ( fileMapperAssembly.getType().equals( SuffixFileMapper.ROLE_HINT ) )
182+
{
183+
fileMapper = new SuffixFileMapper();
184+
( (SuffixFileMapper) fileMapper ).setSuffix( fileMapperAssembly.getTo() );
185+
}
186+
filesMappersArray[index] = fileMapper;
187+
index++;
188+
}
189+
190+
fs.setFileMappers( filesMappersArray );
191+
147192
archiver.addFileSet( fs );
148193
}
149194
catch ( final ArchiverException e )
@@ -207,4 +252,9 @@ public void setUseDefaultExcludes( final boolean useDefaultExcludes )
207252
this.useDefaultExcludes = useDefaultExcludes;
208253
}
209254

255+
public void setFileMappers( final List<org.apache.maven.plugins.assembly.model.FileMapper> fileMappers )
256+
{
257+
this.fileMappers = fileMappers;
258+
}
259+
210260
}

src/main/java/org/apache/maven/plugins/assembly/archive/task/AddFileSetsTask.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ void addFileSet( final FileSet fileSet, final Archiver archiver, final Assembler
165165
task.setExcludes( fileSet.getExcludes() );
166166
task.setIncludes( fileSet.getIncludes() );
167167
task.setOutputDirectory( destDirectory );
168+
task.setFileMappers( fileSet.getFileMappers() );
168169

169170
task.execute( archiver );
170171
}

src/main/mdo/assembly-component.mdo

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,20 @@
283283
<defaultValue>false</defaultValue>
284284
<type>boolean</type>
285285
</field>
286+
<field>
287+
<name>fileMappers</name>
288+
<version>2.0.0+</version>
289+
<association>
290+
<type>FileMapper</type>
291+
<multiplicity>*</multiplicity>
292+
</association>
293+
<description>
294+
<![CDATA[
295+
When &lt;exclude&gt; subelements are present, they define a set of
296+
file mappers, which will transform the filename of included elements.
297+
]]>
298+
</description>
299+
</field>
286300
</fields>
287301
</class>
288302
<class>
@@ -1298,5 +1312,55 @@
12981312
</field>
12991313
</fields>
13001314
</class>
1315+
<class>
1316+
<name>FileMapper</name>
1317+
<version>2.0.0+</version>
1318+
<description>
1319+
Interface of a component, which may be used to map file names.
1320+
</description>
1321+
<fields>
1322+
<field>
1323+
<name>type</name>
1324+
<version>2.0.0+</version>
1325+
<description>
1326+
<![CDATA[
1327+
Type of mapper.
1328+
Valid values:
1329+
<ul>
1330+
<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>
1331+
<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>
1332+
<li><b>"merge"</b> - The target file name will always be the same, as defined by to. from will be ignored.</li>
1333+
<li><b>"prefix"</b> - The target file name will be constructed by adding a prefix as defined by to. from will be ignored.</li>
1334+
<li><b>"suffix"</b> - The target file name will be constructed by adding a suffix as defined by to. from will be ignored.</li>
1335+
</ul>
1336+
]]>
1337+
</description>
1338+
<type>String</type>
1339+
<required>true</required>
1340+
</field>
1341+
<field>
1342+
<name>from</name>
1343+
<version>2.0.0+</version>
1344+
<description>
1345+
<![CDATA[
1346+
Specifies a type-specific pattern for matching source paths which should be mapped
1347+
]]>
1348+
</description>
1349+
<type>String</type>
1350+
<required>false</required>
1351+
</field>
1352+
<field>
1353+
<name>to</name>
1354+
<version>2.0.0+</version>
1355+
<description>
1356+
<![CDATA[
1357+
Specifies a type-specific pattern for producing paths based on source paths
1358+
]]>
1359+
</description>
1360+
<type>String</type>
1361+
<required>false</required>
1362+
</field>
1363+
</fields>
1364+
</class>
13011365
</classes>
13021366
</model>

src/main/mdo/assembly.mdo

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,20 @@
403403
<defaultValue>false</defaultValue>
404404
<type>boolean</type>
405405
</field>
406+
<field>
407+
<name>fileMappers</name>
408+
<version>2.0.0+</version>
409+
<association>
410+
<type>FileMapper</type>
411+
<multiplicity>*</multiplicity>
412+
</association>
413+
<description>
414+
<![CDATA[
415+
When &lt;exclude&gt; subelements are present, they define a set of
416+
file mappers, which will transform the filename of included elements.
417+
]]>
418+
</description>
419+
</field>
406420
</fields>
407421
</class>
408422
<class>
@@ -1410,5 +1424,55 @@
14101424
</field>
14111425
</fields>
14121426
</class>
1427+
<class>
1428+
<name>FileMapper</name>
1429+
<version>2.0.0+</version>
1430+
<description>
1431+
Interface of a component, which may be used to map file names.
1432+
</description>
1433+
<fields>
1434+
<field>
1435+
<name>type</name>
1436+
<version>2.0.0+</version>
1437+
<description>
1438+
<![CDATA[
1439+
Type of mapper.
1440+
Valid values:
1441+
<ul>
1442+
<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>
1443+
<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>
1444+
<li><b>"merge"</b> - The target file name will always be the same, as defined by to. from will be ignored.</li>
1445+
<li><b>"prefix"</b> - The target file name will be constructed by adding a prefix as defined by to. from will be ignored.</li>
1446+
<li><b>"suffix"</b> - The target file name will be constructed by adding a suffix as defined by to. from will be ignored.</li>
1447+
</ul>
1448+
]]>
1449+
</description>
1450+
<type>String</type>
1451+
<required>true</required>
1452+
</field>
1453+
<field>
1454+
<name>from</name>
1455+
<version>2.0.0+</version>
1456+
<description>
1457+
<![CDATA[
1458+
Specifies a type-specific pattern for matching source paths which should be mapped
1459+
]]>
1460+
</description>
1461+
<type>String</type>
1462+
<required>false</required>
1463+
</field>
1464+
<field>
1465+
<name>to</name>
1466+
<version>2.0.0+</version>
1467+
<description>
1468+
<![CDATA[
1469+
Specifies a type-specific pattern for producing paths based on source paths
1470+
]]>
1471+
</description>
1472+
<type>String</type>
1473+
<required>false</required>
1474+
</field>
1475+
</fields>
1476+
</class>
14131477
</classes>
14141478
</model>

0 commit comments

Comments
 (0)