1 | |
package org.apache.maven.plugin.dependency.fromConfiguration; |
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.repository.ArtifactRepository; |
25 | |
import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; |
26 | |
import org.apache.maven.artifact.resolver.ArtifactNotFoundException; |
27 | |
import org.apache.maven.artifact.resolver.ArtifactResolutionException; |
28 | |
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; |
29 | |
import org.apache.maven.artifact.versioning.VersionRange; |
30 | |
import org.apache.maven.model.Dependency; |
31 | |
import org.apache.maven.plugin.MojoExecutionException; |
32 | |
import org.apache.maven.plugin.MojoFailureException; |
33 | |
import org.apache.maven.plugin.dependency.AbstractDependencyMojo; |
34 | |
import org.apache.maven.plugin.dependency.utils.DependencyUtil; |
35 | |
import org.apache.maven.plugin.dependency.utils.filters.ArtifactItemFilter; |
36 | |
import org.apache.maven.plugins.annotations.Component; |
37 | |
import org.apache.maven.plugins.annotations.Parameter; |
38 | |
import org.apache.maven.project.MavenProject; |
39 | |
import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException; |
40 | |
import org.codehaus.plexus.util.StringUtils; |
41 | |
|
42 | |
import java.io.File; |
43 | |
import java.util.Collections; |
44 | |
import java.util.List; |
45 | |
import java.util.Set; |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | 54 | public abstract class AbstractFromConfigurationMojo |
56 | |
extends AbstractDependencyMojo |
57 | |
{ |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
@Parameter( property = "mdep.skip", defaultValue = "false" ) |
64 | |
private boolean skip; |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
@Parameter( property = "outputDirectory", defaultValue = "${project.build.directory}/dependency" ) |
72 | |
private File outputDirectory; |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
@Parameter( property = "mdep.overWriteReleases", defaultValue = "false" ) |
80 | |
private boolean overWriteReleases; |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
@Parameter( property = "mdep.overWriteSnapshots", defaultValue = "false" ) |
88 | |
private boolean overWriteSnapshots; |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
@Parameter( property = "mdep.overIfNewer", defaultValue = "true" ) |
96 | |
private boolean overWriteIfNewer; |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
@Parameter( defaultValue = "${reactorProjects}", readonly = true, required = true ) |
102 | |
protected List<MavenProject> reactorProjects; |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
@Parameter |
111 | |
private List<ArtifactItem> artifactItems; |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
@Component |
117 | |
private ArtifactRepositoryFactory artifactRepositoryManager; |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
@Parameter |
126 | |
private File localRepositoryDirectory; |
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
private ArtifactRepository overrideLocalRepository; |
132 | |
|
133 | |
@Component |
134 | |
private ArtifactFactory artifactFactory; |
135 | |
|
136 | |
abstract ArtifactItemFilter getMarkedArtifactFilter( ArtifactItem item ); |
137 | |
|
138 | |
|
139 | |
protected void verifyRequirements() throws MojoFailureException |
140 | |
{ |
141 | 57 | if ( artifactItems == null || artifactItems.isEmpty() ) |
142 | |
{ |
143 | 0 | throw new MojoFailureException( "Either artifact or artifactItems is required " ); |
144 | |
} |
145 | 57 | } |
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
protected List<ArtifactItem> getProcessedArtifactItems( boolean removeVersion ) |
157 | |
throws MojoExecutionException |
158 | |
{ |
159 | 63 | if ( artifactItems == null || artifactItems.size() < 1 ) |
160 | |
{ |
161 | 2 | throw new MojoExecutionException( "There are no artifactItems configured." ); |
162 | |
} |
163 | |
|
164 | 61 | for ( ArtifactItem artifactItem : artifactItems ) |
165 | |
{ |
166 | 87 | this.getLog().info( "Configured Artifact: " + artifactItem.toString() ); |
167 | |
|
168 | 87 | if ( artifactItem.getOutputDirectory() == null ) |
169 | |
{ |
170 | 70 | artifactItem.setOutputDirectory( this.outputDirectory ); |
171 | |
} |
172 | 87 | artifactItem.getOutputDirectory().mkdirs(); |
173 | |
|
174 | |
|
175 | 87 | if ( StringUtils.isEmpty( artifactItem.getVersion() ) ) |
176 | |
{ |
177 | 12 | fillMissingArtifactVersion( artifactItem ); |
178 | |
} |
179 | |
|
180 | 85 | artifactItem.setArtifact( this.getArtifact( artifactItem ) ); |
181 | |
|
182 | 81 | if ( StringUtils.isEmpty( artifactItem.getDestFileName() ) ) |
183 | |
{ |
184 | 67 | artifactItem.setDestFileName( |
185 | |
DependencyUtil.getFormattedFileName( artifactItem.getArtifact(), removeVersion ) ); |
186 | |
} |
187 | |
|
188 | |
try |
189 | |
{ |
190 | 81 | artifactItem.setNeedsProcessing( checkIfProcessingNeeded( artifactItem ) ); |
191 | |
} |
192 | 0 | catch ( ArtifactFilterException e ) |
193 | |
{ |
194 | 0 | throw new MojoExecutionException( e.getMessage(), e ); |
195 | 81 | } |
196 | |
} |
197 | 55 | return artifactItems; |
198 | |
} |
199 | |
|
200 | |
private boolean checkIfProcessingNeeded( ArtifactItem item ) |
201 | |
throws MojoExecutionException, ArtifactFilterException |
202 | |
{ |
203 | 81 | return StringUtils.equalsIgnoreCase( item.getOverWrite(), "true" ) |
204 | |
|| getMarkedArtifactFilter( item ).isArtifactIncluded( item ); |
205 | |
} |
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
protected Artifact getArtifact( ArtifactItem artifactItem ) |
216 | |
throws MojoExecutionException |
217 | |
{ |
218 | |
Artifact artifact; |
219 | |
|
220 | |
|
221 | |
VersionRange vr; |
222 | |
try |
223 | |
{ |
224 | 85 | vr = VersionRange.createFromVersionSpec( artifactItem.getVersion() ); |
225 | |
} |
226 | 0 | catch ( InvalidVersionSpecificationException e1 ) |
227 | |
{ |
228 | |
|
229 | 0 | e1.printStackTrace(); |
230 | 0 | vr = VersionRange.createFromVersion( artifactItem.getVersion() ); |
231 | 85 | } |
232 | |
|
233 | 85 | if ( StringUtils.isEmpty( artifactItem.getClassifier() ) ) |
234 | |
{ |
235 | 52 | artifact = factory.createDependencyArtifact( artifactItem.getGroupId(), artifactItem.getArtifactId(), vr, |
236 | |
artifactItem.getType(), null, Artifact.SCOPE_COMPILE ); |
237 | |
} |
238 | |
else |
239 | |
{ |
240 | 33 | artifact = factory.createDependencyArtifact( artifactItem.getGroupId(), artifactItem.getArtifactId(), vr, |
241 | |
artifactItem.getType(), artifactItem.getClassifier(), |
242 | |
Artifact.SCOPE_COMPILE ); |
243 | |
} |
244 | |
|
245 | |
|
246 | |
|
247 | 85 | Artifact result = getArtifactFomReactor( artifact ); |
248 | 85 | if ( result != null ) |
249 | |
{ |
250 | 0 | return result; |
251 | |
} |
252 | |
|
253 | |
try |
254 | |
{ |
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | 85 | resolver.resolve( artifact, remoteRepos, getLocal() ); |
266 | |
} |
267 | 2 | catch ( ArtifactResolutionException e ) |
268 | |
{ |
269 | 2 | throw new MojoExecutionException( "Unable to resolve artifact.", e ); |
270 | |
} |
271 | 2 | catch ( ArtifactNotFoundException e ) |
272 | |
{ |
273 | 2 | throw new MojoExecutionException( "Unable to find artifact.", e ); |
274 | 81 | } |
275 | |
|
276 | 81 | return artifact; |
277 | |
} |
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
private Artifact getArtifactFomReactor( Artifact artifact ) |
287 | |
{ |
288 | |
|
289 | 85 | for ( Artifact a : (Set<Artifact>) project.getArtifacts() ) |
290 | |
{ |
291 | 0 | if ( equals( artifact, a ) && hasFile( a ) ) |
292 | |
{ |
293 | 0 | return a; |
294 | |
} |
295 | |
} |
296 | |
|
297 | |
|
298 | 85 | for ( MavenProject p : reactorProjects == null ? Collections.<MavenProject>emptyList() : reactorProjects ) |
299 | |
{ |
300 | |
|
301 | 0 | if ( equals( artifact, p.getArtifact() ) && hasFile( p.getArtifact() ) ) |
302 | |
{ |
303 | 0 | return p.getArtifact(); |
304 | |
} |
305 | |
|
306 | |
|
307 | 0 | for ( Artifact a : (List<Artifact>) p.getAttachedArtifacts() ) |
308 | |
{ |
309 | 0 | if ( equals( artifact, a ) && hasFile( a ) ) |
310 | |
{ |
311 | 0 | return a; |
312 | |
} |
313 | |
} |
314 | |
} |
315 | |
|
316 | |
|
317 | 85 | return null; |
318 | |
} |
319 | |
|
320 | |
|
321 | |
|
322 | |
|
323 | |
|
324 | |
|
325 | |
|
326 | |
private static boolean hasFile( Artifact artifact ) |
327 | |
{ |
328 | 0 | return artifact != null && artifact.getFile() != null && artifact.getFile().isFile(); |
329 | |
} |
330 | |
|
331 | |
|
332 | |
|
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
private static boolean equals( Artifact a, Artifact b ) |
340 | |
{ |
341 | 0 | return a == b || !( a == null || b == null ) |
342 | |
&& StringUtils.equals( a.getGroupId(), b.getGroupId() ) |
343 | |
&& StringUtils.equals( a.getArtifactId(), b.getArtifactId() ) |
344 | |
&& StringUtils.equals( a.getVersion(), b.getVersion() ) |
345 | |
&& StringUtils.equals( a.getType(), b.getType() ) |
346 | |
&& StringUtils.equals( a.getClassifier(), b.getClassifier() ); |
347 | |
} |
348 | |
|
349 | |
|
350 | |
|
351 | |
|
352 | |
|
353 | |
|
354 | |
|
355 | |
|
356 | |
|
357 | |
private void fillMissingArtifactVersion( ArtifactItem artifact ) |
358 | |
throws MojoExecutionException |
359 | |
{ |
360 | 12 | @SuppressWarnings( "unchecked" ) List<Dependency> deps = project.getDependencies(); |
361 | 12 | @SuppressWarnings( "unchecked" ) List<Dependency> depMngt = project.getDependencyManagement() == null |
362 | |
? Collections.<Dependency>emptyList() |
363 | |
: project.getDependencyManagement().getDependencies(); |
364 | |
|
365 | 12 | if ( !findDependencyVersion( artifact, deps, false ) |
366 | |
&& ( project.getDependencyManagement() == null || !findDependencyVersion( artifact, depMngt, false ) ) |
367 | |
&& !findDependencyVersion( artifact, deps, true ) |
368 | |
&& ( project.getDependencyManagement() == null || !findDependencyVersion( artifact, depMngt, true ) ) ) |
369 | |
{ |
370 | 2 | throw new MojoExecutionException( |
371 | |
"Unable to find artifact version of " + artifact.getGroupId() + ":" + artifact.getArtifactId() |
372 | |
+ " in either dependency list or in project's dependency management." ); |
373 | |
} |
374 | 10 | } |
375 | |
|
376 | |
|
377 | |
|
378 | |
|
379 | |
|
380 | |
|
381 | |
|
382 | |
|
383 | |
|
384 | |
|
385 | |
private boolean findDependencyVersion( ArtifactItem artifact, List<Dependency> dependencies, boolean looseMatch ) |
386 | |
{ |
387 | 27 | for ( Dependency dependency : dependencies ) |
388 | |
{ |
389 | 30 | if ( StringUtils.equals( dependency.getArtifactId(), artifact.getArtifactId() ) |
390 | |
&& StringUtils.equals( dependency.getGroupId(), artifact.getGroupId() ) |
391 | |
&& ( looseMatch || StringUtils.equals( dependency.getClassifier(), artifact.getClassifier() ) ) |
392 | |
&& ( looseMatch || StringUtils.equals( dependency.getType(), artifact.getType() ) ) ) |
393 | |
{ |
394 | 10 | artifact.setVersion( dependency.getVersion() ); |
395 | |
|
396 | 10 | return true; |
397 | |
} |
398 | |
} |
399 | |
|
400 | 17 | return false; |
401 | |
} |
402 | |
|
403 | |
|
404 | |
|
405 | |
|
406 | |
|
407 | |
|
408 | |
|
409 | |
|
410 | |
|
411 | |
|
412 | |
|
413 | |
|
414 | |
|
415 | |
|
416 | |
|
417 | |
|
418 | |
|
419 | |
|
420 | |
protected ArtifactRepository getLocal() |
421 | |
{ |
422 | 86 | if ( this.overrideLocalRepository != null ) |
423 | |
{ |
424 | 36 | return this.overrideLocalRepository; |
425 | |
} |
426 | |
|
427 | 50 | ArtifactRepository local = super.getLocal(); |
428 | |
|
429 | 50 | if ( this.localRepositoryDirectory != null ) |
430 | |
{ |
431 | |
|
432 | 1 | String url = "file://" + this.localRepositoryDirectory.getAbsolutePath(); |
433 | 1 | this.overrideLocalRepository = |
434 | |
artifactRepositoryManager.createArtifactRepository( local.getId(), url, local.getLayout(), |
435 | |
local.getSnapshots(), local.getReleases() ); |
436 | |
|
437 | 1 | this.getLog().debug( "Execution local repository is at: " + this.overrideLocalRepository.getBasedir() ); |
438 | 1 | } |
439 | |
else |
440 | |
{ |
441 | 49 | this.overrideLocalRepository = local; |
442 | |
} |
443 | |
|
444 | 50 | return this.overrideLocalRepository; |
445 | |
} |
446 | |
|
447 | |
|
448 | |
|
449 | |
|
450 | |
public List<ArtifactItem> getArtifactItems() |
451 | |
{ |
452 | 2 | return this.artifactItems; |
453 | |
} |
454 | |
|
455 | |
|
456 | |
|
457 | |
|
458 | |
public void setArtifactItems( List<ArtifactItem> theArtifactItems ) |
459 | |
{ |
460 | 54 | this.artifactItems = theArtifactItems; |
461 | 54 | } |
462 | |
|
463 | |
|
464 | |
|
465 | |
|
466 | |
public File getOutputDirectory() |
467 | |
{ |
468 | 53 | return this.outputDirectory; |
469 | |
} |
470 | |
|
471 | |
|
472 | |
|
473 | |
|
474 | |
public void setOutputDirectory( File theOutputDirectory ) |
475 | |
{ |
476 | 53 | this.outputDirectory = theOutputDirectory; |
477 | 53 | } |
478 | |
|
479 | |
|
480 | |
|
481 | |
|
482 | |
public boolean isOverWriteIfNewer() |
483 | |
{ |
484 | 81 | return this.overWriteIfNewer; |
485 | |
} |
486 | |
|
487 | |
|
488 | |
|
489 | |
|
490 | |
public void setOverWriteIfNewer( boolean theOverWriteIfNewer ) |
491 | |
{ |
492 | 10 | this.overWriteIfNewer = theOverWriteIfNewer; |
493 | 10 | } |
494 | |
|
495 | |
|
496 | |
|
497 | |
|
498 | |
public boolean isOverWriteReleases() |
499 | |
{ |
500 | 81 | return this.overWriteReleases; |
501 | |
} |
502 | |
|
503 | |
|
504 | |
|
505 | |
|
506 | |
public void setOverWriteReleases( boolean theOverWriteReleases ) |
507 | |
{ |
508 | 4 | this.overWriteReleases = theOverWriteReleases; |
509 | 4 | } |
510 | |
|
511 | |
|
512 | |
|
513 | |
|
514 | |
public boolean isOverWriteSnapshots() |
515 | |
{ |
516 | 81 | return this.overWriteSnapshots; |
517 | |
} |
518 | |
|
519 | |
|
520 | |
|
521 | |
|
522 | |
public void setOverWriteSnapshots( boolean theOverWriteSnapshots ) |
523 | |
{ |
524 | 2 | this.overWriteSnapshots = theOverWriteSnapshots; |
525 | 2 | } |
526 | |
|
527 | |
public void setLocalRepositoryDirectory( File localRepositoryDirectory ) |
528 | |
{ |
529 | 1 | this.localRepositoryDirectory = localRepositoryDirectory; |
530 | 1 | } |
531 | |
|
532 | |
public boolean isSkip() |
533 | |
{ |
534 | 60 | return skip; |
535 | |
} |
536 | |
|
537 | |
public void setSkip( boolean skip ) |
538 | |
{ |
539 | 2 | this.skip = skip; |
540 | 2 | } |
541 | |
|
542 | |
public void setArtifact( String artifact ) |
543 | |
throws MojoFailureException |
544 | |
{ |
545 | 0 | if ( artifact != null ) |
546 | |
{ |
547 | 0 | String packaging = "jar"; |
548 | |
String classifier; |
549 | 0 | String[] tokens = StringUtils.split( artifact, ":" ); |
550 | 0 | if ( tokens.length < 3 || tokens.length > 5 ) |
551 | |
{ |
552 | 0 | throw new MojoFailureException( |
553 | |
"Invalid artifact, you must specify groupId:artifactId:version[:packaging][:classifier] " |
554 | |
+ artifact ); |
555 | |
} |
556 | 0 | String groupId = tokens[0]; |
557 | 0 | String artifactId = tokens[1]; |
558 | 0 | String version = tokens[2]; |
559 | 0 | if ( tokens.length >= 4 ) |
560 | |
{ |
561 | 0 | packaging = tokens[3]; |
562 | |
} |
563 | 0 | if ( tokens.length == 5 ) |
564 | |
{ |
565 | 0 | classifier = tokens[4]; |
566 | |
} |
567 | |
else |
568 | |
{ |
569 | 0 | classifier = null; |
570 | |
} |
571 | |
|
572 | 0 | Artifact toUnpack = classifier == null |
573 | |
? artifactFactory.createBuildArtifact( groupId, artifactId, version, packaging ) |
574 | |
: artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, packaging, classifier ); |
575 | |
|
576 | 0 | setArtifactItems( Collections.singletonList( new ArtifactItem( toUnpack ) ) ); |
577 | |
} |
578 | 0 | } |
579 | |
} |