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