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