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