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