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.commons.io.FileUtils;
23 import org.apache.maven.artifact.Artifact;
24 import org.apache.maven.artifact.factory.ArtifactFactory;
25 import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
26 import org.apache.maven.artifact.repository.ArtifactRepository;
27 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
28 import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
29 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
30 import org.apache.maven.artifact.resolver.AbstractArtifactResolutionException;
31 import org.apache.maven.artifact.resolver.ArtifactResolver;
32 import org.apache.maven.plugin.AbstractMojo;
33 import org.apache.maven.plugin.MojoExecutionException;
34 import org.apache.maven.plugin.MojoFailureException;
35 import org.apache.maven.plugins.annotations.Component;
36 import org.apache.maven.plugins.annotations.Mojo;
37 import org.apache.maven.plugins.annotations.Parameter;
38 import org.codehaus.plexus.util.StringUtils;
39
40 import java.io.File;
41 import java.io.IOException;
42 import java.util.ArrayList;
43 import java.util.Arrays;
44 import java.util.Collections;
45 import java.util.List;
46 import java.util.Map;
47 import java.util.regex.Matcher;
48 import java.util.regex.Pattern;
49
50
51
52
53
54 @Mojo( name = "get", requiresProject = false, threadSafe = true )
55 public class GetMojo
56 extends AbstractMojo
57 {
58 private static final Pattern ALT_REPO_SYNTAX_PATTERN = Pattern.compile( "(.+)::(.*)::(.+)" );
59
60
61
62
63 @Component
64 private ArtifactFactory artifactFactory;
65
66
67
68
69 @Component
70 private ArtifactResolver artifactResolver;
71
72
73
74
75 @Component
76 private ArtifactRepositoryFactory artifactRepositoryFactory;
77
78
79
80
81 @Component( role = ArtifactRepositoryLayout.class )
82 private Map<String, ArtifactRepositoryLayout> repositoryLayouts;
83
84
85
86
87 @Component
88 private ArtifactMetadataSource source;
89
90
91
92
93 @Parameter( defaultValue = "${localRepository}", readonly = true )
94 private ArtifactRepository localRepository;
95
96
97
98
99 @Parameter( property = "groupId" )
100 private String groupId;
101
102
103
104
105 @Parameter( property = "artifactId" )
106 private String artifactId;
107
108
109
110
111 @Parameter( property = "version" )
112 private String version;
113
114
115
116
117
118
119 @Parameter( property = "classifier" )
120 private String classifier;
121
122
123
124
125 @Parameter( property = "packaging", defaultValue = "jar" )
126 private String packaging = "jar";
127
128
129
130
131
132
133 @Parameter( property = "repoId", defaultValue = "temp" )
134 private String repositoryId = "temp";
135
136
137
138
139
140
141 @Parameter( property = "repoUrl" )
142 private String repositoryUrl;
143
144
145
146
147
148 @Parameter( property = "remoteRepositories" )
149 private String remoteRepositories;
150
151
152
153
154 @Parameter( property = "artifact" )
155 private String artifact;
156
157
158
159
160
161
162
163 @Parameter( property = "dest" )
164 private String destination;
165
166
167
168
169 @Parameter( defaultValue = "${project.remoteArtifactRepositories}", readonly = true, required = true )
170 private List<ArtifactRepository> pomRemoteRepositories;
171
172
173
174
175 @Parameter( property = "transitive", defaultValue = "true" )
176 private boolean transitive = true;
177
178
179
180
181
182
183 @Parameter( property = "mdep.skip", defaultValue = "false" )
184 private boolean skip;
185
186 public void execute()
187 throws MojoExecutionException, MojoFailureException
188 {
189 if ( isSkip() )
190 {
191 getLog().info( "Skipping plugin execution" );
192 return;
193 }
194
195 if ( artifactId == null && artifact == null )
196 {
197 throw new MojoFailureException( "You must specify an artifact, "
198 + "e.g. -Dartifact=org.apache.maven.plugins:maven-downloader-plugin:1.0" );
199 }
200 if ( artifact != null )
201 {
202 String[] tokens = StringUtils.split( artifact, ":" );
203 if ( tokens.length < 3 || tokens.length > 5 )
204 {
205 throw new MojoFailureException(
206 "Invalid artifact, you must specify groupId:artifactId:version[:packaging][:classifier] "
207 + artifact );
208 }
209 groupId = tokens[0];
210 artifactId = tokens[1];
211 version = tokens[2];
212 if ( tokens.length >= 4 )
213 {
214 packaging = tokens[3];
215 }
216 if ( tokens.length == 5 )
217 {
218 classifier = tokens[4];
219 }
220 else
221 {
222 classifier = null;
223 }
224 }
225
226 Artifact toDownload = classifier == null
227 ? artifactFactory.createBuildArtifact( groupId, artifactId, version, packaging )
228 : artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, packaging, classifier );
229 Artifact dummyOriginatingArtifact =
230 artifactFactory.createBuildArtifact( "org.apache.maven.plugins", "maven-downloader-plugin", "1.0", "jar" );
231
232 ArtifactRepositoryPolicy always =
233 new ArtifactRepositoryPolicy( true, ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS,
234 ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN );
235
236 List<ArtifactRepository> repoList = new ArrayList<ArtifactRepository>();
237
238 if ( pomRemoteRepositories != null )
239 {
240 repoList.addAll( pomRemoteRepositories );
241 }
242
243 if ( remoteRepositories != null )
244 {
245
246 List<String> repos = Arrays.asList( StringUtils.split( remoteRepositories, "," ) );
247 for ( String repo : repos )
248 {
249 repoList.add( parseRepository( repo, always ) );
250 }
251 }
252
253 if ( repositoryUrl != null )
254 {
255 getLog().warn( "repositoryUrl parameter is deprecated. Use remoteRepositories instead" );
256 ArtifactRepository remoteRepo =
257 artifactRepositoryFactory.createArtifactRepository( repositoryId, repositoryUrl, getLayout( "default" ),
258 always, always );
259 repoList.add( remoteRepo );
260 }
261
262 try
263 {
264 if ( transitive )
265 {
266 getLog().info( "Resolving " + toDownload + " with transitive dependencies" );
267 artifactResolver.resolveTransitively( Collections.singleton( toDownload ), dummyOriginatingArtifact,
268 repoList, localRepository, source );
269 }
270 else
271 {
272 getLog().info( "Resolving " + toDownload );
273 artifactResolver.resolve( toDownload, repoList, localRepository );
274 }
275 }
276 catch ( AbstractArtifactResolutionException e )
277 {
278 throw new MojoExecutionException( "Couldn't download artifact: " + e.getMessage(), e );
279 }
280
281 if ( destination != null )
282 {
283 getLog().warn( "destination/dest parameter is deprecated: it will disappear in future version." );
284
285 File src = toDownload.getFile();
286 File dest = new File( destination );
287
288 getLog().info( "Copying " + src.getAbsolutePath() + " to " + dest.getAbsolutePath() );
289 if ( transitive )
290 {
291 getLog().warn( "Notice transitive dependencies won't be copied." );
292 }
293
294 try
295 {
296 if ( dest.isDirectory() )
297 {
298 FileUtils.copyFileToDirectory( src, dest );
299 }
300 else
301 {
302 FileUtils.copyFile( src, dest );
303 }
304 }
305 catch ( IOException e )
306 {
307 throw new MojoExecutionException(
308 "Couldn't copy downloaded artifact from " + src.getAbsolutePath() + " to " + dest.getAbsolutePath()
309 + " : " + e.getMessage(), e );
310 }
311 }
312 }
313
314 ArtifactRepository parseRepository( String repo, ArtifactRepositoryPolicy policy )
315 throws MojoFailureException
316 {
317
318 String id = repositoryId;
319 ArtifactRepositoryLayout layout = getLayout( "default" );
320 String url = repo;
321
322
323 if (repo.contains("::"))
324 {
325 Matcher matcher = ALT_REPO_SYNTAX_PATTERN.matcher( repo );
326 if ( !matcher.matches() )
327 {
328 throw new MojoFailureException( repo, "Invalid syntax for repository: " + repo,
329 "Invalid syntax for repository. Use \"id::layout::url\" or \"URL\"." );
330 }
331
332 id = matcher.group( 1 ).trim();
333 if ( !StringUtils.isEmpty( matcher.group( 2 ) ) )
334 {
335 layout = getLayout( matcher.group( 2 ).trim() );
336 }
337 url = matcher.group( 3 ).trim();
338 }
339 return artifactRepositoryFactory.createArtifactRepository( id, url, layout, policy, policy );
340 }
341
342 private ArtifactRepositoryLayout getLayout( String id )
343 throws MojoFailureException
344 {
345 ArtifactRepositoryLayout layout = repositoryLayouts.get( id );
346
347 if ( layout == null )
348 {
349 throw new MojoFailureException( id, "Invalid repository layout", "Invalid repository layout: " + id );
350 }
351
352 return layout;
353 }
354
355 public boolean isSkip()
356 {
357 return skip;
358 }
359
360 public void setSkip( boolean skip )
361 {
362 this.skip = skip;
363 }
364
365 }