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