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.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
26
27 import org.apache.maven.artifact.Artifact;
28 import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
29 import org.apache.maven.artifact.resolver.ArtifactResolutionException;
30 import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
31 import org.apache.maven.artifact.versioning.VersionRange;
32 import org.apache.maven.model.Dependency;
33 import org.apache.maven.plugin.MojoExecutionException;
34 import org.apache.maven.plugin.dependency.AbstractDependencyMojo;
35 import org.apache.maven.plugin.dependency.utils.DependencyUtil;
36 import org.apache.maven.plugin.dependency.utils.filters.ArtifactItemFilter;
37 import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException;
38 import org.codehaus.plexus.util.StringUtils;
39
40
41
42
43
44
45
46
47
48
49 public abstract class AbstractFromConfigurationMojo
50 extends AbstractDependencyMojo
51 {
52
53
54
55
56
57
58
59
60
61 private File outputDirectory;
62
63
64
65
66
67
68
69
70 private boolean overWriteReleases;
71
72
73
74
75
76
77
78
79 private boolean overWriteSnapshots;
80
81
82
83
84
85
86
87
88 private boolean overWriteIfNewer;
89
90
91
92
93
94
95
96
97
98
99 private ArrayList artifactItems;
100
101 abstract ArtifactItemFilter getMarkedArtifactFilter( ArtifactItem item );
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117 protected ArrayList getProcessedArtifactItems( boolean removeVersion )
118 throws MojoExecutionException
119 {
120 if ( artifactItems == null || artifactItems.size() < 1 )
121 {
122 throw new MojoExecutionException( "There are no artifactItems configured." );
123 }
124
125 Iterator iter = artifactItems.iterator();
126 while ( iter.hasNext() )
127 {
128 ArtifactItem artifactItem = (ArtifactItem) iter.next();
129 this.getLog().info( "Configured Artifact: " + artifactItem.toString() );
130
131 if ( artifactItem.getOutputDirectory() == null )
132 {
133 artifactItem.setOutputDirectory( this.outputDirectory );
134 }
135 artifactItem.getOutputDirectory().mkdirs();
136
137
138 if ( StringUtils.isEmpty( artifactItem.getVersion() ) )
139 {
140 fillMissingArtifactVersion( artifactItem );
141 }
142
143 artifactItem.setArtifact( this.getArtifact( artifactItem ) );
144
145 if ( StringUtils.isEmpty( artifactItem.getDestFileName() ) )
146 {
147 artifactItem.setDestFileName( DependencyUtil.getFormattedFileName( artifactItem.getArtifact(),
148 removeVersion ) );
149 }
150
151 try
152 {
153 artifactItem.setNeedsProcessing( checkIfProcessingNeeded( artifactItem ) );
154 }
155 catch ( ArtifactFilterException e )
156 {
157 throw new MojoExecutionException (e.getMessage(),e);
158 }
159 }
160 return artifactItems;
161 }
162
163 private boolean checkIfProcessingNeeded( ArtifactItem item )
164 throws MojoExecutionException, ArtifactFilterException
165 {
166 boolean result = false;
167 if ( StringUtils.equalsIgnoreCase( item.getOverWrite(), "true" ) )
168 {
169 result = true;
170 }
171 else
172 {
173 ArtifactItemFilter filter = getMarkedArtifactFilter( item );
174 result = filter.isArtifactIncluded( item );
175 }
176 return result;
177 }
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193 protected Artifact getArtifact( ArtifactItem artifactItem )
194 throws MojoExecutionException
195 {
196 Artifact artifact;
197
198
199
200 VersionRange vr;
201 try
202 {
203 vr = VersionRange.createFromVersionSpec( artifactItem.getVersion() );
204 }
205 catch ( InvalidVersionSpecificationException e1 )
206 {
207
208 e1.printStackTrace();
209 vr = VersionRange.createFromVersion( artifactItem.getVersion() );
210 }
211
212 if ( StringUtils.isEmpty( artifactItem.getClassifier() ) )
213 {
214 artifact = factory.createDependencyArtifact( artifactItem.getGroupId(), artifactItem.getArtifactId(), vr,
215 artifactItem.getType(), null, Artifact.SCOPE_COMPILE );
216 }
217 else
218 {
219 artifact = factory.createDependencyArtifact( artifactItem.getGroupId(), artifactItem.getArtifactId(), vr,
220 artifactItem.getType(), artifactItem.getClassifier(),
221 Artifact.SCOPE_COMPILE );
222 }
223
224 try
225 {
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242 resolver.resolve( artifact, remoteRepos, local );
243 }
244 catch ( ArtifactResolutionException e )
245 {
246 throw new MojoExecutionException( "Unable to resolve artifact.", e );
247 }
248 catch ( ArtifactNotFoundException e )
249 {
250 throw new MojoExecutionException( "Unable to find artifact.", e );
251 }
252
253 return artifact;
254 }
255
256
257
258
259
260
261
262
263
264
265
266 private void fillMissingArtifactVersion( ArtifactItem artifact )
267 throws MojoExecutionException
268 {
269 if ( !findDependencyVersion( artifact, project.getDependencies(), false )
270 && ( project.getDependencyManagement() == null || !findDependencyVersion( artifact, project
271 .getDependencyManagement().getDependencies(), false ) )
272 && !findDependencyVersion( artifact, project.getDependencies(), true )
273 && ( project.getDependencyManagement() == null || !findDependencyVersion( artifact, project
274 .getDependencyManagement().getDependencies(), true ) ) )
275 {
276 throw new MojoExecutionException( "Unable to find artifact version of " + artifact.getGroupId() + ":"
277 + artifact.getArtifactId() + " in either dependency list or in project's dependency management." );
278 }
279 }
280
281
282
283
284
285
286
287
288
289
290
291
292
293 private boolean findDependencyVersion( ArtifactItem artifact, List list, boolean looseMatch )
294 {
295 boolean result = false;
296
297 for ( int i = 0; i < list.size(); i++ )
298 {
299 Dependency dependency = (Dependency) list.get( i );
300 if ( StringUtils.equals( dependency.getArtifactId(), artifact.getArtifactId() )
301 && StringUtils.equals( dependency.getGroupId(), artifact.getGroupId() )
302 && ( looseMatch || StringUtils.equals( dependency.getClassifier(), artifact.getClassifier() ) )
303 && ( looseMatch || StringUtils.equals( dependency.getType(), artifact.getType() ) ) )
304 {
305
306 artifact.setVersion( dependency.getVersion() );
307
308 result = true;
309 break;
310 }
311 }
312
313 return result;
314 }
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353 public ArrayList getArtifactItems()
354 {
355 return this.artifactItems;
356 }
357
358
359
360
361
362 public void setArtifactItems( ArrayList theArtifactItems )
363 {
364 this.artifactItems = theArtifactItems;
365 }
366
367
368
369
370 public File getOutputDirectory()
371 {
372 return this.outputDirectory;
373 }
374
375
376
377
378
379 public void setOutputDirectory( File theOutputDirectory )
380 {
381 this.outputDirectory = theOutputDirectory;
382 }
383
384
385
386
387 public boolean isOverWriteIfNewer()
388 {
389 return this.overWriteIfNewer;
390 }
391
392
393
394
395
396 public void setOverWriteIfNewer( boolean theOverWriteIfNewer )
397 {
398 this.overWriteIfNewer = theOverWriteIfNewer;
399 }
400
401
402
403
404 public boolean isOverWriteReleases()
405 {
406 return this.overWriteReleases;
407 }
408
409
410
411
412
413 public void setOverWriteReleases( boolean theOverWriteReleases )
414 {
415 this.overWriteReleases = theOverWriteReleases;
416 }
417
418
419
420
421 public boolean isOverWriteSnapshots()
422 {
423 return this.overWriteSnapshots;
424 }
425
426
427
428
429
430 public void setOverWriteSnapshots( boolean theOverWriteSnapshots )
431 {
432 this.overWriteSnapshots = theOverWriteSnapshots;
433 }
434 }