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.fromDependencies;
20
21 import java.io.File;
22 import java.util.Collections;
23 import java.util.Map;
24 import java.util.Set;
25 import org.apache.maven.artifact.Artifact;
26 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
27 import org.apache.maven.plugin.MojoExecutionException;
28 import org.apache.maven.plugins.annotations.Component;
29 import org.apache.maven.plugins.annotations.LifecyclePhase;
30 import org.apache.maven.plugins.annotations.Mojo;
31 import org.apache.maven.plugins.annotations.Parameter;
32 import org.apache.maven.plugins.annotations.ResolutionScope;
33 import org.apache.maven.plugins.dependency.utils.DependencyStatusSets;
34 import org.apache.maven.plugins.dependency.utils.DependencyUtil;
35 import org.apache.maven.plugins.dependency.utils.filters.DestFileFilter;
36 import org.apache.maven.project.ProjectBuildingRequest;
37 import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter;
38 import org.apache.maven.shared.transfer.artifact.DefaultArtifactCoordinate;
39 import org.apache.maven.shared.transfer.artifact.install.ArtifactInstaller;
40 import org.apache.maven.shared.transfer.artifact.install.ArtifactInstallerException;
41 import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolverException;
42
43
44
45
46
47
48
49
50 @Mojo(
51 name = "copy-dependencies",
52 requiresDependencyResolution = ResolutionScope.TEST,
53 defaultPhase = LifecyclePhase.PROCESS_SOURCES,
54 threadSafe = true)
55
56 public class CopyDependenciesMojo extends AbstractFromDependenciesMojo {
57
58
59
60
61
62 @Parameter(property = "mdep.copyPom", defaultValue = "false")
63 protected boolean copyPom = true;
64
65
66
67
68 @Component
69 private ArtifactInstaller installer;
70
71
72
73
74 @Component(role = ArtifactRepositoryLayout.class)
75 private Map<String, ArtifactRepositoryLayout> repositoryLayouts;
76
77
78
79
80
81
82
83 @Parameter(property = "mdep.useBaseVersion", defaultValue = "true")
84 protected boolean useBaseVersion = true;
85
86
87
88
89
90
91 @Parameter(property = "mdep.addParentPoms", defaultValue = "false")
92 protected boolean addParentPoms;
93
94
95
96
97 @Parameter
98 protected boolean useJvmChmod = true;
99
100
101
102
103 @Parameter
104 protected boolean ignorePermissions;
105
106
107
108
109
110
111
112
113 @Override
114 protected void doExecute() throws MojoExecutionException {
115 DependencyStatusSets dss = getDependencySets(this.failOnMissingClassifierArtifact, addParentPoms);
116 Set<Artifact> artifacts = dss.getResolvedDependencies();
117
118 if (!useRepositoryLayout) {
119 for (Artifact artifact : artifacts) {
120 copyArtifact(
121 artifact, isStripVersion(), this.prependGroupId, this.useBaseVersion, this.stripClassifier);
122 }
123 } else {
124 ProjectBuildingRequest buildingRequest = getRepositoryManager()
125 .setLocalRepositoryBasedir(session.getProjectBuildingRequest(), outputDirectory);
126
127 artifacts.forEach(artifact -> installArtifact(artifact, buildingRequest));
128 }
129
130 Set<Artifact> skippedArtifacts = dss.getSkippedDependencies();
131 for (Artifact artifact : skippedArtifacts) {
132 getLog().info(artifact.getId() + " already exists in destination.");
133 }
134
135 if (isCopyPom() && !useRepositoryLayout) {
136 copyPoms(getOutputDirectory(), artifacts, this.stripVersion);
137 copyPoms(getOutputDirectory(), skippedArtifacts, this.stripVersion, this.stripClassifier);
138
139
140
141
142 }
143 }
144
145
146
147
148
149
150
151 private void installArtifact(Artifact artifact, ProjectBuildingRequest buildingRequest) {
152 try {
153 installer.install(buildingRequest, Collections.singletonList(artifact));
154 installBaseSnapshot(artifact, buildingRequest);
155
156 if (!"pom".equals(artifact.getType()) && isCopyPom()) {
157 Artifact pomArtifact = getResolvedPomArtifact(artifact);
158 if (pomArtifact != null
159 && pomArtifact.getFile() != null
160 && pomArtifact.getFile().exists()) {
161 installer.install(buildingRequest, Collections.singletonList(pomArtifact));
162 installBaseSnapshot(pomArtifact, buildingRequest);
163 }
164 }
165 } catch (ArtifactInstallerException e) {
166 getLog().warn("unable to install " + artifact, e);
167 }
168 }
169
170 private void installBaseSnapshot(Artifact artifact, ProjectBuildingRequest buildingRequest)
171 throws ArtifactInstallerException {
172 if (artifact.isSnapshot() && !artifact.getBaseVersion().equals(artifact.getVersion())) {
173 String version = artifact.getVersion();
174 try {
175 artifact.setVersion(artifact.getBaseVersion());
176 installer.install(buildingRequest, Collections.singletonList(artifact));
177 } finally {
178 artifact.setVersion(version);
179 }
180 }
181 }
182
183
184
185
186
187
188
189
190
191
192
193
194 protected void copyArtifact(
195 Artifact artifact, boolean removeVersion, boolean prependGroupId, boolean theUseBaseVersion)
196 throws MojoExecutionException {
197 copyArtifact(artifact, removeVersion, prependGroupId, theUseBaseVersion, false);
198 }
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213 protected void copyArtifact(
214 Artifact artifact,
215 boolean removeVersion,
216 boolean prependGroupId,
217 boolean theUseBaseVersion,
218 boolean removeClassifier)
219 throws MojoExecutionException {
220
221 String destFileName = DependencyUtil.getFormattedFileName(
222 artifact, removeVersion, prependGroupId, theUseBaseVersion, removeClassifier);
223
224 File destDir = DependencyUtil.getFormattedOutputDirectory(
225 useSubDirectoryPerScope,
226 useSubDirectoryPerType,
227 useSubDirectoryPerArtifact,
228 useRepositoryLayout,
229 stripVersion,
230 stripType,
231 outputDirectory,
232 artifact);
233 File destFile = new File(destDir, destFileName);
234
235 copyFile(artifact.getFile(), destFile);
236 }
237
238
239
240
241
242
243
244
245
246 public void copyPoms(File destDir, Set<Artifact> artifacts, boolean removeVersion) throws MojoExecutionException {
247
248 copyPoms(destDir, artifacts, removeVersion, false);
249 }
250
251
252
253
254
255
256
257
258
259
260 public void copyPoms(File destDir, Set<Artifact> artifacts, boolean removeVersion, boolean removeClassifier)
261 throws MojoExecutionException {
262
263 for (Artifact artifact : artifacts) {
264 Artifact pomArtifact = getResolvedPomArtifact(artifact);
265
266
267 if (pomArtifact != null
268 && pomArtifact.getFile() != null
269 && pomArtifact.getFile().exists()) {
270 File pomDestFile = new File(
271 destDir,
272 DependencyUtil.getFormattedFileName(
273 pomArtifact, removeVersion, prependGroupId, useBaseVersion, removeClassifier));
274 if (!pomDestFile.exists()) {
275 copyFile(pomArtifact.getFile(), pomDestFile);
276 }
277 }
278 }
279 }
280
281
282
283
284
285 protected Artifact getResolvedPomArtifact(Artifact artifact) {
286 DefaultArtifactCoordinate coordinate = new DefaultArtifactCoordinate();
287 coordinate.setGroupId(artifact.getGroupId());
288 coordinate.setArtifactId(artifact.getArtifactId());
289 coordinate.setVersion(artifact.getVersion());
290 coordinate.setExtension("pom");
291
292 Artifact pomArtifact = null;
293
294 try {
295 ProjectBuildingRequest buildingRequest = newResolveArtifactProjectBuildingRequest();
296
297 pomArtifact = getArtifactResolver()
298 .resolveArtifact(buildingRequest, coordinate)
299 .getArtifact();
300 } catch (ArtifactResolverException e) {
301 getLog().info(e.getMessage());
302 }
303 return pomArtifact;
304 }
305
306 @Override
307 protected ArtifactsFilter getMarkedArtifactFilter() {
308 return new DestFileFilter(
309 this.overWriteReleases,
310 this.overWriteSnapshots,
311 this.overWriteIfNewer,
312 this.useSubDirectoryPerArtifact,
313 this.useSubDirectoryPerType,
314 this.useSubDirectoryPerScope,
315 this.useRepositoryLayout,
316 this.stripVersion,
317 this.prependGroupId,
318 this.useBaseVersion,
319 this.outputDirectory);
320 }
321
322
323
324
325 public boolean isCopyPom() {
326 return this.copyPom;
327 }
328
329
330
331
332 public void setCopyPom(boolean copyPom) {
333 this.copyPom = copyPom;
334 }
335 }