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.plugin.testing;
20  
21  // START SNIPPET: resolve-mojo
22  import javax.inject.Inject;
23  
24  import org.apache.maven.execution.MavenSession;
25  import org.apache.maven.plugin.AbstractMojo;
26  import org.apache.maven.plugin.MojoExecutionException;
27  import org.apache.maven.plugins.annotations.Mojo;
28  import org.apache.maven.plugins.annotations.Parameter;
29  import org.apache.maven.project.MavenProject;
30  import org.eclipse.aether.RepositorySystem;
31  import org.eclipse.aether.artifact.DefaultArtifact;
32  import org.eclipse.aether.resolution.ArtifactRequest;
33  import org.eclipse.aether.resolution.ArtifactResolutionException;
34  import org.eclipse.aether.resolution.ArtifactResult;
35  
36  /**
37   * Simple mojo for resolving artifacts.
38   */
39  @Mojo(name = "simple-resolve")
40  public class SimpleResolveMojo extends AbstractMojo {
41  
42      private final MavenProject project;
43      private final MavenSession session;
44      private final RepositorySystem repositorySystem;
45  
46      @Parameter
47      private String artifact;
48  
49      @Inject
50      SimpleResolveMojo(MavenProject project, MavenSession session, RepositorySystem repositorySystem) {
51          this.project = project;
52          this.session = session;
53          this.repositorySystem = repositorySystem;
54      }
55  
56      @Override
57      public void execute() throws MojoExecutionException {
58  
59          ArtifactRequest request =
60                  new ArtifactRequest(new DefaultArtifact(artifact), project.getRemoteProjectRepositories(), null);
61  
62          try {
63              ArtifactResult result = repositorySystem.resolveArtifact(session.getRepositorySession(), request);
64              getLog().info("Resolved artifact to " + result.getArtifact().getFile());
65          } catch (ArtifactResolutionException e) {
66              throw new MojoExecutionException("Failed to resolve artifact", e);
67          }
68      }
69  }
70  // END SNIPPET: resolve-mojo