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