1 package org.apache.maven.plugin.assembly.archive.phase;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.plugin.assembly.AssemblerConfigurationSource;
23 import org.apache.maven.plugin.assembly.archive.ArchiveCreationException;
24 import org.apache.maven.plugin.assembly.format.AssemblyFormattingException;
25 import org.apache.maven.plugin.assembly.format.ReaderFormatter;
26 import org.apache.maven.plugin.assembly.model.Assembly;
27 import org.apache.maven.plugin.assembly.model.FileItem;
28 import org.apache.maven.plugin.assembly.utils.AssemblyFormatUtils;
29 import org.apache.maven.plugin.assembly.utils.TypeConversionUtils;
30 import org.codehaus.plexus.archiver.Archiver;
31 import org.codehaus.plexus.archiver.ArchiverException;
32 import org.codehaus.plexus.component.annotations.Component;
33 import org.codehaus.plexus.components.io.functions.InputStreamTransformer;
34 import org.codehaus.plexus.components.io.resources.PlexusIoResource;
35 import org.codehaus.plexus.logging.AbstractLogEnabled;
36
37 import java.io.File;
38 import java.io.IOException;
39 import java.util.List;
40
41 import static org.apache.maven.plugin.assembly.utils.AssemblyFormatUtils.warnForPlatformSpecifics;
42 import static org.codehaus.plexus.components.io.resources.ResourceFactory.createResource;
43
44
45
46
47
48
49 @Component( role = AssemblyArchiverPhase.class, hint = "file-items" )
50 public class FileItemAssemblyPhase
51 extends AbstractLogEnabled
52 implements AssemblyArchiverPhase, PhaseOrder
53 {
54
55
56
57
58 public void execute( final Assembly assembly, final Archiver archiver,
59 final AssemblerConfigurationSource configSource )
60 throws ArchiveCreationException, AssemblyFormattingException
61 {
62 final List<FileItem> fileList = assembly.getFiles();
63 final File basedir = configSource.getBasedir();
64
65 for ( final FileItem fileItem : fileList )
66 {
67 final String sourcePath = fileItem.getSource();
68
69
70 File source = new File( sourcePath );
71
72
73
74 final String sourceName = source.getName();
75
76 if ( !source.isAbsolute() )
77 {
78 source = new File( basedir, sourcePath );
79 }
80
81 String destName = fileItem.getDestName();
82
83 if ( destName == null )
84 {
85 destName = sourceName;
86 }
87
88 final String outputDirectory1 = fileItem.getOutputDirectory();
89
90 warnForPlatformSpecifics( getLogger(), outputDirectory1 );
91
92 final String outputDirectory =
93 AssemblyFormatUtils.getOutputDirectory( outputDirectory1, configSource.getFinalName(), configSource,
94 AssemblyFormatUtils.moduleProjectInterpolator(
95 configSource.getProject() ),
96 AssemblyFormatUtils.artifactProjectInterpolator( null ) );
97
98 String target;
99
100
101 if ( outputDirectory.endsWith( "/" ) || outputDirectory.endsWith( "\\" ) )
102 {
103 target = outputDirectory + destName;
104 }
105 else if ( outputDirectory.length() < 1 )
106 {
107 target = destName;
108 }
109 else
110 {
111 target = outputDirectory + "/" + destName;
112 }
113
114 try
115 {
116 final InputStreamTransformer fileSetTransformers =
117 ReaderFormatter.getFileSetTransformers( configSource, fileItem.isFiltered(),
118 fileItem.getLineEnding() );
119
120 final PlexusIoResource restoUse = createResource( source, fileSetTransformers );
121
122 int mode = TypeConversionUtils.modeToInt( fileItem.getFileMode(), getLogger() );
123 archiver.addResource( restoUse, target, mode );
124 }
125 catch ( final ArchiverException e )
126 {
127 throw new ArchiveCreationException( "Error adding file to archive: " + e.getMessage(), e );
128 }
129 catch ( IOException e )
130 {
131 throw new ArchiveCreationException( "Error adding file to archive: " + e.getMessage(), e );
132 }
133 }
134 }
135
136 public int order()
137 {
138 return 10;
139 }
140 }