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.resolver.examples.maven;
20  
21  import java.util.List;
22  
23  import org.apache.maven.plugin.AbstractMojo;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugin.MojoFailureException;
26  import org.apache.maven.plugins.annotations.Component;
27  import org.apache.maven.plugins.annotations.Mojo;
28  import org.apache.maven.plugins.annotations.Parameter;
29  import org.eclipse.aether.RepositorySystem;
30  import org.eclipse.aether.RepositorySystemSession;
31  import org.eclipse.aether.artifact.Artifact;
32  import org.eclipse.aether.artifact.DefaultArtifact;
33  import org.eclipse.aether.repository.RemoteRepository;
34  import org.eclipse.aether.resolution.ArtifactRequest;
35  import org.eclipse.aether.resolution.ArtifactResolutionException;
36  import org.eclipse.aether.resolution.ArtifactResult;
37  import org.slf4j.Logger;
38  import org.slf4j.LoggerFactory;
39  
40  /**
41   * Resolves a single artifact (not including its transitive dependencies).
42   */
43  @Mojo(name = "resolve-artifact", threadSafe = true)
44  public class ResolveArtifactMojo extends AbstractMojo {
45      private static final Logger LOGGER = LoggerFactory.getLogger(ResolveArtifactMojo.class);
46      /**
47       * The entry point to Maven Artifact Resolver, i.e. the component doing all the work.
48       */
49      @Component
50      private RepositorySystem repoSystem;
51  
52      /**
53       * The current repository/network configuration of Maven.
54       */
55      @Parameter(defaultValue = "${repositorySystemSession}", readonly = true)
56      private RepositorySystemSession repoSession;
57  
58      /**
59       * The project's remote repositories to use for the resolution.
60       */
61      @Parameter(defaultValue = "${project.remoteProjectRepositories}", readonly = true)
62      private List<RemoteRepository> remoteRepos;
63  
64      /**
65       * The {@code <groupId>:<artifactId>[:<extension>[:<classifier>]]:<version>} of the artifact to resolve.
66       */
67      @Parameter(property = "resolver.artifactCoords", readonly = true)
68      private String artifactCoords;
69  
70      /**
71       * The actual execution of the mojo.
72       */
73      public void execute() throws MojoExecutionException, MojoFailureException {
74          Artifact artifact;
75          try {
76              artifact = new DefaultArtifact(artifactCoords);
77          } catch (IllegalArgumentException e) {
78              throw new MojoFailureException(e.getMessage(), e);
79          }
80  
81          ArtifactRequest request = new ArtifactRequest();
82          request.setArtifact(artifact);
83          request.setRepositories(remoteRepos);
84  
85          LOGGER.info("Resolving artifact {} from {}", artifact, remoteRepos);
86  
87          ArtifactResult result;
88          try {
89              result = repoSystem.resolveArtifact(repoSession, request);
90          } catch (ArtifactResolutionException e) {
91              throw new MojoExecutionException(e.getMessage(), e);
92          }
93  
94          LOGGER.info(
95                  "Resolved artifact {} to {} from {}",
96                  artifact,
97                  result.getArtifact().getFile(),
98                  result.getRepository());
99      }
100 }