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