/
GetMojo.java
294 lines (255 loc) · 11.2 KB
/
GetMojo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.plugins.dependency;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.maven.artifact.handler.ArtifactHandler;
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
import org.apache.maven.artifact.repository.MavenArtifactRepository;
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.dependency.utils.StringUtils;
import org.apache.maven.project.DefaultProjectBuildingRequest;
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.repository.RepositorySystem;
import org.apache.maven.settings.Settings;
import org.apache.maven.shared.transfer.artifact.ArtifactCoordinate;
import org.apache.maven.shared.transfer.artifact.DefaultArtifactCoordinate;
import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver;
import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolverException;
import org.apache.maven.shared.transfer.dependencies.DefaultDependableCoordinate;
import org.apache.maven.shared.transfer.dependencies.DependableCoordinate;
import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolver;
import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolverException;
/**
* Resolves a single artifact, eventually transitively, from the specified remote repositories. Caveat: will always
* check the central repository defined in the super pom. You could use a mirror entry in your <code>settings.xml</code>
*/
@Mojo(name = "get", requiresProject = false, threadSafe = true)
public class GetMojo extends AbstractMojo {
private static final Pattern ALT_REPO_SYNTAX_PATTERN = Pattern.compile("(.+)::(.*)::(.+)");
@Component
private MavenSession session;
@Component
private ArtifactResolver artifactResolver;
@Component
private DependencyResolver dependencyResolver;
@Component
private ArtifactHandlerManager artifactHandlerManager;
/**
* Map that contains the layouts.
*/
@Component(role = ArtifactRepositoryLayout.class)
private Map<String, ArtifactRepositoryLayout> repositoryLayouts;
/**
* The repository system.
*/
@Component
private RepositorySystem repositorySystem;
private DefaultDependableCoordinate coordinate = new DefaultDependableCoordinate();
/**
* Repositories in the format id::[layout]::url or just url, separated by comma. ie.
* central::default::https://repo.maven.apache.org/maven2,myrepo::::https://repo.acme.com,https://repo.acme2.com
*/
@Parameter(property = "remoteRepositories")
private String remoteRepositories;
/**
* A string of the form groupId:artifactId:version[:packaging[:classifier]].
*/
@Parameter(property = "artifact")
private String artifact;
/**
*
*/
@Parameter(defaultValue = "${project.remoteArtifactRepositories}", readonly = true, required = true)
private List<ArtifactRepository> pomRemoteRepositories;
/**
* Download transitively, retrieving the specified artifact and all of its dependencies.
*/
@Parameter(property = "transitive", defaultValue = "true")
private boolean transitive = true;
/**
* Skip plugin execution completely.
*
* @since 2.7
*/
@Parameter(property = "mdep.skip", defaultValue = "false")
private boolean skip;
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (isSkip()) {
getLog().info("Skipping plugin execution");
return;
}
if (coordinate.getArtifactId() == null && artifact == null) {
throw new MojoFailureException("You must specify an artifact, "
+ "e.g. -Dartifact=org.apache.maven.plugins:maven-downloader-plugin:1.0");
}
if (artifact != null) {
String[] tokens = artifact.split("\\s*:+\\s*");
if (tokens.length < 3 || tokens.length > 5) {
throw new MojoFailureException("Invalid artifact, you must specify "
+ "groupId:artifactId:version[:packaging[:classifier]] " + artifact);
}
coordinate.setGroupId(tokens[0]);
coordinate.setArtifactId(tokens[1]);
coordinate.setVersion(tokens[2]);
if (tokens.length >= 4) {
coordinate.setType(tokens[3]);
}
if (tokens.length == 5) {
coordinate.setClassifier(tokens[4]);
}
}
ArtifactRepositoryPolicy always = new ArtifactRepositoryPolicy(
true, ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS, ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN);
List<ArtifactRepository> repoList = new ArrayList<>();
if (pomRemoteRepositories != null) {
repoList.addAll(pomRemoteRepositories);
}
if (remoteRepositories != null) {
// Use the same format as in the deploy plugin id::layout::url
String[] repos = remoteRepositories.split("\\s*,+\\s*");
for (String repo : repos) {
repoList.add(parseRepository(repo, always));
}
}
try {
ProjectBuildingRequest buildingRequest =
new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
Settings settings = session.getSettings();
repositorySystem.injectMirror(repoList, settings.getMirrors());
repositorySystem.injectProxy(repoList, settings.getProxies());
repositorySystem.injectAuthentication(repoList, settings.getServers());
buildingRequest.setRemoteRepositories(repoList);
if (transitive) {
getLog().info("Resolving " + coordinate + " with transitive dependencies");
dependencyResolver.resolveDependencies(buildingRequest, coordinate, null);
} else {
getLog().info("Resolving " + coordinate);
artifactResolver.resolveArtifact(buildingRequest, toArtifactCoordinate(coordinate));
}
} catch (ArtifactResolverException | DependencyResolverException e) {
throw new MojoExecutionException("Couldn't download artifact: " + e.getMessage(), e);
}
}
private ArtifactCoordinate toArtifactCoordinate(DependableCoordinate dependableCoordinate) {
ArtifactHandler artifactHandler = artifactHandlerManager.getArtifactHandler(dependableCoordinate.getType());
DefaultArtifactCoordinate artifactCoordinate = new DefaultArtifactCoordinate();
artifactCoordinate.setGroupId(dependableCoordinate.getGroupId());
artifactCoordinate.setArtifactId(dependableCoordinate.getArtifactId());
artifactCoordinate.setVersion(dependableCoordinate.getVersion());
artifactCoordinate.setClassifier(dependableCoordinate.getClassifier());
artifactCoordinate.setExtension(artifactHandler.getExtension());
return artifactCoordinate;
}
ArtifactRepository parseRepository(String repo, ArtifactRepositoryPolicy policy) throws MojoFailureException {
// if it's a simple url
String id = "temp";
ArtifactRepositoryLayout layout = getLayout("default");
String url = repo;
// if it's an extended repo URL of the form id::layout::url
if (repo.contains("::")) {
Matcher matcher = ALT_REPO_SYNTAX_PATTERN.matcher(repo);
if (!matcher.matches()) {
throw new MojoFailureException(
repo,
"Invalid syntax for repository: " + repo,
"Invalid syntax for repository. Use \"id::layout::url\" or \"URL\".");
}
id = matcher.group(1).trim();
if (!StringUtils.isEmpty(matcher.group(2))) {
layout = getLayout(matcher.group(2).trim());
}
url = matcher.group(3).trim();
}
return new MavenArtifactRepository(id, url, layout, policy, policy);
}
private ArtifactRepositoryLayout getLayout(String id) throws MojoFailureException {
ArtifactRepositoryLayout layout = repositoryLayouts.get(id);
if (layout == null) {
throw new MojoFailureException(id, "Invalid repository layout", "Invalid repository layout: " + id);
}
return layout;
}
/**
* @return {@link #skip}
*/
protected boolean isSkip() {
return skip;
}
/**
* The groupId of the artifact to download. Ignored if {@link #artifact} is used.
*
* @param groupId The groupId.
*/
@Parameter(property = "groupId")
public void setGroupId(String groupId) {
this.coordinate.setGroupId(groupId);
}
/**
* The artifactId of the artifact to download. Ignored if {@link #artifact} is used.
*
* @param artifactId The artifactId.
*/
@Parameter(property = "artifactId")
public void setArtifactId(String artifactId) {
this.coordinate.setArtifactId(artifactId);
}
/**
* The version of the artifact to download. Ignored if {@link #artifact} is used.
*
* @param version The version.
*/
@Parameter(property = "version")
public void setVersion(String version) {
this.coordinate.setVersion(version);
}
/**
* The classifier of the artifact to download. Ignored if {@link #artifact} is used.
*
* @param classifier The classifier to be used.
*
* @since 2.3
*/
@Parameter(property = "classifier")
public void setClassifier(String classifier) {
this.coordinate.setClassifier(classifier);
}
/**
* The packaging of the artifact to download. Ignored if {@link #artifact} is used.
*
* @param type packaging.
*/
@Parameter(property = "packaging", defaultValue = "jar")
public void setPackaging(String type) {
this.coordinate.setType(type);
}
}