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