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 | 43 | 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 | 43 | @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 | 49 | if ( skip ) |
105 | |
{ |
106 | 0 | return; |
107 | |
} |
108 | |
|
109 | 49 | DependencyStatusSets dss = getDependencySets( this.failOnMissingClassifierArtifact ); |
110 | 46 | Set<Artifact> artifacts = dss.getResolvedDependencies(); |
111 | |
|
112 | 46 | if ( !useRepositoryLayout ) |
113 | |
{ |
114 | 45 | for ( Artifact artifact : artifacts ) |
115 | |
{ |
116 | 136 | copyArtifact( artifact, isStripVersion(), this.prependGroupId, this.useBaseVersion ); |
117 | |
} |
118 | |
} |
119 | |
else |
120 | |
{ |
121 | |
try |
122 | |
{ |
123 | 1 | ArtifactRepository targetRepository = |
124 | |
repositoryFactory.createDeploymentArtifactRepository( "local", |
125 | |
outputDirectory.toURL().toExternalForm(), |
126 | |
repositoryLayouts.get( "default" ), |
127 | |
false ); |
128 | 1 | for ( Artifact artifact : artifacts ) |
129 | |
{ |
130 | 9 | installArtifact( artifact, targetRepository ); |
131 | |
} |
132 | |
} |
133 | 0 | catch ( MalformedURLException e ) |
134 | |
{ |
135 | 0 | throw new MojoExecutionException( "Could not create outputDirectory repository", e ); |
136 | 1 | } |
137 | |
} |
138 | |
|
139 | 46 | Set<Artifact> skippedArtifacts = dss.getSkippedDependencies(); |
140 | 46 | for ( Artifact artifact : skippedArtifacts ) |
141 | |
{ |
142 | 2 | getLog().info( artifact.getFile().getName() + " already exists in destination." ); |
143 | |
} |
144 | |
|
145 | 46 | if ( isCopyPom() ) |
146 | |
{ |
147 | 46 | copyPoms( getOutputDirectory(), artifacts, this.stripVersion ); |
148 | 46 | copyPoms( getOutputDirectory(), skippedArtifacts, |
149 | |
this.stripVersion ); |
150 | |
} |
151 | 46 | } |
152 | |
|
153 | |
private void installArtifact( Artifact artifact, ArtifactRepository targetRepository ) |
154 | |
{ |
155 | |
try |
156 | |
{ |
157 | 9 | if ( "pom".equals( artifact.getType() ) ) |
158 | |
{ |
159 | 1 | installer.install( artifact.getFile(), artifact, targetRepository ); |
160 | 1 | installBaseSnapshot( artifact, targetRepository ); |
161 | |
} |
162 | |
else |
163 | |
{ |
164 | 8 | installer.install( artifact.getFile(), artifact, targetRepository ); |
165 | 8 | installBaseSnapshot( artifact, targetRepository ); |
166 | |
|
167 | 8 | if ( isCopyPom() ) |
168 | |
{ |
169 | 8 | Artifact pomArtifact = getResolvedPomArtifact( artifact ); |
170 | 8 | if ( pomArtifact.getFile() != null && pomArtifact.getFile().exists() ) |
171 | |
{ |
172 | 0 | installer.install( pomArtifact.getFile(), pomArtifact, targetRepository ); |
173 | 0 | installBaseSnapshot( pomArtifact, targetRepository ); |
174 | |
} |
175 | |
} |
176 | |
} |
177 | |
} |
178 | 0 | catch ( ArtifactInstallationException e ) |
179 | |
{ |
180 | 0 | getLog().info( e.getMessage() ); |
181 | 9 | } |
182 | 9 | } |
183 | |
|
184 | |
private void installBaseSnapshot( Artifact artifact, ArtifactRepository targetRepository ) |
185 | |
throws ArtifactInstallationException |
186 | |
{ |
187 | 9 | if ( artifact.isSnapshot() && !artifact.getBaseVersion().equals( artifact.getVersion() ) ) |
188 | |
{ |
189 | 2 | Artifact baseArtifact = |
190 | |
this.factory.createArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getBaseVersion(), |
191 | |
artifact.getScope(), artifact.getType() ); |
192 | 2 | installer.install( artifact.getFile(), baseArtifact, targetRepository ); |
193 | |
} |
194 | 9 | } |
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 | 136 | String destFileName = DependencyUtil.getFormattedFileName( artifact, removeVersion, prependGroupId, |
215 | |
useBaseVersion ); |
216 | |
|
217 | |
File destDir; |
218 | 136 | destDir = DependencyUtil.getFormattedOutputDirectory( useSubDirectoryPerScope, useSubDirectoryPerType, |
219 | |
useSubDirectoryPerArtifact, useRepositoryLayout, |
220 | |
stripVersion, outputDirectory, artifact ); |
221 | 136 | File destFile = new File( destDir, destFileName ); |
222 | |
|
223 | 136 | copyFile( artifact.getFile(), destFile ); |
224 | 136 | } |
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
public void copyPoms( File destDir, Set<Artifact> artifacts, boolean removeVersion ) |
230 | |
throws MojoExecutionException |
231 | |
|
232 | |
{ |
233 | 92 | for ( Artifact artifact : artifacts ) |
234 | |
{ |
235 | 147 | Artifact pomArtifact = getResolvedPomArtifact( artifact ); |
236 | |
|
237 | |
|
238 | 147 | if ( pomArtifact.getFile() != null && pomArtifact.getFile().exists() ) |
239 | |
{ |
240 | 22 | File pomDestFile = new File( destDir, DependencyUtil.getFormattedFileName( pomArtifact, removeVersion, |
241 | |
prependGroupId, useBaseVersion ) ); |
242 | 22 | if ( !pomDestFile.exists() ) |
243 | |
{ |
244 | 22 | copyFile( pomArtifact.getFile(), pomDestFile ); |
245 | |
} |
246 | |
} |
247 | 147 | } |
248 | 92 | } |
249 | |
|
250 | |
protected Artifact getResolvedPomArtifact( Artifact artifact ) |
251 | |
{ |
252 | 155 | Artifact pomArtifact = |
253 | |
this.factory.createArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), "", |
254 | |
"pom" ); |
255 | |
|
256 | |
try |
257 | |
{ |
258 | 155 | this.resolver.resolve( pomArtifact, this.remoteRepos, this.getLocal() ); |
259 | |
} |
260 | 133 | catch ( Exception e ) |
261 | |
{ |
262 | 133 | getLog().info( e.getMessage() ); |
263 | 22 | } |
264 | 155 | return pomArtifact; |
265 | |
} |
266 | |
|
267 | |
protected ArtifactsFilter getMarkedArtifactFilter() |
268 | |
{ |
269 | 50 | 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 | |
} |