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