1 | |
package org.apache.maven.plugin.assembly.archive.task; |
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.artifact.Artifact; |
23 | |
import org.apache.maven.plugin.assembly.AssemblerConfigurationSource; |
24 | |
import org.apache.maven.plugin.assembly.archive.ArchiveCreationException; |
25 | |
import org.apache.maven.plugin.assembly.format.AssemblyFormattingException; |
26 | |
import org.apache.maven.plugin.assembly.utils.AssemblyFormatUtils; |
27 | |
import org.apache.maven.plugin.assembly.utils.TypeConversionUtils; |
28 | |
import org.apache.maven.project.MavenProject; |
29 | |
import org.codehaus.plexus.archiver.Archiver; |
30 | |
import org.codehaus.plexus.archiver.ArchiverException; |
31 | |
import org.codehaus.plexus.logging.Logger; |
32 | |
import org.codehaus.plexus.util.FileUtils; |
33 | |
import org.codehaus.plexus.util.StringUtils; |
34 | |
|
35 | |
import java.io.File; |
36 | |
import java.io.IOException; |
37 | |
import java.util.List; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
public class AddArtifactTask |
43 | |
implements ArchiverTask |
44 | |
{ |
45 | |
|
46 | 1 | public static final String[] DEFAULT_INCLUDES_ARRAY = { "**/*" }; |
47 | |
|
48 | 18 | private int directoryMode = -1; |
49 | |
|
50 | 18 | private int fileMode = -1; |
51 | |
|
52 | 18 | private boolean unpack = false; |
53 | |
|
54 | |
private List<String> includes; |
55 | |
|
56 | |
private List<String> excludes; |
57 | |
|
58 | |
private final Artifact artifact; |
59 | |
|
60 | |
private MavenProject project; |
61 | |
|
62 | |
private MavenProject moduleProject; |
63 | |
|
64 | |
private Artifact moduleArtifact; |
65 | |
|
66 | |
private String outputDirectory; |
67 | |
|
68 | |
private String outputFileNameMapping; |
69 | |
|
70 | |
private final Logger logger; |
71 | |
|
72 | |
public AddArtifactTask( final Artifact artifact, final Logger logger ) |
73 | 18 | { |
74 | 18 | this.artifact = artifact; |
75 | 18 | this.logger = logger; |
76 | 18 | } |
77 | |
|
78 | |
public void execute( final Archiver archiver, final AssemblerConfigurationSource configSource ) |
79 | |
throws ArchiveCreationException, AssemblyFormattingException |
80 | |
{ |
81 | |
|
82 | |
|
83 | 18 | if ( ( ( artifact.getFile() != null ) && ( archiver.getDestFile() != null ) ) |
84 | |
&& artifact.getFile() |
85 | |
.equals( archiver.getDestFile() ) ) |
86 | |
{ |
87 | 0 | final File tempRoot = configSource.getTemporaryRootDirectory(); |
88 | 0 | final File tempArtifactFile = new File( tempRoot, artifact.getFile() |
89 | |
.getName() ); |
90 | |
|
91 | 0 | logger.warn( "Artifact: " |
92 | |
+ artifact.getId() |
93 | |
+ " references the same file as the assembly destination file. Moving it to a temporary location for inclusion." ); |
94 | |
try |
95 | |
{ |
96 | 0 | FileUtils.copyFile( artifact.getFile(), tempArtifactFile ); |
97 | |
} |
98 | 0 | catch ( final IOException e ) |
99 | |
{ |
100 | 0 | throw new ArchiveCreationException( "Error moving artifact file: '" + artifact.getFile() |
101 | |
+ "' to temporary location: " + tempArtifactFile + ". Reason: " + e.getMessage(), e ); |
102 | 0 | } |
103 | |
|
104 | 0 | artifact.setFile( tempArtifactFile ); |
105 | |
} |
106 | |
|
107 | 18 | String destDirectory = outputDirectory; |
108 | |
|
109 | 18 | destDirectory = |
110 | |
AssemblyFormatUtils.getOutputDirectory( destDirectory, configSource.getProject(), moduleProject, project, |
111 | |
configSource.getFinalName(), configSource ); |
112 | |
|
113 | 18 | if ( unpack ) |
114 | |
{ |
115 | 4 | String outputLocation = destDirectory; |
116 | |
|
117 | 4 | if ( ( outputLocation.length() > 0 ) && !outputLocation.endsWith( "/" ) ) |
118 | |
{ |
119 | 0 | outputLocation += "/"; |
120 | |
} |
121 | |
|
122 | 4 | String[] includesArray = TypeConversionUtils.toStringArray( includes ); |
123 | 4 | if ( includesArray == null ) |
124 | |
{ |
125 | 3 | includesArray = DEFAULT_INCLUDES_ARRAY; |
126 | |
} |
127 | 4 | final String[] excludesArray = TypeConversionUtils.toStringArray( excludes ); |
128 | |
|
129 | 4 | final int oldDirMode = archiver.getOverrideDirectoryMode(); |
130 | 4 | final int oldFileMode = archiver.getOverrideFileMode(); |
131 | |
|
132 | 4 | logger.debug( "Unpacking artifact: " + artifact.getId() + " to assembly location: " + outputLocation + "." ); |
133 | |
|
134 | 4 | boolean fileModeSet = false; |
135 | 4 | boolean dirModeSet = false; |
136 | |
|
137 | |
try |
138 | |
{ |
139 | 4 | if ( fileMode != -1 ) |
140 | |
{ |
141 | 2 | archiver.setFileMode( fileMode ); |
142 | 2 | fileModeSet = true; |
143 | |
} |
144 | |
|
145 | 4 | if ( directoryMode != -1 ) |
146 | |
{ |
147 | 2 | archiver.setDirectoryMode( directoryMode ); |
148 | 2 | dirModeSet = true; |
149 | |
} |
150 | |
|
151 | 4 | final File artifactFile = artifact.getFile(); |
152 | 4 | if ( artifactFile == null ) |
153 | |
{ |
154 | 0 | logger.warn( "Skipping artifact: " + artifact.getId() |
155 | |
+ "; it does not have an associated file or directory." ); |
156 | |
} |
157 | 4 | else if ( artifactFile.isDirectory() ) |
158 | |
{ |
159 | 0 | logger.debug( "Adding artifact directory contents for: " + artifact + " to: " + outputLocation ); |
160 | 0 | archiver.addDirectory( artifactFile, outputLocation, includesArray, excludesArray ); |
161 | |
} |
162 | |
else |
163 | |
{ |
164 | 4 | logger.debug( "Unpacking artifact contents for: " + artifact + " to: " + outputLocation ); |
165 | 4 | logger.debug( "includes:\n" + StringUtils.join( includesArray, "\n" ) + "\n" ); |
166 | 4 | logger.debug( "excludes:\n" |
167 | |
+ ( excludesArray == null ? "none" : StringUtils.join( excludesArray, "\n" ) ) |
168 | |
+ "\n" ); |
169 | 4 | archiver.addArchivedFileSet( artifactFile, outputLocation, includesArray, excludesArray ); |
170 | |
} |
171 | |
} |
172 | 0 | catch ( final ArchiverException e ) |
173 | |
{ |
174 | 0 | throw new ArchiveCreationException( "Error adding file-set for '" + artifact.getId() + "' to archive: " |
175 | |
+ e.getMessage(), e ); |
176 | |
} |
177 | |
finally |
178 | |
{ |
179 | 4 | if ( dirModeSet ) |
180 | |
{ |
181 | 2 | archiver.setDirectoryMode( oldDirMode ); |
182 | |
} |
183 | |
|
184 | 4 | if ( fileModeSet ) |
185 | |
{ |
186 | 2 | archiver.setFileMode( oldFileMode ); |
187 | |
} |
188 | |
} |
189 | 4 | } |
190 | |
else |
191 | |
{ |
192 | 14 | final String fileNameMapping = |
193 | |
AssemblyFormatUtils.evaluateFileNameMapping( outputFileNameMapping, artifact, |
194 | |
configSource.getProject(), moduleProject, moduleArtifact, |
195 | |
project, configSource ); |
196 | |
|
197 | 14 | final String outputLocation = destDirectory + fileNameMapping; |
198 | |
|
199 | |
try |
200 | |
{ |
201 | 14 | final File artifactFile = artifact.getFile(); |
202 | |
|
203 | 14 | logger.debug( "Adding artifact: " + artifact.getId() + " with file: " + artifactFile |
204 | |
+ " to assembly location: " + outputLocation + "." ); |
205 | |
|
206 | 14 | if ( fileMode != -1 ) |
207 | |
{ |
208 | 11 | archiver.addFile( artifactFile, outputLocation, fileMode ); |
209 | |
} |
210 | |
else |
211 | |
{ |
212 | 3 | archiver.addFile( artifactFile, outputLocation ); |
213 | |
} |
214 | |
} |
215 | 0 | catch ( final ArchiverException e ) |
216 | |
{ |
217 | 0 | throw new ArchiveCreationException( "Error adding file '" + artifact.getId() + "' to archive: " |
218 | |
+ e.getMessage(), e ); |
219 | 14 | } |
220 | |
} |
221 | 18 | } |
222 | |
|
223 | |
public void setDirectoryMode( final int directoryMode ) |
224 | |
{ |
225 | 4 | this.directoryMode = directoryMode; |
226 | 4 | } |
227 | |
|
228 | |
public void setFileMode( final int fileMode ) |
229 | |
{ |
230 | 13 | this.fileMode = fileMode; |
231 | 13 | } |
232 | |
|
233 | |
public void setExcludes( final List<String> excludes ) |
234 | |
{ |
235 | 1 | this.excludes = excludes; |
236 | 1 | } |
237 | |
|
238 | |
public void setIncludes( final List<String> includes ) |
239 | |
{ |
240 | 1 | this.includes = includes; |
241 | 1 | } |
242 | |
|
243 | |
public void setUnpack( final boolean unpack ) |
244 | |
{ |
245 | 16 | this.unpack = unpack; |
246 | 16 | } |
247 | |
|
248 | |
public void setProject( final MavenProject project ) |
249 | |
{ |
250 | 14 | this.project = project; |
251 | 14 | } |
252 | |
|
253 | |
public void setOutputDirectory( final String outputDirectory ) |
254 | |
{ |
255 | 14 | this.outputDirectory = outputDirectory; |
256 | 14 | } |
257 | |
|
258 | |
public void setFileNameMapping( final String outputFileNameMapping ) |
259 | |
{ |
260 | 18 | this.outputFileNameMapping = outputFileNameMapping; |
261 | 18 | } |
262 | |
|
263 | |
public void setOutputDirectory( final String outputDirectory, final String defaultOutputDirectory ) |
264 | |
{ |
265 | 7 | setOutputDirectory( outputDirectory == null ? defaultOutputDirectory : outputDirectory ); |
266 | 7 | } |
267 | |
|
268 | |
public void setFileNameMapping( final String outputFileNameMapping, final String defaultOutputFileNameMapping ) |
269 | |
{ |
270 | 7 | setFileNameMapping( outputFileNameMapping == null ? defaultOutputFileNameMapping : outputFileNameMapping ); |
271 | 7 | } |
272 | |
|
273 | |
public MavenProject getModuleProject() |
274 | |
{ |
275 | 0 | return moduleProject; |
276 | |
} |
277 | |
|
278 | |
public void setModuleProject( final MavenProject moduleProject ) |
279 | |
{ |
280 | 13 | this.moduleProject = moduleProject; |
281 | 13 | } |
282 | |
|
283 | |
public Artifact getModuleArtifact() |
284 | |
{ |
285 | 0 | return moduleArtifact; |
286 | |
} |
287 | |
|
288 | |
public void setModuleArtifact( final Artifact moduleArtifact ) |
289 | |
{ |
290 | 13 | this.moduleArtifact = moduleArtifact; |
291 | 13 | } |
292 | |
|
293 | |
} |