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