1 package org.apache.maven.plugin.dependency;
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.factory.ArtifactFactory;
23 import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
24 import org.apache.maven.artifact.repository.ArtifactRepository;
25 import org.apache.maven.artifact.resolver.ArtifactCollector;
26 import org.apache.maven.artifact.resolver.ArtifactResolver;
27 import org.apache.maven.plugin.AbstractMojo;
28 import org.apache.maven.plugin.MojoExecutionException;
29 import org.apache.maven.plugin.dependency.utils.DependencySilentLog;
30 import org.apache.maven.plugin.logging.Log;
31 import org.apache.maven.plugins.annotations.Component;
32 import org.apache.maven.plugins.annotations.Parameter;
33 import org.apache.maven.project.MavenProject;
34 import org.codehaus.plexus.archiver.ArchiverException;
35 import org.codehaus.plexus.archiver.UnArchiver;
36 import org.codehaus.plexus.archiver.manager.ArchiverManager;
37 import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
38 import org.codehaus.plexus.components.io.fileselectors.IncludeExcludeFileSelector;
39 import org.codehaus.plexus.util.FileUtils;
40 import org.codehaus.plexus.util.ReflectionUtils;
41 import org.codehaus.plexus.util.StringUtils;
42
43 import java.io.File;
44 import java.io.IOException;
45 import java.lang.reflect.Field;
46 import java.util.List;
47
48
49
50
51
52
53 public abstract class AbstractDependencyMojo
54 extends AbstractMojo
55 {
56
57
58
59 @Component
60 protected ArtifactFactory factory;
61
62
63
64
65 @Component
66 protected ArtifactResolver resolver;
67
68
69
70
71 @Component( role = ArtifactCollector.class )
72 protected ArtifactCollector artifactCollector;
73
74
75
76
77 @Component( role = ArtifactMetadataSource.class, hint = "maven" )
78 protected ArtifactMetadataSource artifactMetadataSource;
79
80
81
82
83 @Parameter( defaultValue = "${localRepository}", readonly = true, required = true )
84 private ArtifactRepository local;
85
86
87
88
89 @Parameter( defaultValue = "${project.remoteArtifactRepositories}", readonly = true, required = true )
90 protected List<ArtifactRepository> remoteRepos;
91
92
93
94
95 @Component
96 protected ArchiverManager archiverManager;
97
98
99
100
101
102
103
104
105 @Parameter( property = "dependency.useJvmChmod", defaultValue = "false" )
106 private boolean useJvmChmod;
107
108
109
110
111 @Component
112 protected MavenProject project;
113
114
115
116
117 @Parameter( defaultValue = "${reactorProjects}" )
118 protected List<MavenProject> reactorProjects;
119
120
121
122
123
124
125 @Parameter( property = "silent", defaultValue = "false" )
126 public boolean silent;
127
128
129
130
131
132
133 @Parameter( property = "outputAbsoluteArtifactFilename", defaultValue = "false" )
134 protected boolean outputAbsoluteArtifactFilename;
135
136 private Log log;
137
138
139
140
141 public Log getLog()
142 {
143 if ( log == null )
144 {
145 if ( silent )
146 {
147 log = new DependencySilentLog();
148 }
149 else
150 {
151 log = super.getLog();
152 }
153 }
154
155 return this.log;
156 }
157
158
159
160
161 public ArchiverManager getArchiverManager()
162 {
163 return this.archiverManager;
164 }
165
166
167
168
169
170
171
172
173
174 protected void copyFile( File artifact, File destFile )
175 throws MojoExecutionException
176 {
177 Log theLog = this.getLog();
178 try
179 {
180 theLog.info(
181 "Copying " + ( this.outputAbsoluteArtifactFilename ? artifact.getAbsolutePath() : artifact.getName() )
182 + " to " + 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 protected void unpack( File file, File location )
200 throws MojoExecutionException
201 {
202 unpack( file, location, null, null );
203 }
204
205
206
207
208
209
210
211
212
213
214
215 protected void unpack( File file, File location, String includes, String excludes )
216 throws MojoExecutionException
217 {
218 try
219 {
220 logUnpack( file, location, includes, excludes );
221
222 location.mkdirs();
223
224 if ( file.isDirectory() )
225 {
226
227 throw new MojoExecutionException( "Artifact has not been packaged yet. When used on reactor artifact, "
228 + "unpack should be executed after packaging: see MDEP-98." );
229 }
230
231 UnArchiver unArchiver;
232
233 unArchiver = archiverManager.getUnArchiver( file );
234
235 unArchiver.setUseJvmChmod( useJvmChmod );
236
237 unArchiver.setSourceFile( file );
238
239 unArchiver.setDestDirectory( location );
240
241 if ( StringUtils.isNotEmpty( excludes ) || StringUtils.isNotEmpty( includes ) )
242 {
243
244
245
246 IncludeExcludeFileSelector[] selectors =
247 new IncludeExcludeFileSelector[]{ new IncludeExcludeFileSelector() };
248
249 if ( StringUtils.isNotEmpty( excludes ) )
250 {
251 selectors[0].setExcludes( excludes.split( "," ) );
252 }
253
254 if ( StringUtils.isNotEmpty( includes ) )
255 {
256 selectors[0].setIncludes( includes.split( "," ) );
257 }
258
259 unArchiver.setFileSelectors( selectors );
260 }
261 if ( this.silent )
262 {
263 silenceUnarchiver( unArchiver );
264 }
265
266 unArchiver.extract();
267 }
268 catch ( NoSuchArchiverException e )
269 {
270 throw new MojoExecutionException( "Unknown archiver type", e );
271 }
272 catch ( ArchiverException e )
273 {
274 throw new MojoExecutionException(
275 "Error unpacking file: " + file + " to: " + location + "\r\n" + e.toString(), e );
276 }
277 }
278
279 private void silenceUnarchiver( UnArchiver unArchiver )
280 {
281
282 try
283 {
284 Field field = ReflectionUtils.getFieldByNameIncludingSuperclasses( "logger", unArchiver.getClass() );
285
286 field.setAccessible( true );
287
288 field.set( unArchiver, this.getLog() );
289 }
290 catch ( Exception e )
291 {
292
293 }
294 }
295
296
297
298
299 public ArtifactFactory getFactory()
300 {
301 return this.factory;
302 }
303
304
305
306
307 public void setFactory( ArtifactFactory factory )
308 {
309 this.factory = factory;
310 }
311
312
313
314
315 public MavenProject getProject()
316 {
317 return this.project;
318 }
319
320
321
322
323 protected ArtifactRepository getLocal()
324 {
325 return this.local;
326 }
327
328
329
330
331 public void setLocal( ArtifactRepository local )
332 {
333 this.local = local;
334 }
335
336
337
338
339 public List<ArtifactRepository> getRemoteRepos()
340 {
341 return this.remoteRepos;
342 }
343
344
345
346
347 public void setRemoteRepos( List<ArtifactRepository> remoteRepos )
348 {
349 this.remoteRepos = remoteRepos;
350 }
351
352
353
354
355 public org.apache.maven.artifact.resolver.ArtifactResolver getResolver()
356 {
357 return this.resolver;
358 }
359
360
361
362
363 public void setResolver( ArtifactResolver resolver )
364 {
365 this.resolver = resolver;
366 }
367
368
369
370
371 public void setArchiverManager( ArchiverManager archiverManager )
372 {
373 this.archiverManager = archiverManager;
374 }
375
376
377
378
379 public ArtifactCollector getArtifactCollector()
380 {
381 return this.artifactCollector;
382 }
383
384
385
386
387 public void setArtifactCollector( ArtifactCollector theArtifactCollector )
388 {
389 this.artifactCollector = theArtifactCollector;
390 }
391
392
393
394
395 public ArtifactMetadataSource getArtifactMetadataSource()
396 {
397 return this.artifactMetadataSource;
398 }
399
400
401
402
403 public void setArtifactMetadataSource( ArtifactMetadataSource theArtifactMetadataSource )
404 {
405 this.artifactMetadataSource = theArtifactMetadataSource;
406 }
407
408 private void logUnpack( File file, File location, String includes, String excludes )
409 {
410 if ( !getLog().isInfoEnabled() )
411 {
412 return;
413 }
414
415 StringBuffer msg = new StringBuffer();
416 msg.append( "Unpacking " );
417 msg.append( file );
418 msg.append( " to " );
419 msg.append( location );
420
421 if ( includes != null && excludes != null )
422 {
423 msg.append( " with includes \"" );
424 msg.append( includes );
425 msg.append( "\" and excludes \"" );
426 msg.append( excludes );
427 msg.append( "\"" );
428 }
429 else if ( includes != null )
430 {
431 msg.append( " with includes \"" );
432 msg.append( includes );
433 msg.append( "\"" );
434 }
435 else if ( excludes != null )
436 {
437 msg.append( " with excludes \"" );
438 msg.append( excludes );
439 msg.append( "\"" );
440 }
441
442 getLog().info( msg.toString() );
443 }
444 }