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