1 package org.apache.maven.plugin.dependency;
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.installer.ArtifactInstallationException;
24 import org.apache.maven.artifact.installer.ArtifactInstaller;
25 import org.apache.maven.artifact.repository.ArtifactRepository;
26 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
27 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
28 import org.apache.maven.plugin.MojoExecutionException;
29 import org.apache.maven.plugin.dependency.utils.DependencyStatusSets;
30 import org.apache.maven.plugin.dependency.utils.DependencyUtil;
31 import org.apache.maven.plugin.dependency.utils.filters.DestFileFilter;
32 import org.apache.maven.plugins.annotations.Component;
33 import org.apache.maven.plugins.annotations.LifecyclePhase;
34 import org.apache.maven.plugins.annotations.Mojo;
35 import org.apache.maven.plugins.annotations.Parameter;
36 import org.apache.maven.plugins.annotations.ResolutionScope;
37 import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter;
38
39 import java.io.File;
40 import java.net.MalformedURLException;
41 import java.util.Map;
42 import java.util.Set;
43
44
45
46
47
48
49
50
51
52 @Mojo( name = "copy-dependencies", requiresDependencyResolution = ResolutionScope.TEST,
53 defaultPhase = LifecyclePhase.PROCESS_SOURCES, threadSafe = true )
54 public class CopyDependenciesMojo
55 extends AbstractFromDependenciesMojo
56 {
57
58
59
60
61
62
63 @Parameter( property = "mdep.skip", defaultValue = "false" )
64 private boolean skip;
65
66
67
68
69 @Component
70 protected ArtifactInstaller installer;
71
72
73
74
75 @Component
76 protected ArtifactRepositoryFactory repositoryFactory;
77
78
79
80
81 @Component( role = ArtifactRepositoryLayout.class )
82 private Map<String, ArtifactRepositoryLayout> repositoryLayouts;
83
84
85
86
87
88
89
90 @Parameter( property = "mdep.useBaseVersion", defaultValue = "true" )
91 protected boolean useBaseVersion = true;
92
93
94
95
96
97
98
99
100
101 public void execute()
102 throws MojoExecutionException
103 {
104 if ( skip )
105 {
106 return;
107 }
108
109 DependencyStatusSets dss = getDependencySets( this.failOnMissingClassifierArtifact );
110 Set<Artifact> artifacts = dss.getResolvedDependencies();
111
112 if ( !useRepositoryLayout )
113 {
114 for ( Artifact artifact : artifacts )
115 {
116 copyArtifact( artifact, isStripVersion(), this.prependGroupId, this.useBaseVersion );
117 }
118 }
119 else
120 {
121 try
122 {
123 ArtifactRepository targetRepository =
124 repositoryFactory.createDeploymentArtifactRepository( "local",
125 outputDirectory.toURL().toExternalForm(),
126 repositoryLayouts.get( "default" ),
127 false
128 for ( Artifact artifact : artifacts )
129 {
130 installArtifact( artifact, targetRepository );
131 }
132 }
133 catch ( MalformedURLException e )
134 {
135 throw new MojoExecutionException( "Could not create outputDirectory repository", e );
136 }
137 }
138
139 Set<Artifact> skippedArtifacts = dss.getSkippedDependencies();
140 for ( Artifact artifact : skippedArtifacts )
141 {
142 getLog().info( artifact.getFile().getName() + " already exists in destination." );
143 }
144
145 if ( isCopyPom() )
146 {
147 copyPoms( getOutputDirectory(), artifacts, this.stripVersion );
148 copyPoms( getOutputDirectory(), skippedArtifacts,
149 this.stripVersion );
150 }
151 }
152
153 private void installArtifact( Artifact artifact, ArtifactRepository targetRepository )
154 {
155 try
156 {
157 if ( "pom".equals( artifact.getType() ) )
158 {
159 installer.install( artifact.getFile(), artifact, targetRepository );
160 installBaseSnapshot( artifact, targetRepository );
161 }
162 else
163 {
164 installer.install( artifact.getFile(), artifact, targetRepository );
165 installBaseSnapshot( artifact, targetRepository );
166
167 if ( isCopyPom() )
168 {
169 Artifact pomArtifact = getResolvedPomArtifact( artifact );
170 if ( pomArtifact.getFile() != null && pomArtifact.getFile().exists() )
171 {
172 installer.install( pomArtifact.getFile(), pomArtifact, targetRepository );
173 installBaseSnapshot( pomArtifact, targetRepository );
174 }
175 }
176 }
177 }
178 catch ( ArtifactInstallationException e )
179 {
180 getLog().info( e.getMessage() );
181 }
182 }
183
184 private void installBaseSnapshot( Artifact artifact, ArtifactRepository targetRepository )
185 throws ArtifactInstallationException
186 {
187 if ( artifact.isSnapshot() && !artifact.getBaseVersion().equals( artifact.getVersion() ) )
188 {
189 Artifact baseArtifact =
190 this.factory.createArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getBaseVersion(),
191 artifact.getScope(), artifact.getType() );
192 installer.install( artifact.getFile(), baseArtifact, targetRepository );
193 }
194 }
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210 protected void copyArtifact( Artifact artifact, boolean removeVersion, boolean prependGroupId,
211 boolean useBaseVersion ) throws MojoExecutionException
212 {
213
214 String destFileName = DependencyUtil.getFormattedFileName( artifact, removeVersion, prependGroupId,
215 useBaseVersion );
216
217 File destDir;
218 destDir = DependencyUtil.getFormattedOutputDirectory( useSubDirectoryPerScope, useSubDirectoryPerType,
219 useSubDirectoryPerArtifact, useRepositoryLayout,
220 stripVersion, outputDirectory, artifact );
221 File destFile = new File( destDir, destFileName );
222
223 copyFile( artifact.getFile(), destFile );
224 }
225
226
227
228
229 public void copyPoms( File destDir, Set<Artifact> artifacts, boolean removeVersion )
230 throws MojoExecutionException
231
232 {
233 for ( Artifact artifact : artifacts )
234 {
235 Artifact pomArtifact = getResolvedPomArtifact( artifact );
236
237
238 if ( pomArtifact.getFile() != null && pomArtifact.getFile().exists() )
239 {
240 File pomDestFile = new File( destDir, DependencyUtil.getFormattedFileName( pomArtifact, removeVersion,
241 prependGroupId, useBaseVersion ) );
242 if ( !pomDestFile.exists() )
243 {
244 copyFile( pomArtifact.getFile(), pomDestFile );
245 }
246 }
247 }
248 }
249
250 protected Artifact getResolvedPomArtifact( Artifact artifact )
251 {
252 Artifact pomArtifact =
253 this.factory.createArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), "",
254 "pom" );
255
256 try
257 {
258 this.resolver.resolve( pomArtifact, this.remoteRepos, this.getLocal() );
259 }
260 catch ( Exception e )
261 {
262 getLog().info( e.getMessage() );
263 }
264 return pomArtifact;
265 }
266
267 protected ArtifactsFilter getMarkedArtifactFilter()
268 {
269 return new DestFileFilter( this.overWriteReleases, this.overWriteSnapshots, this.overWriteIfNewer,
270 this.useSubDirectoryPerArtifact, this.useSubDirectoryPerType,
271 this.useSubDirectoryPerScope, this.useRepositoryLayout, this.stripVersion,
272 this.outputDirectory );
273 }
274 }