View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Resolves a single artifact, eventually transitively, from the specified remote repositories. Caveat: will always
55   * check the central repository defined in the super pom. You could use a mirror entry in your <code>settings.xml</code>
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       * Map that contains the layouts.
75       */
76      @Component(role = ArtifactRepositoryLayout.class)
77      private Map<String, ArtifactRepositoryLayout> repositoryLayouts;
78  
79      /**
80       * The repository system.
81       */
82      @Component
83      private RepositorySystem repositorySystem;
84  
85      private DefaultDependableCoordinate coordinate = new DefaultDependableCoordinate();
86  
87      /**
88       * The groupId of the artifact to download. Ignored if {@link #artifact} is used.
89       */
90      @Parameter(property = "groupId")
91      private String groupId;
92  
93      /**
94       * The artifactId of the artifact to download. Ignored if {@link #artifact} is used.
95       */
96      @Parameter(property = "artifactId")
97      private String artifactId;
98  
99      /**
100      * The version of the artifact to download. Ignored if {@link #artifact} is used.
101      */
102     @Parameter(property = "version")
103     private String version;
104 
105     /**
106      * The classifier of the artifact to download. Ignored if {@link #artifact} is used.
107      *
108      * @since 2.3
109      */
110     @Parameter(property = "classifier")
111     private String classifier;
112 
113     /**
114      * The packaging of the artifact to download. Ignored if {@link #artifact} is used.
115      */
116     @Parameter(property = "packaging", defaultValue = "jar")
117     private String packaging = "jar";
118 
119     /**
120      * Repositories in the format id::[layout]::url or just url, separated by comma. ie.
121      * central::default::https://repo.maven.apache.org/maven2,myrepo::::https://repo.acme.com,https://repo.acme2.com
122      */
123     @Parameter(property = "remoteRepositories")
124     private String remoteRepositories;
125 
126     /**
127      * A string of the form groupId:artifactId:version[:packaging[:classifier]].
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      * Download transitively, retrieving the specified artifact and all of its dependencies.
140      */
141     @Parameter(property = "transitive", defaultValue = "true")
142     private boolean transitive = true;
143 
144     /**
145      * Skip plugin execution completely.
146      *
147      * @since 2.7
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             // Use the same format as in the deploy plugin id::layout::url
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         // if it's a simple url
233         String id = "temp";
234         ArtifactRepositoryLayout layout = getLayout("default");
235         String url = repo;
236 
237         // if it's an extended repo URL of the form id::layout::url
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      * @return {@link #skip}
268      */
269     protected boolean isSkip() {
270         return skip;
271     }
272 
273     /**
274      * @param groupId The groupId.
275      */
276     public void setGroupId(String groupId) {
277         this.coordinate.setGroupId(groupId);
278     }
279 
280     /**
281      * @param artifactId The artifactId.
282      */
283     public void setArtifactId(String artifactId) {
284         this.coordinate.setArtifactId(artifactId);
285     }
286 
287     /**
288      * @param version The version.
289      */
290     public void setVersion(String version) {
291         this.coordinate.setVersion(version);
292     }
293 
294     /**
295      * @param classifier The classifier to be used.
296      */
297     public void setClassifier(String classifier) {
298         this.coordinate.setClassifier(classifier);
299     }
300 
301     /**
302      * @param type packaging.
303      */
304     public void setPackaging(String type) {
305         this.coordinate.setType(type);
306     }
307 }