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  
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   * Resolves a single artifact, eventually transitively, from the specified remote repositories. Caveat: will always
56   * check the central repository defined in the super pom. You could use a mirror entry in your <code>settings.xml</code>
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       * Map that contains the layouts.
76       */
77      @Component(role = ArtifactRepositoryLayout.class)
78      private Map<String, ArtifactRepositoryLayout> repositoryLayouts;
79  
80      /**
81       * The repository system.
82       */
83      @Component
84      private RepositorySystem repositorySystem;
85  
86      private DefaultDependableCoordinate coordinate = new DefaultDependableCoordinate();
87  
88      /**
89       * Repositories in the format id::[layout]::url or just url, separated by comma. ie.
90       * central::default::https://repo.maven.apache.org/maven2,myrepo::::https://repo.acme.com,https://repo.acme2.com
91       */
92      @Parameter(property = "remoteRepositories")
93      private String remoteRepositories;
94  
95      /**
96       * A string of the form groupId:artifactId:version[:packaging[:classifier]].
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      * Download transitively, retrieving the specified artifact and all of its dependencies.
109      */
110     @Parameter(property = "transitive", defaultValue = "true")
111     private boolean transitive = true;
112 
113     /**
114      * Skip plugin execution completely.
115      *
116      * @since 2.7
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             // Use the same format as in the deploy plugin id::layout::url
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         // if it's a simple url
202         String id = "temp";
203         ArtifactRepositoryLayout layout = getLayout("default");
204         String url = repo;
205 
206         // if it's an extended repo URL of the form id::layout::url
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      * @return {@link #skip}
237      */
238     protected boolean isSkip() {
239         return skip;
240     }
241 
242     /**
243      * The groupId of the artifact to download. Ignored if {@link #artifact} is used.
244      *
245      * @param groupId The groupId.
246      */
247     @Parameter(property = "groupId")
248     public void setGroupId(String groupId) {
249         this.coordinate.setGroupId(groupId);
250     }
251 
252     /**
253      * The artifactId of the artifact to download. Ignored if {@link #artifact} is used.
254      *
255      * @param artifactId The artifactId.
256      */
257     @Parameter(property = "artifactId")
258     public void setArtifactId(String artifactId) {
259         this.coordinate.setArtifactId(artifactId);
260     }
261 
262     /**
263      * The version of the artifact to download. Ignored if {@link #artifact} is used.
264      *
265      * @param version The version.
266      */
267     @Parameter(property = "version")
268     public void setVersion(String version) {
269         this.coordinate.setVersion(version);
270     }
271 
272     /**
273      * The classifier of the artifact to download. Ignored if {@link #artifact} is used.
274      *
275      * @param classifier The classifier to be used.
276      *
277      * @since 2.3
278      */
279     @Parameter(property = "classifier")
280     public void setClassifier(String classifier) {
281         this.coordinate.setClassifier(classifier);
282     }
283 
284     /**
285      * The packaging of the artifact to download. Ignored if {@link #artifact} is used.
286      *
287      * @param type packaging.
288      */
289     @Parameter(property = "packaging", defaultValue = "jar")
290     public void setPackaging(String type) {
291         this.coordinate.setType(type);
292     }
293 }