View Javadoc
1   package org.apache.maven.resolver.examples.maven;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.List;
23  
24  import org.apache.maven.plugin.AbstractMojo;
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.plugin.MojoFailureException;
27  import org.apache.maven.plugins.annotations.Component;
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
46      extends AbstractMojo
47  {
48      private static final Logger LOGGER = LoggerFactory.getLogger( ResolveArtifactMojo.class );
49      /**
50       * The entry point to Maven Artifact Resolver, i.e. the component doing all the work.
51       */
52      @Component
53      private RepositorySystem repoSystem;
54  
55      /**
56       * The current repository/network configuration of Maven.
57       */
58      @Parameter( defaultValue = "${repositorySystemSession}", readonly = true )
59      private RepositorySystemSession repoSession;
60  
61      /**
62       * The project's remote repositories to use for the resolution.
63       */
64      @Parameter( defaultValue = "${project.remoteProjectRepositories}", readonly = true )
65      private List<RemoteRepository> remoteRepos;
66  
67      /**
68       * The {@code <groupId>:<artifactId>[:<extension>[:<classifier>]]:<version>} of the artifact to resolve.
69       */
70      @Parameter ( property = "resolver.artifactCoords", readonly = true )
71      private String artifactCoords;
72  
73      /**
74       * The actual execution of the mojo.
75       */
76      public void execute()
77          throws MojoExecutionException, MojoFailureException
78      {
79          Artifact artifact;
80          try
81          {
82              artifact = new DefaultArtifact( artifactCoords );
83          }
84          catch ( IllegalArgumentException e )
85          {
86              throw new MojoFailureException( e.getMessage(), e );
87          }
88  
89          ArtifactRequest request = new ArtifactRequest();
90          request.setArtifact( artifact );
91          request.setRepositories( remoteRepos );
92  
93          LOGGER.info( "Resolving artifact {} from {}", artifact, remoteRepos );
94  
95          ArtifactResult result;
96          try
97          {
98              result = repoSystem.resolveArtifact( repoSession, request );
99          }
100         catch ( ArtifactResolutionException e )
101         {
102             throw new MojoExecutionException( e.getMessage(), e );
103         }
104 
105         LOGGER.info( "Resolved artifact {} to {} from {}", artifact, result.getArtifact().getFile(),
106                 result.getRepository() );
107     }
108 
109 }