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