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