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