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 | |
@Component |
102 | |
protected MavenProject project; |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
@Parameter( defaultValue = "${reactorProjects}" ) |
108 | |
protected List<MavenProject> reactorProjects; |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
@Parameter( property = "silent", defaultValue = "false" ) |
116 | |
public boolean silent; |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
@Parameter( property = "outputAbsoluteArtifactFilename", defaultValue = "false" ) |
124 | |
protected boolean outputAbsoluteArtifactFilename; |
125 | |
|
126 | |
private Log log; |
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
public Log getLog() |
132 | |
{ |
133 | 803 | if ( log == null ) |
134 | |
{ |
135 | 130 | if ( silent ) |
136 | |
{ |
137 | 116 | log = new DependencySilentLog(); |
138 | |
} |
139 | |
else |
140 | |
{ |
141 | 14 | log = super.getLog(); |
142 | |
} |
143 | |
} |
144 | |
|
145 | 803 | return this.log; |
146 | |
} |
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
public ArchiverManager getArchiverManager() |
152 | |
{ |
153 | 50 | return this.archiverManager; |
154 | |
} |
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
protected void copyFile( File artifact, File destFile ) |
165 | |
throws MojoExecutionException |
166 | |
{ |
167 | 190 | Log theLog = this.getLog(); |
168 | |
try |
169 | |
{ |
170 | 190 | theLog.info( |
171 | |
"Copying " + ( this.outputAbsoluteArtifactFilename ? artifact.getAbsolutePath() : artifact.getName() ) |
172 | |
+ " to " + destFile ); |
173 | |
|
174 | 190 | if ( artifact.isDirectory() ) |
175 | |
{ |
176 | |
|
177 | 0 | throw new MojoExecutionException( "Artifact has not been packaged yet. When used on reactor artifact, " |
178 | |
+ "copy should be executed after packaging: see MDEP-187." ); |
179 | |
} |
180 | |
|
181 | 190 | FileUtils.copyFile( artifact, destFile ); |
182 | |
} |
183 | 0 | catch ( IOException e ) |
184 | |
{ |
185 | 0 | throw new MojoExecutionException( "Error copying artifact from " + artifact + " to " + destFile, e ); |
186 | 190 | } |
187 | 190 | } |
188 | |
|
189 | |
protected void unpack( File file, File location ) |
190 | |
throws MojoExecutionException |
191 | |
{ |
192 | 0 | unpack( file, location, null, null ); |
193 | 0 | } |
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
protected void unpack( File file, File location, String includes, String excludes ) |
206 | |
throws MojoExecutionException |
207 | |
{ |
208 | |
try |
209 | |
{ |
210 | 183 | logUnpack( file, location, includes, excludes ); |
211 | |
|
212 | 183 | location.mkdirs(); |
213 | |
|
214 | 183 | if ( file.isDirectory() ) |
215 | |
{ |
216 | |
|
217 | 0 | throw new MojoExecutionException( "Artifact has not been packaged yet. When used on reactor artifact, " |
218 | |
+ "unpack should be executed after packaging: see MDEP-98." ); |
219 | |
} |
220 | |
|
221 | |
UnArchiver unArchiver; |
222 | |
|
223 | 183 | unArchiver = archiverManager.getUnArchiver( file ); |
224 | |
|
225 | 183 | unArchiver.setSourceFile( file ); |
226 | |
|
227 | 183 | unArchiver.setDestDirectory( location ); |
228 | |
|
229 | 183 | if ( StringUtils.isNotEmpty( excludes ) || StringUtils.isNotEmpty( includes ) ) |
230 | |
{ |
231 | |
|
232 | |
|
233 | |
|
234 | 54 | IncludeExcludeFileSelector[] selectors = |
235 | |
new IncludeExcludeFileSelector[]{ new IncludeExcludeFileSelector() }; |
236 | |
|
237 | 54 | if ( StringUtils.isNotEmpty( excludes ) ) |
238 | |
{ |
239 | 25 | selectors[0].setExcludes( excludes.split( "," ) ); |
240 | |
} |
241 | |
|
242 | 54 | if ( StringUtils.isNotEmpty( includes ) ) |
243 | |
{ |
244 | 29 | selectors[0].setIncludes( includes.split( "," ) ); |
245 | |
} |
246 | |
|
247 | 54 | unArchiver.setFileSelectors( selectors ); |
248 | |
} |
249 | 183 | if ( this.silent ) |
250 | |
{ |
251 | 168 | silenceUnarchiver( unArchiver ); |
252 | |
} |
253 | |
|
254 | 183 | unArchiver.extract(); |
255 | |
} |
256 | 0 | catch ( NoSuchArchiverException e ) |
257 | |
{ |
258 | 0 | throw new MojoExecutionException( "Unknown archiver type", e ); |
259 | |
} |
260 | 0 | catch ( ArchiverException e ) |
261 | |
{ |
262 | 0 | throw new MojoExecutionException( |
263 | |
"Error unpacking file: " + file + " to: " + location + "\r\n" + e.toString(), e ); |
264 | 183 | } |
265 | 183 | } |
266 | |
|
267 | |
private void silenceUnarchiver( UnArchiver unArchiver ) |
268 | |
{ |
269 | |
|
270 | |
try |
271 | |
{ |
272 | 168 | Field field = ReflectionUtils.getFieldByNameIncludingSuperclasses( "logger", unArchiver.getClass() ); |
273 | |
|
274 | 168 | field.setAccessible( true ); |
275 | |
|
276 | 168 | field.set( unArchiver, this.getLog() ); |
277 | |
} |
278 | 0 | catch ( Exception e ) |
279 | |
{ |
280 | |
|
281 | 168 | } |
282 | 168 | } |
283 | |
|
284 | |
|
285 | |
|
286 | |
|
287 | |
public ArtifactFactory getFactory() |
288 | |
{ |
289 | 0 | return this.factory; |
290 | |
} |
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
public void setFactory( ArtifactFactory factory ) |
296 | |
{ |
297 | 62 | this.factory = factory; |
298 | 62 | } |
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
public MavenProject getProject() |
304 | |
{ |
305 | 232 | return this.project; |
306 | |
} |
307 | |
|
308 | |
|
309 | |
|
310 | |
|
311 | |
protected ArtifactRepository getLocal() |
312 | |
{ |
313 | 209 | return this.local; |
314 | |
} |
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
public void setLocal( ArtifactRepository local ) |
320 | |
{ |
321 | 59 | this.local = local; |
322 | 59 | } |
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
public List<ArtifactRepository> getRemoteRepos() |
328 | |
{ |
329 | 0 | return this.remoteRepos; |
330 | |
} |
331 | |
|
332 | |
|
333 | |
|
334 | |
|
335 | |
public void setRemoteRepos( List<ArtifactRepository> remoteRepos ) |
336 | |
{ |
337 | 0 | this.remoteRepos = remoteRepos; |
338 | 0 | } |
339 | |
|
340 | |
|
341 | |
|
342 | |
|
343 | |
public org.apache.maven.artifact.resolver.ArtifactResolver getResolver() |
344 | |
{ |
345 | 0 | return this.resolver; |
346 | |
} |
347 | |
|
348 | |
|
349 | |
|
350 | |
|
351 | |
public void setResolver( ArtifactResolver resolver ) |
352 | |
{ |
353 | 63 | this.resolver = resolver; |
354 | 63 | } |
355 | |
|
356 | |
|
357 | |
|
358 | |
|
359 | |
public void setArchiverManager( ArchiverManager archiverManager ) |
360 | |
{ |
361 | 11 | this.archiverManager = archiverManager; |
362 | 11 | } |
363 | |
|
364 | |
|
365 | |
|
366 | |
|
367 | |
public ArtifactCollector getArtifactCollector() |
368 | |
{ |
369 | 0 | return this.artifactCollector; |
370 | |
} |
371 | |
|
372 | |
|
373 | |
|
374 | |
|
375 | |
public void setArtifactCollector( ArtifactCollector theArtifactCollector ) |
376 | |
{ |
377 | 53 | this.artifactCollector = theArtifactCollector; |
378 | 53 | } |
379 | |
|
380 | |
|
381 | |
|
382 | |
|
383 | |
public ArtifactMetadataSource getArtifactMetadataSource() |
384 | |
{ |
385 | 0 | return this.artifactMetadataSource; |
386 | |
} |
387 | |
|
388 | |
|
389 | |
|
390 | |
|
391 | |
public void setArtifactMetadataSource( ArtifactMetadataSource theArtifactMetadataSource ) |
392 | |
{ |
393 | 0 | this.artifactMetadataSource = theArtifactMetadataSource; |
394 | 0 | } |
395 | |
|
396 | |
private void logUnpack( File file, File location, String includes, String excludes ) |
397 | |
{ |
398 | 183 | if ( !getLog().isInfoEnabled() ) |
399 | |
{ |
400 | 168 | return; |
401 | |
} |
402 | |
|
403 | 15 | StringBuffer msg = new StringBuffer(); |
404 | 15 | msg.append( "Unpacking " ); |
405 | 15 | msg.append( file ); |
406 | 15 | msg.append( " to " ); |
407 | 15 | msg.append( location ); |
408 | |
|
409 | 15 | if ( includes != null && excludes != null ) |
410 | |
{ |
411 | 15 | msg.append( " with includes \"" ); |
412 | 15 | msg.append( includes ); |
413 | 15 | msg.append( "\" and excludes \"" ); |
414 | 15 | msg.append( excludes ); |
415 | 15 | msg.append( "\"" ); |
416 | |
} |
417 | 0 | else if ( includes != null ) |
418 | |
{ |
419 | 0 | msg.append( " with includes \"" ); |
420 | 0 | msg.append( includes ); |
421 | 0 | msg.append( "\"" ); |
422 | |
} |
423 | 0 | else if ( excludes != null ) |
424 | |
{ |
425 | 0 | msg.append( " with excludes \"" ); |
426 | 0 | msg.append( excludes ); |
427 | 0 | msg.append( "\"" ); |
428 | |
} |
429 | |
|
430 | 15 | getLog().info( msg.toString() ); |
431 | 15 | } |
432 | |
} |