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 = "${project.pluginArtifactRepositories}", readonly = true, required = true )
97 private List<ArtifactRepository> remotePluginRepositories;
98
99
100
101
102 @Parameter( defaultValue = "${reactorProjects}", readonly = true )
103 protected List<MavenProject> reactorProjects;
104
105
106
107
108 @Parameter( defaultValue = "${session}", readonly = true, required = true )
109 protected MavenSession session;
110
111
112
113
114
115
116 @Parameter( property = "silent", defaultValue = "false" )
117 private boolean silent;
118
119
120
121
122
123
124 @Parameter( property = "outputAbsoluteArtifactFilename", defaultValue = "false" )
125 protected boolean outputAbsoluteArtifactFilename;
126
127
128
129
130
131
132 @Parameter( property = "mdep.skip", defaultValue = "false" )
133 private boolean skip;
134
135
136
137
138
139
140 @Override
141 public final void execute()
142 throws MojoExecutionException, MojoFailureException
143 {
144 if ( isSkip() )
145 {
146 getLog().info( "Skipping plugin execution" );
147 return;
148 }
149
150 doExecute();
151 }
152
153
154
155
156
157 protected abstract void doExecute()
158 throws MojoExecutionException, MojoFailureException;
159
160
161
162
163 public ArchiverManager getArchiverManager()
164 {
165 return this.archiverManager;
166 }
167
168
169
170
171
172
173
174
175 protected void copyFile( File artifact, File destFile )
176 throws MojoExecutionException
177 {
178 try
179 {
180 getLog().info( "Copying "
181 + ( this.outputAbsoluteArtifactFilename ? artifact.getAbsolutePath() : artifact.getName() ) + " to "
182 + destFile );
183
184 if ( artifact.isDirectory() )
185 {
186
187 throw new MojoExecutionException( "Artifact has not been packaged yet. When used on reactor artifact, "
188 + "copy should be executed after packaging: see MDEP-187." );
189 }
190
191 FileUtils.copyFile( artifact, destFile );
192 }
193 catch ( IOException e )
194 {
195 throw new MojoExecutionException( "Error copying artifact from " + artifact + " to " + destFile, e );
196 }
197 }
198
199
200
201
202
203
204
205
206
207 protected void unpack( Artifact artifact, File location, String encoding, FileMapper[] fileMappers )
208 throws MojoExecutionException
209 {
210 unpack( artifact, location, null, null, encoding, fileMappers );
211 }
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227 protected void unpack( Artifact artifact, File location, String includes, String excludes, String encoding,
228 FileMapper[] fileMappers ) throws MojoExecutionException
229 {
230 unpack( artifact, artifact.getType(), location, includes, excludes, encoding, fileMappers );
231 }
232
233
234
235
236
237
238
239
240
241
242
243
244 protected void unpack( Artifact artifact, String type, File location, String includes, String excludes,
245 String encoding, FileMapper[] fileMappers )
246 throws MojoExecutionException
247 {
248 File file = artifact.getFile();
249 try
250 {
251 logUnpack( file, location, includes, excludes );
252
253 location.mkdirs();
254 if ( !location.exists() )
255 {
256 throw new MojoExecutionException( "Location to write unpacked files to could not be created: "
257 + location );
258 }
259
260 if ( file.isDirectory() )
261 {
262
263 throw new MojoExecutionException( "Artifact has not been packaged yet. When used on reactor artifact, "
264 + "unpack should be executed after packaging: see MDEP-98." );
265 }
266
267 UnArchiver unArchiver;
268
269 try
270 {
271 unArchiver = archiverManager.getUnArchiver( type );
272 getLog().debug( "Found unArchiver by type: " + unArchiver );
273 }
274 catch ( NoSuchArchiverException e )
275 {
276 unArchiver = archiverManager.getUnArchiver( file );
277 getLog().debug( "Found unArchiver by extension: " + unArchiver );
278 }
279
280 if ( encoding != null && unArchiver instanceof ZipUnArchiver )
281 {
282 ( (ZipUnArchiver) unArchiver ).setEncoding( encoding );
283 getLog().info( "Unpacks '" + type + "' with encoding '" + encoding + "'." );
284 }
285
286 unArchiver.setIgnorePermissions( ignorePermissions );
287
288 unArchiver.setSourceFile( file );
289
290 unArchiver.setDestDirectory( location );
291
292 if ( StringUtils.isNotEmpty( excludes ) || StringUtils.isNotEmpty( includes ) )
293 {
294
295
296
297 IncludeExcludeFileSelector[] selectors =
298 new IncludeExcludeFileSelector[] { new IncludeExcludeFileSelector() };
299
300 if ( StringUtils.isNotEmpty( excludes ) )
301 {
302 selectors[0].setExcludes( excludes.split( "," ) );
303 }
304
305 if ( StringUtils.isNotEmpty( includes ) )
306 {
307 selectors[0].setIncludes( includes.split( "," ) );
308 }
309
310 unArchiver.setFileSelectors( selectors );
311 }
312 if ( this.silent )
313 {
314 silenceUnarchiver( unArchiver );
315 }
316
317 unArchiver.setFileMappers( fileMappers );
318
319 unArchiver.extract();
320 }
321 catch ( NoSuchArchiverException e )
322 {
323 throw new MojoExecutionException( "Unknown archiver type", e );
324 }
325 catch ( ArchiverException e )
326 {
327 throw new MojoExecutionException( "Error unpacking file: " + file + " to: " + location, e );
328 }
329 }
330
331 private void silenceUnarchiver( UnArchiver unArchiver )
332 {
333
334 try
335 {
336 Field field = ReflectionUtils.getFieldByNameIncludingSuperclasses( "logger", unArchiver.getClass() );
337
338 field.setAccessible( true );
339
340 field.set( unArchiver, this.getLog() );
341 }
342 catch ( Exception e )
343 {
344
345 }
346 }
347
348
349
350
351
352 public ProjectBuildingRequest newResolveArtifactProjectBuildingRequest()
353 {
354 return newProjectBuildingRequest( remoteRepositories );
355 }
356
357
358
359
360
361 protected ProjectBuildingRequest newResolvePluginProjectBuildingRequest()
362 {
363 return newProjectBuildingRequest( remotePluginRepositories );
364 }
365
366 private ProjectBuildingRequest newProjectBuildingRequest( List<ArtifactRepository> repositories )
367 {
368 ProjectBuildingRequest buildingRequest =
369 new DefaultProjectBuildingRequest( session.getProjectBuildingRequest() );
370
371 buildingRequest.setRemoteRepositories( repositories );
372
373 return buildingRequest;
374 }
375
376
377
378
379 public MavenProject getProject()
380 {
381 return this.project;
382 }
383
384
385
386
387 public void setArchiverManager( ArchiverManager archiverManager )
388 {
389 this.archiverManager = archiverManager;
390 }
391
392
393
394
395 public boolean isUseJvmChmod()
396 {
397 return useJvmChmod;
398 }
399
400
401
402
403 public void setUseJvmChmod( boolean useJvmChmod )
404 {
405 this.useJvmChmod = useJvmChmod;
406 }
407
408
409
410
411 public boolean isSkip()
412 {
413 return skip;
414 }
415
416
417
418
419 public void setSkip( boolean skip )
420 {
421 this.skip = skip;
422 }
423
424
425
426
427 protected final boolean isSilent()
428 {
429 return silent;
430 }
431
432
433
434
435 public void setSilent( boolean silent )
436 {
437 this.silent = silent;
438 if ( silent )
439 {
440 setLog( new DependencySilentLog() );
441 }
442 }
443
444 private void logUnpack( File file, File location, String includes, String excludes )
445 {
446 if ( !getLog().isInfoEnabled() )
447 {
448 return;
449 }
450
451 StringBuilder msg = new StringBuilder();
452 msg.append( "Unpacking " );
453 msg.append( file );
454 msg.append( " to " );
455 msg.append( location );
456
457 if ( includes != null && excludes != null )
458 {
459 msg.append( " with includes \"" );
460 msg.append( includes );
461 msg.append( "\" and excludes \"" );
462 msg.append( excludes );
463 msg.append( "\"" );
464 }
465 else if ( includes != null )
466 {
467 msg.append( " with includes \"" );
468 msg.append( includes );
469 msg.append( "\"" );
470 }
471 else if ( excludes != null )
472 {
473 msg.append( " with excludes \"" );
474 msg.append( excludes );
475 msg.append( "\"" );
476 }
477
478 getLog().info( msg.toString() );
479 }
480 }