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 @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 @Parameter( property = "mdep.useBaseVersion", defaultValue = "true" )
82 protected boolean useBaseVersion = true;
83
84
85
86
87
88
89
90
91
92 protected void doExecute()
93 throws MojoExecutionException
94 {
95 DependencyStatusSets dss = getDependencySets( this.failOnMissingClassifierArtifact );
96 Set<Artifact> artifacts = dss.getResolvedDependencies();
97
98 if ( !useRepositoryLayout )
99 {
100 for ( Artifact artifact : artifacts )
101 {
102 copyArtifact( artifact, isStripVersion(), this.prependGroupId, this.useBaseVersion );
103 }
104 }
105 else
106 {
107 try
108 {
109 ArtifactRepository targetRepository =
110 repositoryFactory.createDeploymentArtifactRepository( "local",
111 outputDirectory.toURL().toExternalForm(),
112 repositoryLayouts.get( "default" ),
113 false
114 for ( Artifact artifact : artifacts )
115 {
116 installArtifact( artifact, targetRepository );
117 }
118 }
119 catch ( MalformedURLException e )
120 {
121 throw new MojoExecutionException( "Could not create outputDirectory repository", e );
122 }
123 }
124
125 Set<Artifact> skippedArtifacts = dss.getSkippedDependencies();
126 for ( Artifact artifact : skippedArtifacts )
127 {
128 getLog().info( artifact.getFile().getName() + " already exists in destination." );
129 }
130
131 if ( isCopyPom() )
132 {
133 copyPoms( getOutputDirectory(), artifacts, this.stripVersion );
134 copyPoms( getOutputDirectory(), skippedArtifacts,
135 this.stripVersion );
136 }
137 }
138
139 private void installArtifact( Artifact artifact, ArtifactRepository targetRepository )
140 {
141 try
142 {
143 if ( "pom".equals( artifact.getType() ) )
144 {
145 installer.install( artifact.getFile(), artifact, targetRepository );
146 installBaseSnapshot( artifact, targetRepository );
147 }
148 else
149 {
150 installer.install( artifact.getFile(), artifact, targetRepository );
151 installBaseSnapshot( artifact, targetRepository );
152
153 if ( isCopyPom() )
154 {
155 Artifact pomArtifact = getResolvedPomArtifact( artifact );
156 if ( pomArtifact.getFile() != null && pomArtifact.getFile().exists() )
157 {
158 installer.install( pomArtifact.getFile(), pomArtifact, targetRepository );
159 installBaseSnapshot( pomArtifact, targetRepository );
160 }
161 }
162 }
163 }
164 catch ( ArtifactInstallationException e )
165 {
166 getLog().info( e.getMessage() );
167 }
168 }
169
170 private void installBaseSnapshot( Artifact artifact, ArtifactRepository targetRepository )
171 throws ArtifactInstallationException
172 {
173 if ( artifact.isSnapshot() && !artifact.getBaseVersion().equals( artifact.getVersion() ) )
174 {
175 Artifact baseArtifact =
176 this.factory.createArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getBaseVersion(),
177 artifact.getScope(), artifact.getType() );
178 installer.install( artifact.getFile(), baseArtifact, targetRepository );
179 }
180 }
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196 protected void copyArtifact( Artifact artifact, boolean removeVersion, boolean prependGroupId,
197 boolean useBaseVersion ) throws MojoExecutionException
198 {
199
200 String destFileName = DependencyUtil.getFormattedFileName( artifact, removeVersion, prependGroupId,
201 useBaseVersion );
202
203 File destDir;
204 destDir = DependencyUtil.getFormattedOutputDirectory( useSubDirectoryPerScope, useSubDirectoryPerType,
205 useSubDirectoryPerArtifact, useRepositoryLayout,
206 stripVersion, outputDirectory, artifact );
207 File destFile = new File( destDir, destFileName );
208
209 copyFile( artifact.getFile(), destFile );
210 }
211
212
213
214
215 public void copyPoms( File destDir, Set<Artifact> artifacts, boolean removeVersion )
216 throws MojoExecutionException
217
218 {
219 for ( Artifact artifact : artifacts )
220 {
221 Artifact pomArtifact = getResolvedPomArtifact( artifact );
222
223
224 if ( pomArtifact.getFile() != null && pomArtifact.getFile().exists() )
225 {
226 File pomDestFile = new File( destDir, DependencyUtil.getFormattedFileName( pomArtifact, removeVersion,
227 prependGroupId, useBaseVersion ) );
228 if ( !pomDestFile.exists() )
229 {
230 copyFile( pomArtifact.getFile(), pomDestFile );
231 }
232 }
233 }
234 }
235
236 protected Artifact getResolvedPomArtifact( Artifact artifact )
237 {
238 Artifact pomArtifact =
239 this.factory.createArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), "",
240 "pom" );
241
242 try
243 {
244 this.resolver.resolve( pomArtifact, this.remoteRepos, this.getLocal() );
245 }
246 catch ( Exception e )
247 {
248 getLog().info( e.getMessage() );
249 }
250 return pomArtifact;
251 }
252
253 protected ArtifactsFilter getMarkedArtifactFilter()
254 {
255 return new DestFileFilter( this.overWriteReleases, this.overWriteSnapshots, this.overWriteIfNewer,
256 this.useSubDirectoryPerArtifact, this.useSubDirectoryPerType,
257 this.useSubDirectoryPerScope, this.useRepositoryLayout, this.stripVersion,
258 this.outputDirectory );
259 }
260 }