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 | 140 | 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 | 803 | if ( log == null ) |
144 | |
{ |
145 | 130 | if ( silent ) |
146 | |
{ |
147 | 116 | log = new DependencySilentLog(); |
148 | |
} |
149 | |
else |
150 | |
{ |
151 | 14 | log = super.getLog(); |
152 | |
} |
153 | |
} |
154 | |
|
155 | 803 | return this.log; |
156 | |
} |
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
public ArchiverManager getArchiverManager() |
162 | |
{ |
163 | 50 | 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 | 190 | Log theLog = this.getLog(); |
178 | |
try |
179 | |
{ |
180 | 190 | theLog.info( |
181 | |
"Copying " + ( this.outputAbsoluteArtifactFilename ? artifact.getAbsolutePath() : artifact.getName() ) |
182 | |
+ " to " + destFile ); |
183 | |
|
184 | 190 | if ( artifact.isDirectory() ) |
185 | |
{ |
186 | |
|
187 | 0 | 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 | 190 | FileUtils.copyFile( artifact, destFile ); |
192 | |
} |
193 | 0 | catch ( IOException e ) |
194 | |
{ |
195 | 0 | throw new MojoExecutionException( "Error copying artifact from " + artifact + " to " + destFile, e ); |
196 | 190 | } |
197 | 190 | } |
198 | |
|
199 | |
protected void unpack( File file, File location ) |
200 | |
throws MojoExecutionException |
201 | |
{ |
202 | 0 | unpack( file, location, null, null ); |
203 | 0 | } |
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 | 183 | logUnpack( file, location, includes, excludes ); |
221 | |
|
222 | 183 | location.mkdirs(); |
223 | |
|
224 | 183 | if ( file.isDirectory() ) |
225 | |
{ |
226 | |
|
227 | 0 | 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 | 183 | unArchiver = archiverManager.getUnArchiver( file ); |
234 | |
|
235 | 183 | unArchiver.setUseJvmChmod( useJvmChmod ); |
236 | |
|
237 | 183 | unArchiver.setSourceFile( file ); |
238 | |
|
239 | 183 | unArchiver.setDestDirectory( location ); |
240 | |
|
241 | 183 | if ( StringUtils.isNotEmpty( excludes ) || StringUtils.isNotEmpty( includes ) ) |
242 | |
{ |
243 | |
|
244 | |
|
245 | |
|
246 | 54 | IncludeExcludeFileSelector[] selectors = |
247 | |
new IncludeExcludeFileSelector[]{ new IncludeExcludeFileSelector() }; |
248 | |
|
249 | 54 | if ( StringUtils.isNotEmpty( excludes ) ) |
250 | |
{ |
251 | 25 | selectors[0].setExcludes( excludes.split( "," ) ); |
252 | |
} |
253 | |
|
254 | 54 | if ( StringUtils.isNotEmpty( includes ) ) |
255 | |
{ |
256 | 29 | selectors[0].setIncludes( includes.split( "," ) ); |
257 | |
} |
258 | |
|
259 | 54 | unArchiver.setFileSelectors( selectors ); |
260 | |
} |
261 | 183 | if ( this.silent ) |
262 | |
{ |
263 | 168 | silenceUnarchiver( unArchiver ); |
264 | |
} |
265 | |
|
266 | 183 | unArchiver.extract(); |
267 | |
} |
268 | 0 | catch ( NoSuchArchiverException e ) |
269 | |
{ |
270 | 0 | throw new MojoExecutionException( "Unknown archiver type", e ); |
271 | |
} |
272 | 0 | catch ( ArchiverException e ) |
273 | |
{ |
274 | 0 | throw new MojoExecutionException( |
275 | |
"Error unpacking file: " + file + " to: " + location + "\r\n" + e.toString(), e ); |
276 | 183 | } |
277 | 183 | } |
278 | |
|
279 | |
private void silenceUnarchiver( UnArchiver unArchiver ) |
280 | |
{ |
281 | |
|
282 | |
try |
283 | |
{ |
284 | 168 | Field field = ReflectionUtils.getFieldByNameIncludingSuperclasses( "logger", unArchiver.getClass() ); |
285 | |
|
286 | 168 | field.setAccessible( true ); |
287 | |
|
288 | 168 | field.set( unArchiver, this.getLog() ); |
289 | |
} |
290 | 0 | catch ( Exception e ) |
291 | |
{ |
292 | |
|
293 | 168 | } |
294 | 168 | } |
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
public ArtifactFactory getFactory() |
300 | |
{ |
301 | 0 | return this.factory; |
302 | |
} |
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
public void setFactory( ArtifactFactory factory ) |
308 | |
{ |
309 | 62 | this.factory = factory; |
310 | 62 | } |
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
public MavenProject getProject() |
316 | |
{ |
317 | 232 | return this.project; |
318 | |
} |
319 | |
|
320 | |
|
321 | |
|
322 | |
|
323 | |
protected ArtifactRepository getLocal() |
324 | |
{ |
325 | 209 | return this.local; |
326 | |
} |
327 | |
|
328 | |
|
329 | |
|
330 | |
|
331 | |
public void setLocal( ArtifactRepository local ) |
332 | |
{ |
333 | 59 | this.local = local; |
334 | 59 | } |
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
public List<ArtifactRepository> getRemoteRepos() |
340 | |
{ |
341 | 0 | return this.remoteRepos; |
342 | |
} |
343 | |
|
344 | |
|
345 | |
|
346 | |
|
347 | |
public void setRemoteRepos( List<ArtifactRepository> remoteRepos ) |
348 | |
{ |
349 | 0 | this.remoteRepos = remoteRepos; |
350 | 0 | } |
351 | |
|
352 | |
|
353 | |
|
354 | |
|
355 | |
public org.apache.maven.artifact.resolver.ArtifactResolver getResolver() |
356 | |
{ |
357 | 0 | return this.resolver; |
358 | |
} |
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
public void setResolver( ArtifactResolver resolver ) |
364 | |
{ |
365 | 63 | this.resolver = resolver; |
366 | 63 | } |
367 | |
|
368 | |
|
369 | |
|
370 | |
|
371 | |
public void setArchiverManager( ArchiverManager archiverManager ) |
372 | |
{ |
373 | 11 | this.archiverManager = archiverManager; |
374 | 11 | } |
375 | |
|
376 | |
|
377 | |
|
378 | |
|
379 | |
public ArtifactCollector getArtifactCollector() |
380 | |
{ |
381 | 0 | return this.artifactCollector; |
382 | |
} |
383 | |
|
384 | |
|
385 | |
|
386 | |
|
387 | |
public void setArtifactCollector( ArtifactCollector theArtifactCollector ) |
388 | |
{ |
389 | 53 | this.artifactCollector = theArtifactCollector; |
390 | 53 | } |
391 | |
|
392 | |
|
393 | |
|
394 | |
|
395 | |
public ArtifactMetadataSource getArtifactMetadataSource() |
396 | |
{ |
397 | 0 | return this.artifactMetadataSource; |
398 | |
} |
399 | |
|
400 | |
|
401 | |
|
402 | |
|
403 | |
public void setArtifactMetadataSource( ArtifactMetadataSource theArtifactMetadataSource ) |
404 | |
{ |
405 | 0 | this.artifactMetadataSource = theArtifactMetadataSource; |
406 | 0 | } |
407 | |
|
408 | |
private void logUnpack( File file, File location, String includes, String excludes ) |
409 | |
{ |
410 | 183 | if ( !getLog().isInfoEnabled() ) |
411 | |
{ |
412 | 168 | return; |
413 | |
} |
414 | |
|
415 | 15 | StringBuffer msg = new StringBuffer(); |
416 | 15 | msg.append( "Unpacking " ); |
417 | 15 | msg.append( file ); |
418 | 15 | msg.append( " to " ); |
419 | 15 | msg.append( location ); |
420 | |
|
421 | 15 | if ( includes != null && excludes != null ) |
422 | |
{ |
423 | 15 | msg.append( " with includes \"" ); |
424 | 15 | msg.append( includes ); |
425 | 15 | msg.append( "\" and excludes \"" ); |
426 | 15 | msg.append( excludes ); |
427 | 15 | msg.append( "\"" ); |
428 | |
} |
429 | 0 | else if ( includes != null ) |
430 | |
{ |
431 | 0 | msg.append( " with includes \"" ); |
432 | 0 | msg.append( includes ); |
433 | 0 | msg.append( "\"" ); |
434 | |
} |
435 | 0 | else if ( excludes != null ) |
436 | |
{ |
437 | 0 | msg.append( " with excludes \"" ); |
438 | 0 | msg.append( excludes ); |
439 | 0 | msg.append( "\"" ); |
440 | |
} |
441 | |
|
442 | 15 | getLog().info( msg.toString() ); |
443 | 15 | } |
444 | |
} |