1 package org.apache.maven.plugins.dependency;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27
28 import org.apache.maven.artifact.handler.ArtifactHandler;
29 import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
30 import org.apache.maven.artifact.repository.ArtifactRepository;
31 import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
32 import org.apache.maven.artifact.repository.MavenArtifactRepository;
33 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
34 import org.apache.maven.execution.MavenSession;
35 import org.apache.maven.plugin.AbstractMojo;
36 import org.apache.maven.plugin.MojoExecutionException;
37 import org.apache.maven.plugin.MojoFailureException;
38 import org.apache.maven.plugins.annotations.Component;
39 import org.apache.maven.plugins.annotations.Mojo;
40 import org.apache.maven.plugins.annotations.Parameter;
41 import org.apache.maven.project.DefaultProjectBuildingRequest;
42 import org.apache.maven.project.ProjectBuildingRequest;
43 import org.apache.maven.repository.RepositorySystem;
44 import org.apache.maven.settings.Settings;
45 import org.apache.maven.shared.transfer.artifact.ArtifactCoordinate;
46 import org.apache.maven.shared.transfer.artifact.DefaultArtifactCoordinate;
47 import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver;
48 import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolverException;
49 import org.apache.maven.shared.transfer.dependencies.DefaultDependableCoordinate;
50 import org.apache.maven.shared.transfer.dependencies.DependableCoordinate;
51 import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolver;
52 import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolverException;
53 import org.codehaus.plexus.util.StringUtils;
54
55
56
57
58
59 @Mojo( name = "get", requiresProject = false, threadSafe = true )
60 public class GetMojo
61 extends AbstractMojo
62 {
63 private static final Pattern ALT_REPO_SYNTAX_PATTERN = Pattern.compile( "(.+)::(.*)::(.+)" );
64
65 @Parameter( defaultValue = "${session}", required = true, readonly = true )
66 private MavenSession session;
67
68
69
70
71 @Component
72 private ArtifactResolver artifactResolver;
73
74
75
76
77 @Component
78 private DependencyResolver dependencyResolver;
79
80 @Component
81 private ArtifactHandlerManager artifactHandlerManager;
82
83
84
85
86 @Component( role = ArtifactRepositoryLayout.class )
87 private Map<String, ArtifactRepositoryLayout> repositoryLayouts;
88
89
90
91
92 @Component
93 private RepositorySystem repositorySystem;
94
95 private DefaultDependableCoordinate coordinate = new DefaultDependableCoordinate();
96
97
98
99
100 @Parameter( property = "groupId" )
101 private String groupId;
102
103
104
105
106 @Parameter( property = "artifactId" )
107 private String artifactId;
108
109
110
111
112 @Parameter( property = "version" )
113 private String version;
114
115
116
117
118
119
120 @Parameter( property = "classifier" )
121 private String classifier;
122
123
124
125
126 @Parameter( property = "packaging", defaultValue = "jar" )
127 private String packaging = "jar";
128
129
130
131
132
133 @Parameter( property = "remoteRepositories" )
134 private String remoteRepositories;
135
136
137
138
139 @Parameter( property = "artifact" )
140 private String artifact;
141
142
143
144
145 @Parameter( defaultValue = "${project.remoteArtifactRepositories}", readonly = true, required = true )
146 private List<ArtifactRepository> pomRemoteRepositories;
147
148
149
150
151 @Parameter( property = "transitive", defaultValue = "true" )
152 private boolean transitive = true;
153
154
155
156
157
158
159 @Parameter( property = "mdep.skip", defaultValue = "false" )
160 private boolean skip;
161
162 @Override
163 public void execute()
164 throws MojoExecutionException, MojoFailureException
165 {
166 if ( isSkip() )
167 {
168 getLog().info( "Skipping plugin execution" );
169 return;
170 }
171
172 if ( coordinate.getArtifactId() == null && artifact == null )
173 {
174 throw new MojoFailureException( "You must specify an artifact, "
175 + "e.g. -Dartifact=org.apache.maven.plugins:maven-downloader-plugin:1.0" );
176 }
177 if ( artifact != null )
178 {
179 String[] tokens = StringUtils.split( artifact, ":" );
180 if ( tokens.length < 3 || tokens.length > 5 )
181 {
182 throw new MojoFailureException( "Invalid artifact, you must specify "
183 + "groupId:artifactId:version[:packaging[:classifier]] " + artifact );
184 }
185 coordinate.setGroupId( tokens[0] );
186 coordinate.setArtifactId( tokens[1] );
187 coordinate.setVersion( tokens[2] );
188 if ( tokens.length >= 4 )
189 {
190 coordinate.setType( tokens[3] );
191 }
192 if ( tokens.length == 5 )
193 {
194 coordinate.setClassifier( tokens[4] );
195 }
196 }
197
198 ArtifactRepositoryPolicy always =
199 new ArtifactRepositoryPolicy( true, ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS,
200 ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN );
201
202 List<ArtifactRepository> repoList = new ArrayList<>();
203
204 if ( pomRemoteRepositories != null )
205 {
206 repoList.addAll( pomRemoteRepositories );
207 }
208
209 if ( remoteRepositories != null )
210 {
211
212 String[] repos = StringUtils.split( remoteRepositories, "," );
213 for ( String repo : repos )
214 {
215 repoList.add( parseRepository( repo, always ) );
216 }
217 }
218
219 try
220 {
221 ProjectBuildingRequest buildingRequest =
222 new DefaultProjectBuildingRequest( session.getProjectBuildingRequest() );
223
224 Settings settings = session.getSettings();
225 repositorySystem.injectMirror( repoList, settings.getMirrors() );
226 repositorySystem.injectProxy( repoList, settings.getProxies() );
227 repositorySystem.injectAuthentication( repoList, settings.getServers() );
228
229 buildingRequest.setRemoteRepositories( repoList );
230
231 if ( transitive )
232 {
233 getLog().info( "Resolving " + coordinate + " with transitive dependencies" );
234 dependencyResolver.resolveDependencies( buildingRequest, coordinate, null );
235 }
236 else
237 {
238 getLog().info( "Resolving " + coordinate );
239 artifactResolver.resolveArtifact( buildingRequest, toArtifactCoordinate( coordinate ) );
240 }
241 }
242 catch ( ArtifactResolverException | DependencyResolverException e )
243 {
244 throw new MojoExecutionException( "Couldn't download artifact: " + e.getMessage(), e );
245 }
246 }
247
248 private ArtifactCoordinate toArtifactCoordinate( DependableCoordinate dependableCoordinate )
249 {
250 ArtifactHandler artifactHandler = artifactHandlerManager.getArtifactHandler( dependableCoordinate.getType() );
251 DefaultArtifactCoordinate artifactCoordinate = new DefaultArtifactCoordinate();
252 artifactCoordinate.setGroupId( dependableCoordinate.getGroupId() );
253 artifactCoordinate.setArtifactId( dependableCoordinate.getArtifactId() );
254 artifactCoordinate.setVersion( dependableCoordinate.getVersion() );
255 artifactCoordinate.setClassifier( dependableCoordinate.getClassifier() );
256 artifactCoordinate.setExtension( artifactHandler.getExtension() );
257 return artifactCoordinate;
258 }
259
260 ArtifactRepository parseRepository( String repo, ArtifactRepositoryPolicy policy )
261 throws MojoFailureException
262 {
263
264 String id = "temp";
265 ArtifactRepositoryLayout layout = getLayout( "default" );
266 String url = repo;
267
268
269 if ( repo.contains( "::" ) )
270 {
271 Matcher matcher = ALT_REPO_SYNTAX_PATTERN.matcher( repo );
272 if ( !matcher.matches() )
273 {
274 throw new MojoFailureException( repo, "Invalid syntax for repository: " + repo,
275 "Invalid syntax for repository. Use \"id::layout::url\" or \"URL\"." );
276 }
277
278 id = matcher.group( 1 ).trim();
279 if ( !StringUtils.isEmpty( matcher.group( 2 ) ) )
280 {
281 layout = getLayout( matcher.group( 2 ).trim() );
282 }
283 url = matcher.group( 3 ).trim();
284 }
285 return new MavenArtifactRepository( id, url, layout, policy, policy );
286 }
287
288 private ArtifactRepositoryLayout getLayout( String id )
289 throws MojoFailureException
290 {
291 ArtifactRepositoryLayout layout = repositoryLayouts.get( id );
292
293 if ( layout == null )
294 {
295 throw new MojoFailureException( id, "Invalid repository layout", "Invalid repository layout: " + id );
296 }
297
298 return layout;
299 }
300
301
302
303
304 protected boolean isSkip()
305 {
306 return skip;
307 }
308
309
310
311
312 public void setGroupId( String groupId )
313 {
314 this.coordinate.setGroupId( groupId );
315 }
316
317
318
319
320 public void setArtifactId( String artifactId )
321 {
322 this.coordinate.setArtifactId( artifactId );
323 }
324
325
326
327
328 public void setVersion( String version )
329 {
330 this.coordinate.setVersion( version );
331 }
332
333
334
335
336 public void setClassifier( String classifier )
337 {
338 this.coordinate.setClassifier( classifier );
339 }
340
341
342
343
344 public void setPackaging( String type )
345 {
346 this.coordinate.setType( type );
347 }
348
349 }