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
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
102
103
104 @Parameter
105 private File localRepositoryDirectory;
106
107 @Component
108 private ArtifactResolver artifactResolver;
109
110 @Component
111 private RepositoryManager repositoryManager;
112
113 @Component
114 private ArtifactHandlerManager artifactHandlerManager;
115
116 abstract ArtifactItemFilter getMarkedArtifactFilter( ArtifactItem item );
117
118
119 protected void verifyRequirements()
120 throws MojoFailureException
121 {
122 if ( artifactItems == null || artifactItems.isEmpty() )
123 {
124 throw new MojoFailureException( "Either artifact or artifactItems is required " );
125 }
126 }
127
128
129
130
131
132
133
134
135
136
137 protected List<ArtifactItem> getProcessedArtifactItems( ProcessArtifactItemsRequest processArtifactItemsRequest )
138 throws MojoExecutionException
139 {
140
141 boolean removeVersion = processArtifactItemsRequest.isRemoveVersion(),
142 prependGroupId = processArtifactItemsRequest.isPrependGroupId(),
143 useBaseVersion = processArtifactItemsRequest.isUseBaseVersion();
144
145 boolean removeClassifier = processArtifactItemsRequest.isRemoveClassifier();
146
147 if ( artifactItems == null || artifactItems.size() < 1 )
148 {
149 throw new MojoExecutionException( "There are no artifactItems configured." );
150 }
151
152 for ( ArtifactItem artifactItem : artifactItems )
153 {
154 this.getLog().info( "Configured Artifact: " + artifactItem.toString() );
155
156 if ( artifactItem.getOutputDirectory() == null )
157 {
158 artifactItem.setOutputDirectory( this.outputDirectory );
159 }
160 artifactItem.getOutputDirectory().mkdirs();
161
162
163 if ( StringUtils.isEmpty( artifactItem.getVersion() ) )
164 {
165 fillMissingArtifactVersion( artifactItem );
166 }
167
168 artifactItem.setArtifact( this.getArtifact( artifactItem ) );
169
170 if ( StringUtils.isEmpty( artifactItem.getDestFileName() ) )
171 {
172 artifactItem.setDestFileName( DependencyUtil.getFormattedFileName( artifactItem.getArtifact(),
173 removeVersion, prependGroupId,
174 useBaseVersion, removeClassifier ) );
175 }
176
177 try
178 {
179 artifactItem.setNeedsProcessing( checkIfProcessingNeeded( artifactItem ) );
180 }
181 catch ( ArtifactFilterException e )
182 {
183 throw new MojoExecutionException( e.getMessage(), e );
184 }
185 }
186 return artifactItems;
187 }
188
189 private boolean checkIfProcessingNeeded( ArtifactItem item )
190 throws MojoExecutionException, ArtifactFilterException
191 {
192 return StringUtils.equalsIgnoreCase( item.getOverWrite(), "true" )
193 || getMarkedArtifactFilter( item ).isArtifactIncluded( item );
194 }
195
196
197
198
199
200
201
202
203
204 protected Artifact getArtifact( ArtifactItem artifactItem )
205 throws MojoExecutionException
206 {
207 Artifact artifact;
208
209 try
210 {
211
212
213
214
215
216
217
218
219
220
221 ProjectBuildingRequest buildingRequest = newResolveArtifactProjectBuildingRequest();
222
223 if ( localRepositoryDirectory != null )
224 {
225 buildingRequest =
226 repositoryManager.setLocalRepositoryBasedir( buildingRequest, localRepositoryDirectory );
227 }
228
229
230 DefaultArtifactCoordinate coordinate = new DefaultArtifactCoordinate();
231 coordinate.setGroupId( artifactItem.getGroupId() );
232 coordinate.setArtifactId( artifactItem.getArtifactId() );
233 coordinate.setVersion( artifactItem.getVersion() );
234 coordinate.setClassifier( artifactItem.getClassifier() );
235
236 final String extension;
237 ArtifactHandler artifactHandler = artifactHandlerManager.getArtifactHandler( artifactItem.getType() );
238 if ( artifactHandler != null )
239 {
240 extension = artifactHandler.getExtension();
241 }
242 else
243 {
244 extension = artifactItem.getType();
245 }
246 coordinate.setExtension( extension );
247
248 artifact = artifactResolver.resolveArtifact( buildingRequest, coordinate ).getArtifact();
249 }
250 catch ( ArtifactResolverException e )
251 {
252 throw new MojoExecutionException( "Unable to find/resolve artifact.", e );
253 }
254
255 return artifact;
256 }
257
258
259
260
261
262
263
264
265
266 private void fillMissingArtifactVersion( ArtifactItem artifact )
267 throws MojoExecutionException
268 {
269 MavenProject project = getProject();
270 List<Dependency> deps = project.getDependencies();
271 List<Dependency> depMngt = project.getDependencyManagement() == null ? Collections.<Dependency>emptyList()
272 : project.getDependencyManagement().getDependencies();
273
274 if ( !findDependencyVersion( artifact, deps, false )
275 && ( project.getDependencyManagement() == null || !findDependencyVersion( artifact, depMngt, false ) )
276 && !findDependencyVersion( artifact, deps, true )
277 && ( project.getDependencyManagement() == null || !findDependencyVersion( artifact, depMngt, true ) ) )
278 {
279 throw new MojoExecutionException( "Unable to find artifact version of " + artifact.getGroupId() + ":"
280 + artifact.getArtifactId() + " in either dependency list or in project's dependency management." );
281 }
282 }
283
284
285
286
287
288
289
290
291
292
293 private boolean findDependencyVersion( ArtifactItem artifact, List<Dependency> dependencies, boolean looseMatch )
294 {
295 for ( Dependency dependency : dependencies )
296 {
297 if ( StringUtils.equals( dependency.getArtifactId(), artifact.getArtifactId() )
298 && StringUtils.equals( dependency.getGroupId(), artifact.getGroupId() )
299 && ( looseMatch || StringUtils.equals( dependency.getClassifier(), artifact.getClassifier() ) )
300 && ( looseMatch || StringUtils.equals( dependency.getType(), artifact.getType() ) ) )
301 {
302 artifact.setVersion( dependency.getVersion() );
303
304 return true;
305 }
306 }
307
308 return false;
309 }
310
311
312
313
314 public List<ArtifactItem> getArtifactItems()
315 {
316 return this.artifactItems;
317 }
318
319
320
321
322 public void setArtifactItems( List<ArtifactItem> theArtifactItems )
323 {
324 this.artifactItems = theArtifactItems;
325 }
326
327
328
329
330 public File getOutputDirectory()
331 {
332 return this.outputDirectory;
333 }
334
335
336
337
338 public void setOutputDirectory( File theOutputDirectory )
339 {
340 this.outputDirectory = theOutputDirectory;
341 }
342
343
344
345
346 public boolean isOverWriteIfNewer()
347 {
348 return this.overWriteIfNewer;
349 }
350
351
352
353
354 public void setOverWriteIfNewer( boolean theOverWriteIfNewer )
355 {
356 this.overWriteIfNewer = theOverWriteIfNewer;
357 }
358
359
360
361
362 public boolean isOverWriteReleases()
363 {
364 return this.overWriteReleases;
365 }
366
367
368
369
370 public void setOverWriteReleases( boolean theOverWriteReleases )
371 {
372 this.overWriteReleases = theOverWriteReleases;
373 }
374
375
376
377
378 public boolean isOverWriteSnapshots()
379 {
380 return this.overWriteSnapshots;
381 }
382
383
384
385
386 public void setOverWriteSnapshots( boolean theOverWriteSnapshots )
387 {
388 this.overWriteSnapshots = theOverWriteSnapshots;
389 }
390
391 public void setLocalRepositoryDirectory( File localRepositoryDirectory )
392 {
393 this.localRepositoryDirectory = localRepositoryDirectory;
394 }
395
396 public void setArtifact( String artifact )
397 throws MojoFailureException
398 {
399 if ( artifact != null )
400 {
401 String packaging = "jar";
402 String classifier;
403 String[] tokens = StringUtils.split( artifact, ":" );
404 if ( tokens.length < 3 || tokens.length > 5 )
405 {
406 throw new MojoFailureException( "Invalid artifact, "
407 + "you must specify groupId:artifactId:version[:packaging[:classifier]] " + artifact );
408 }
409 String groupId = tokens[0];
410 String artifactId = tokens[1];
411 String version = tokens[2];
412 if ( tokens.length >= 4 )
413 {
414 packaging = tokens[3];
415 }
416 if ( tokens.length == 5 )
417 {
418 classifier = tokens[4];
419 }
420 else
421 {
422 classifier = null;
423 }
424
425 ArtifactItem artifactItem = new ArtifactItem();
426 artifactItem.setGroupId( groupId );
427 artifactItem.setArtifactId( artifactId );
428 artifactItem.setVersion( version );
429 artifactItem.setType( packaging );
430 artifactItem.setClassifier( classifier );
431
432 setArtifactItems( Collections.singletonList( artifactItem ) );
433 }
434 }
435 }