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