1 package org.apache.maven.plugins.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
26 import org.apache.maven.artifact.Artifact;
27 import org.apache.maven.artifact.handler.ArtifactHandler;
28 import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
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.plugins.annotations.Component;
33 import org.apache.maven.plugins.annotations.Parameter;
34 import org.apache.maven.plugins.dependency.AbstractDependencyMojo;
35 import org.apache.maven.plugins.dependency.utils.DependencyUtil;
36 import org.apache.maven.plugins.dependency.utils.filters.ArtifactItemFilter;
37 import org.apache.maven.project.MavenProject;
38 import org.apache.maven.project.ProjectBuildingRequest;
39 import org.apache.maven.shared.artifact.DefaultArtifactCoordinate;
40 import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException;
41 import org.apache.maven.shared.repository.RepositoryManager;
42 import org.apache.maven.shared.artifact.resolve.ArtifactResolver;
43 import org.apache.maven.shared.artifact.resolve.ArtifactResolverException;
44 import org.codehaus.plexus.util.StringUtils;
45
46
47
48
49
50
51
52
53 public abstract class AbstractFromConfigurationMojo
54 extends AbstractDependencyMojo
55 {
56
57
58
59
60
61 @Parameter( property = "outputDirectory", defaultValue = "${project.build.directory}/dependency" )
62 private File outputDirectory;
63
64
65
66
67
68
69 @Parameter( property = "mdep.overWriteReleases", defaultValue = "false" )
70 private boolean overWriteReleases;
71
72
73
74
75
76
77 @Parameter( property = "mdep.overWriteSnapshots", defaultValue = "false" )
78 private boolean overWriteSnapshots;
79
80
81
82
83
84
85 @Parameter( property = "mdep.overIfNewer", defaultValue = "true" )
86 private boolean overWriteIfNewer;
87
88
89
90
91
92
93
94 @Parameter
95 private List<ArtifactItem> artifactItems;
96
97
98
99
100
101
102
103 @Parameter
104 private File localRepositoryDirectory;
105
106 @Component
107 private ArtifactResolver artifactResolver;
108
109 @Component
110 private RepositoryManager repositoryManager;
111
112 @Component
113 private ArtifactHandlerManager artifactHandlerManager;
114
115 abstract ArtifactItemFilter getMarkedArtifactFilter( ArtifactItem item );
116
117
118
119
120
121
122 protected void verifyRequirements()
123 throws MojoFailureException
124 {
125 if ( artifactItems == null || artifactItems.isEmpty() )
126 {
127 throw new MojoFailureException( "Either artifact or artifactItems is required " );
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(),
145 prependGroupId = processArtifactItemsRequest.isPrependGroupId(),
146 useBaseVersion = 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 try
213 {
214
215
216
217
218
219
220
221
222
223
224 ProjectBuildingRequest buildingRequest = newResolveArtifactProjectBuildingRequest();
225
226 if ( localRepositoryDirectory != null )
227 {
228 buildingRequest =
229 repositoryManager.setLocalRepositoryBasedir( buildingRequest, localRepositoryDirectory );
230 }
231
232
233 DefaultArtifactCoordinate coordinate = new DefaultArtifactCoordinate();
234 coordinate.setGroupId( artifactItem.getGroupId() );
235 coordinate.setArtifactId( artifactItem.getArtifactId() );
236 coordinate.setVersion( artifactItem.getVersion() );
237 coordinate.setClassifier( artifactItem.getClassifier() );
238
239 final String extension;
240 ArtifactHandler artifactHandler = artifactHandlerManager.getArtifactHandler( artifactItem.getType() );
241 if ( artifactHandler != null )
242 {
243 extension = artifactHandler.getExtension();
244 }
245 else
246 {
247 extension = artifactItem.getType();
248 }
249 coordinate.setExtension( extension );
250
251 artifact = artifactResolver.resolveArtifact( buildingRequest, coordinate ).getArtifact();
252 }
253 catch ( ArtifactResolverException e )
254 {
255 throw new MojoExecutionException( "Unable to find/resolve artifact.", e );
256 }
257
258 return artifact;
259 }
260
261
262
263
264
265
266
267
268
269 private void fillMissingArtifactVersion( ArtifactItem artifact )
270 throws MojoExecutionException
271 {
272 MavenProject project = getProject();
273 List<Dependency> deps = project.getDependencies();
274 List<Dependency> depMngt = project.getDependencyManagement() == null ? Collections.<Dependency>emptyList()
275 : project.getDependencyManagement().getDependencies();
276
277 if ( !findDependencyVersion( artifact, deps, false )
278 && ( project.getDependencyManagement() == null || !findDependencyVersion( artifact, depMngt, false ) )
279 && !findDependencyVersion( artifact, deps, true )
280 && ( project.getDependencyManagement() == null || !findDependencyVersion( artifact, depMngt, true ) ) )
281 {
282 throw new MojoExecutionException( "Unable to find artifact version of " + artifact.getGroupId() + ":"
283 + artifact.getArtifactId() + " in either dependency list or in project's dependency management." );
284 }
285 }
286
287
288
289
290
291
292
293
294
295
296 private boolean findDependencyVersion( ArtifactItem artifact, List<Dependency> dependencies, boolean looseMatch )
297 {
298 for ( Dependency dependency : dependencies )
299 {
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 artifact.setVersion( dependency.getVersion() );
306
307 return true;
308 }
309 }
310
311 return false;
312 }
313
314
315
316
317 public List<ArtifactItem> getArtifactItems()
318 {
319 return this.artifactItems;
320 }
321
322
323
324
325 public void setArtifactItems( List<ArtifactItem> theArtifactItems )
326 {
327 this.artifactItems = theArtifactItems;
328 }
329
330
331
332
333 public File getOutputDirectory()
334 {
335 return this.outputDirectory;
336 }
337
338
339
340
341 public void setOutputDirectory( File theOutputDirectory )
342 {
343 this.outputDirectory = theOutputDirectory;
344 }
345
346
347
348
349 public boolean isOverWriteIfNewer()
350 {
351 return this.overWriteIfNewer;
352 }
353
354
355
356
357 public void setOverWriteIfNewer( boolean theOverWriteIfNewer )
358 {
359 this.overWriteIfNewer = theOverWriteIfNewer;
360 }
361
362
363
364
365 public boolean isOverWriteReleases()
366 {
367 return this.overWriteReleases;
368 }
369
370
371
372
373 public void setOverWriteReleases( boolean theOverWriteReleases )
374 {
375 this.overWriteReleases = theOverWriteReleases;
376 }
377
378
379
380
381 public boolean isOverWriteSnapshots()
382 {
383 return this.overWriteSnapshots;
384 }
385
386
387
388
389 public void setOverWriteSnapshots( boolean theOverWriteSnapshots )
390 {
391 this.overWriteSnapshots = theOverWriteSnapshots;
392 }
393
394
395
396
397 public void setLocalRepositoryDirectory( File localRepositoryDirectory )
398 {
399 this.localRepositoryDirectory = localRepositoryDirectory;
400 }
401
402
403
404
405
406 public void setArtifact( String artifact )
407 throws MojoFailureException
408 {
409 if ( artifact != null )
410 {
411 String packaging = "jar";
412 String classifier;
413 String[] tokens = StringUtils.split( artifact, ":" );
414 if ( tokens.length < 3 || tokens.length > 5 )
415 {
416 throw new MojoFailureException( "Invalid artifact, "
417 + "you must specify groupId:artifactId:version[:packaging[:classifier]] " + artifact );
418 }
419 String groupId = tokens[0];
420 String artifactId = tokens[1];
421 String version = tokens[2];
422 if ( tokens.length >= 4 )
423 {
424 packaging = tokens[3];
425 }
426 if ( tokens.length == 5 )
427 {
428 classifier = tokens[4];
429 }
430 else
431 {
432 classifier = null;
433 }
434
435 ArtifactItem artifactItem = new ArtifactItem();
436 artifactItem.setGroupId( groupId );
437 artifactItem.setArtifactId( artifactId );
438 artifactItem.setVersion( version );
439 artifactItem.setType( packaging );
440 artifactItem.setClassifier( classifier );
441
442 setArtifactItems( Collections.singletonList( artifactItem ) );
443 }
444 }
445 }