1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.dependency.fromConfiguration;
20
21 import java.io.File;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.Objects;
25
26 import org.apache.commons.lang3.StringUtils;
27 import org.apache.maven.artifact.Artifact;
28 import org.apache.maven.artifact.handler.ArtifactHandler;
29 import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
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.plugins.annotations.Component;
34 import org.apache.maven.plugins.annotations.Parameter;
35 import org.apache.maven.plugins.dependency.AbstractDependencyMojo;
36 import org.apache.maven.plugins.dependency.utils.DependencyUtil;
37 import org.apache.maven.plugins.dependency.utils.filters.ArtifactItemFilter;
38 import org.apache.maven.project.MavenProject;
39 import org.apache.maven.project.ProjectBuildingRequest;
40 import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException;
41 import org.apache.maven.shared.transfer.artifact.DefaultArtifactCoordinate;
42 import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver;
43 import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolverException;
44 import org.apache.maven.shared.transfer.repository.RepositoryManager;
45
46
47
48
49
50
51
52
53 public abstract class AbstractFromConfigurationMojo extends AbstractDependencyMojo {
54
55
56
57
58
59 @Parameter(property = "outputDirectory", defaultValue = "${project.build.directory}/dependency")
60 private File outputDirectory;
61
62
63
64
65
66
67 @Parameter(property = "mdep.overWriteReleases", defaultValue = "false")
68 private boolean overWriteReleases;
69
70
71
72
73
74
75 @Parameter(property = "mdep.overWriteSnapshots", defaultValue = "false")
76 private boolean overWriteSnapshots;
77
78
79
80
81
82
83 @Parameter(property = "mdep.overIfNewer", defaultValue = "true")
84 private boolean overWriteIfNewer;
85
86
87
88
89
90
91
92 @Parameter
93 private List<ArtifactItem> artifactItems;
94
95
96
97
98
99
100
101 @Parameter
102 private File localRepositoryDirectory;
103
104 @Component
105 private ArtifactResolver artifactResolver;
106
107 @Component
108 private RepositoryManager repositoryManager;
109
110 @Component
111 private ArtifactHandlerManager artifactHandlerManager;
112
113 abstract ArtifactItemFilter getMarkedArtifactFilter(ArtifactItem item);
114
115
116
117
118
119
120 protected void verifyRequirements() throws MojoFailureException {
121 if (artifactItems == null || artifactItems.isEmpty()) {
122 throw new MojoFailureException("Either artifact or artifactItems is required ");
123 }
124 }
125
126
127
128
129
130
131
132
133
134
135 protected List<ArtifactItem> getProcessedArtifactItems(ProcessArtifactItemsRequest processArtifactItemsRequest)
136 throws MojoExecutionException {
137
138 boolean removeVersion = processArtifactItemsRequest.isRemoveVersion(),
139 prependGroupId = processArtifactItemsRequest.isPrependGroupId(),
140 useBaseVersion = processArtifactItemsRequest.isUseBaseVersion();
141
142 boolean removeClassifier = processArtifactItemsRequest.isRemoveClassifier();
143
144 if (artifactItems == null || artifactItems.size() < 1) {
145 throw new MojoExecutionException("There are no artifactItems configured.");
146 }
147
148 for (ArtifactItem artifactItem : artifactItems) {
149 this.getLog().info("Configured Artifact: " + artifactItem.toString());
150
151 if (artifactItem.getOutputDirectory() == null) {
152 artifactItem.setOutputDirectory(this.outputDirectory);
153 }
154 artifactItem.getOutputDirectory().mkdirs();
155
156
157 if (StringUtils.isEmpty(artifactItem.getVersion())) {
158 fillMissingArtifactVersion(artifactItem);
159 }
160
161 artifactItem.setArtifact(this.getArtifact(artifactItem));
162
163 if (StringUtils.isEmpty(artifactItem.getDestFileName())) {
164 artifactItem.setDestFileName(DependencyUtil.getFormattedFileName(
165 artifactItem.getArtifact(), removeVersion, prependGroupId, useBaseVersion, removeClassifier));
166 }
167
168 try {
169 artifactItem.setNeedsProcessing(checkIfProcessingNeeded(artifactItem));
170 } catch (ArtifactFilterException e) {
171 throw new MojoExecutionException(e.getMessage(), e);
172 }
173 }
174 return artifactItems;
175 }
176
177 private boolean checkIfProcessingNeeded(ArtifactItem item) throws MojoExecutionException, ArtifactFilterException {
178 return StringUtils.equalsIgnoreCase(item.getOverWrite(), "true")
179 || getMarkedArtifactFilter(item).isArtifactIncluded(item);
180 }
181
182
183
184
185
186
187
188
189
190 protected Artifact getArtifact(ArtifactItem artifactItem) throws MojoExecutionException {
191 Artifact artifact;
192
193 try {
194
195
196
197
198
199
200
201
202
203
204 ProjectBuildingRequest buildingRequest = newResolveArtifactProjectBuildingRequest();
205
206 if (localRepositoryDirectory != null) {
207 buildingRequest =
208 repositoryManager.setLocalRepositoryBasedir(buildingRequest, localRepositoryDirectory);
209 }
210
211
212 DefaultArtifactCoordinate coordinate = new DefaultArtifactCoordinate();
213 coordinate.setGroupId(artifactItem.getGroupId());
214 coordinate.setArtifactId(artifactItem.getArtifactId());
215 coordinate.setVersion(artifactItem.getVersion());
216 coordinate.setClassifier(artifactItem.getClassifier());
217
218 final String extension;
219 ArtifactHandler artifactHandler = artifactHandlerManager.getArtifactHandler(artifactItem.getType());
220 if (artifactHandler != null) {
221 extension = artifactHandler.getExtension();
222 } else {
223 extension = artifactItem.getType();
224 }
225 coordinate.setExtension(extension);
226
227 artifact = artifactResolver
228 .resolveArtifact(buildingRequest, coordinate)
229 .getArtifact();
230 } catch (ArtifactResolverException e) {
231 throw new MojoExecutionException("Unable to find/resolve artifact.", e);
232 }
233
234 return artifact;
235 }
236
237
238
239
240
241
242
243
244
245 private void fillMissingArtifactVersion(ArtifactItem artifact) throws MojoExecutionException {
246 MavenProject project = getProject();
247 List<Dependency> deps = project.getDependencies();
248 List<Dependency> depMngt = project.getDependencyManagement() == null
249 ? Collections.emptyList()
250 : project.getDependencyManagement().getDependencies();
251
252 if (!findDependencyVersion(artifact, deps, false)
253 && (project.getDependencyManagement() == null || !findDependencyVersion(artifact, depMngt, false))
254 && !findDependencyVersion(artifact, deps, true)
255 && (project.getDependencyManagement() == null || !findDependencyVersion(artifact, depMngt, true))) {
256 throw new MojoExecutionException("Unable to find artifact version of " + artifact.getGroupId() + ":"
257 + artifact.getArtifactId() + " in either dependency list or in project's dependency management.");
258 }
259 }
260
261
262
263
264
265
266
267
268
269
270 private boolean findDependencyVersion(ArtifactItem artifact, List<Dependency> dependencies, boolean looseMatch) {
271 for (Dependency dependency : dependencies) {
272 if (Objects.equals(dependency.getArtifactId(), artifact.getArtifactId())
273 && Objects.equals(dependency.getGroupId(), artifact.getGroupId())
274 && (looseMatch || Objects.equals(dependency.getClassifier(), artifact.getClassifier()))
275 && (looseMatch || Objects.equals(dependency.getType(), artifact.getType()))) {
276 artifact.setVersion(dependency.getVersion());
277
278 return true;
279 }
280 }
281
282 return false;
283 }
284
285
286
287
288 public List<ArtifactItem> getArtifactItems() {
289 return this.artifactItems;
290 }
291
292
293
294
295 public void setArtifactItems(List<ArtifactItem> theArtifactItems) {
296 this.artifactItems = theArtifactItems;
297 }
298
299
300
301
302 public File getOutputDirectory() {
303 return this.outputDirectory;
304 }
305
306
307
308
309 public void setOutputDirectory(File theOutputDirectory) {
310 this.outputDirectory = theOutputDirectory;
311 }
312
313
314
315
316 public boolean isOverWriteIfNewer() {
317 return this.overWriteIfNewer;
318 }
319
320
321
322
323 public void setOverWriteIfNewer(boolean theOverWriteIfNewer) {
324 this.overWriteIfNewer = theOverWriteIfNewer;
325 }
326
327
328
329
330 public boolean isOverWriteReleases() {
331 return this.overWriteReleases;
332 }
333
334
335
336
337 public void setOverWriteReleases(boolean theOverWriteReleases) {
338 this.overWriteReleases = theOverWriteReleases;
339 }
340
341
342
343
344 public boolean isOverWriteSnapshots() {
345 return this.overWriteSnapshots;
346 }
347
348
349
350
351 public void setOverWriteSnapshots(boolean theOverWriteSnapshots) {
352 this.overWriteSnapshots = theOverWriteSnapshots;
353 }
354
355
356
357
358 public void setLocalRepositoryDirectory(File localRepositoryDirectory) {
359 this.localRepositoryDirectory = localRepositoryDirectory;
360 }
361
362
363
364
365
366 public void setArtifact(String artifact) throws MojoFailureException {
367 if (artifact != null) {
368 String packaging = "jar";
369 String classifier;
370 String[] tokens = StringUtils.split(artifact, ":");
371 if (tokens.length < 3 || tokens.length > 5) {
372 throw new MojoFailureException("Invalid artifact, "
373 + "you must specify groupId:artifactId:version[:packaging[:classifier]] " + artifact);
374 }
375 String groupId = tokens[0];
376 String artifactId = tokens[1];
377 String version = tokens[2];
378 if (tokens.length >= 4) {
379 packaging = tokens[3];
380 }
381 if (tokens.length == 5) {
382 classifier = tokens[4];
383 } else {
384 classifier = null;
385 }
386
387 ArtifactItem artifactItem = new ArtifactItem();
388 artifactItem.setGroupId(groupId);
389 artifactItem.setArtifactId(artifactId);
390 artifactItem.setVersion(version);
391 artifactItem.setType(packaging);
392 artifactItem.setClassifier(classifier);
393
394 setArtifactItems(Collections.singletonList(artifactItem));
395 }
396 }
397 }