1 package org.apache.maven.plugins.dependency;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.lang.reflect.Field;
25 import java.util.List;
26
27 import org.apache.maven.artifact.Artifact;
28 import org.apache.maven.artifact.repository.ArtifactRepository;
29 import org.apache.maven.execution.MavenSession;
30 import org.apache.maven.plugin.AbstractMojo;
31 import org.apache.maven.plugin.MojoExecutionException;
32 import org.apache.maven.plugin.MojoFailureException;
33 import org.apache.maven.plugins.annotations.Component;
34 import org.apache.maven.plugins.annotations.Parameter;
35 import org.apache.maven.plugins.dependency.utils.DependencySilentLog;
36 import org.apache.maven.project.DefaultProjectBuildingRequest;
37 import org.apache.maven.project.MavenProject;
38 import org.apache.maven.project.ProjectBuildingRequest;
39 import org.codehaus.plexus.archiver.ArchiverException;
40 import org.codehaus.plexus.archiver.UnArchiver;
41 import org.codehaus.plexus.archiver.manager.ArchiverManager;
42 import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
43 import org.codehaus.plexus.archiver.zip.ZipUnArchiver;
44 import org.codehaus.plexus.components.io.filemappers.FileMapper;
45 import org.codehaus.plexus.components.io.fileselectors.IncludeExcludeFileSelector;
46 import org.codehaus.plexus.util.FileUtils;
47 import org.codehaus.plexus.util.ReflectionUtils;
48 import org.codehaus.plexus.util.StringUtils;
49
50
51
52
53 public abstract class AbstractDependencyMojo
54 extends AbstractMojo
55 {
56
57
58
59 @Component
60 private ArchiverManager archiverManager;
61
62
63
64
65
66
67
68
69
70 @Parameter( property = "dependency.useJvmChmod", defaultValue = "true" )
71 private boolean useJvmChmod = true;
72
73
74
75
76
77
78 @Parameter( property = "dependency.ignorePermissions", defaultValue = "false" )
79 private boolean ignorePermissions;
80
81
82
83
84 @Parameter( defaultValue = "${project}", readonly = true, required = true )
85 private MavenProject project;
86
87
88
89
90 @Parameter( defaultValue = "${project.remoteArtifactRepositories}", readonly = true, required = true )
91 private List<ArtifactRepository> remoteRepositories;
92
93
94
95
96 @Parameter( defaultValue = "${reactorProjects}", readonly = true )
97 protected List<MavenProject> reactorProjects;
98
99
100
101
102 @Parameter( defaultValue = "${session}", readonly = true, required = true )
103 protected MavenSession session;
104
105
106
107
108
109
110 @Parameter( property = "silent", defaultValue = "false" )
111 private boolean silent;
112
113
114
115
116
117
118 @Parameter( property = "outputAbsoluteArtifactFilename", defaultValue = "false" )
119 protected boolean outputAbsoluteArtifactFilename;
120
121
122
123
124
125
126 @Parameter( property = "mdep.skip", defaultValue = "false" )
127 private boolean skip;
128
129
130
131
132
133
134 @Override
135 public final void execute()
136 throws MojoExecutionException, MojoFailureException
137 {
138 if ( isSkip() )
139 {
140 getLog().info( "Skipping plugin execution" );
141 return;
142 }
143
144 doExecute();
145 }
146
147
148
149
150
151 protected abstract void doExecute()
152 throws MojoExecutionException, MojoFailureException;
153
154
155
156
157 public ArchiverManager getArchiverManager()
158 {
159 return this.archiverManager;
160 }
161
162
163
164
165
166
167
168
169 protected void copyFile( File artifact, File destFile )
170 throws MojoExecutionException
171 {
172 try
173 {
174 getLog().info( "Copying "
175 + ( this.outputAbsoluteArtifactFilename ? artifact.getAbsolutePath() : artifact.getName() ) + " to "
176 + destFile );
177
178 if ( artifact.isDirectory() )
179 {
180
181 throw new MojoExecutionException( "Artifact has not been packaged yet. When used on reactor artifact, "
182 + "copy should be executed after packaging: see MDEP-187." );
183 }
184
185 FileUtils.copyFile( artifact, destFile );
186 }
187 catch ( IOException e )
188 {
189 throw new MojoExecutionException( "Error copying artifact from " + artifact + " to " + destFile, e );
190 }
191 }
192
193
194
195
196
197
198
199
200
201 protected void unpack( Artifact artifact, File ___location, String encoding, FileMapper[] fileMappers )
202 throws MojoExecutionException
203 {
204 unpack( artifact, ___location, null, null, encoding, fileMappers );
205 }
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221 protected void unpack( Artifact artifact, File ___location, String includes, String excludes, String encoding,
222 FileMapper[] fileMappers ) throws MojoExecutionException
223 {
224 unpack( artifact, artifact.getType(), ___location, includes, excludes, encoding, fileMappers );
225 }
226
227
228
229
230
231
232
233
234
235
236
237
238 protected void unpack( Artifact artifact, String type, File ___location, String includes, String excludes,
239 String encoding, FileMapper[] fileMappers )
240 throws MojoExecutionException
241 {
242 File file = artifact.getFile();
243 try
244 {
245 logUnpack( file, ___location, includes, excludes );
246
247 ___location.mkdirs();
248 if ( !___location.exists() )
249 {
250 throw new MojoExecutionException( "Location to write unpacked files to could not be created: "
251 + ___location );
252 }
253
254 if ( file.isDirectory() )
255 {
256
257 throw new MojoExecutionException( "Artifact has not been packaged yet. When used on reactor artifact, "
258 + "unpack should be executed after packaging: see MDEP-98." );
259 }
260
261 UnArchiver unArchiver;
262
263 try
264 {
265 unArchiver = archiverManager.getUnArchiver( type );
266 getLog().debug( "Found unArchiver by type: " + unArchiver );
267 }
268 catch ( NoSuchArchiverException e )
269 {
270 unArchiver = archiverManager.getUnArchiver( file );
271 getLog().debug( "Found unArchiver by extension: " + unArchiver );
272 }
273
274 if ( encoding != null && unArchiver instanceof ZipUnArchiver )
275 {
276 ( (ZipUnArchiver) unArchiver ).setEncoding( encoding );
277 getLog().info( "Unpacks '" + type + "' with encoding '" + encoding + "'." );
278 }
279
280 unArchiver.setIgnorePermissions( ignorePermissions );
281
282 unArchiver.setSourceFile( file );
283
284 unArchiver.setDestDirectory( ___location );
285
286 if ( StringUtils.isNotEmpty( excludes ) || StringUtils.isNotEmpty( includes ) )
287 {
288
289
290
291 IncludeExcludeFileSelector[] selectors =
292 new IncludeExcludeFileSelector[] { new IncludeExcludeFileSelector() };
293
294 if ( StringUtils.isNotEmpty( excludes ) )
295 {
296 selectors[0].setExcludes( excludes.split( "," ) );
297 }
298
299 if ( StringUtils.isNotEmpty( includes ) )
300 {
301 selectors[0].setIncludes( includes.split( "," ) );
302 }
303
304 unArchiver.setFileSelectors( selectors );
305 }
306 if ( this.silent )
307 {
308 silenceUnarchiver( unArchiver );
309 }
310
311 unArchiver.setFileMappers( fileMappers );
312
313 unArchiver.extract();
314 }
315 catch ( NoSuchArchiverException e )
316 {
317 throw new MojoExecutionException( "Unknown archiver type", e );
318 }
319 catch ( ArchiverException e )
320 {
321 throw new MojoExecutionException( "Error unpacking file: " + file + " to: " + ___location
322 + System.lineSeparator() + e.toString(), e );
323 }
324 }
325
326 private void silenceUnarchiver( UnArchiver unArchiver )
327 {
328
329 try
330 {
331 Field field = ReflectionUtils.getFieldByNameIncludingSuperclasses( "logger", unArchiver.getClass() );
332
333 field.setAccessible( true );
334
335 field.set( unArchiver, this.getLog() );
336 }
337 catch ( Exception e )
338 {
339
340 }
341 }
342
343
344
345
346
347 public ProjectBuildingRequest newResolveArtifactProjectBuildingRequest()
348 {
349 ProjectBuildingRequest buildingRequest =
350 new DefaultProjectBuildingRequest( session.getProjectBuildingRequest() );
351
352 buildingRequest.setRemoteRepositories( remoteRepositories );
353
354 return buildingRequest;
355 }
356
357
358
359
360 public MavenProject getProject()
361 {
362 return this.project;
363 }
364
365
366
367
368 public void setArchiverManager( ArchiverManager archiverManager )
369 {
370 this.archiverManager = archiverManager;
371 }
372
373
374
375
376 public boolean isUseJvmChmod()
377 {
378 return useJvmChmod;
379 }
380
381
382
383
384 public void setUseJvmChmod( boolean useJvmChmod )
385 {
386 this.useJvmChmod = useJvmChmod;
387 }
388
389
390
391
392 public boolean isSkip()
393 {
394 return skip;
395 }
396
397
398
399
400 public void setSkip( boolean skip )
401 {
402 this.skip = skip;
403 }
404
405
406
407
408 protected final boolean isSilent()
409 {
410 return silent;
411 }
412
413
414
415
416 public void setSilent( boolean silent )
417 {
418 this.silent = silent;
419 if ( silent )
420 {
421 setLog( new DependencySilentLog() );
422 }
423 }
424
425 private void logUnpack( File file, File ___location, String includes, String excludes )
426 {
427 if ( !getLog().isInfoEnabled() )
428 {
429 return;
430 }
431
432 StringBuilder msg = new StringBuilder();
433 msg.append( "Unpacking " );
434 msg.append( file );
435 msg.append( " to " );
436 msg.append( ___location );
437
438 if ( includes != null && excludes != null )
439 {
440 msg.append( " with includes \"" );
441 msg.append( includes );
442 msg.append( "\" and excludes \"" );
443 msg.append( excludes );
444 msg.append( "\"" );
445 }
446 else if ( includes != null )
447 {
448 msg.append( " with includes \"" );
449 msg.append( includes );
450 msg.append( "\"" );
451 }
452 else if ( excludes != null )
453 {
454 msg.append( " with excludes \"" );
455 msg.append( excludes );
456 msg.append( "\"" );
457 }
458
459 getLog().info( msg.toString() );
460 }
461 }