001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.maven.resolver.examples.maven; 020 021import javax.inject.Inject; 022 023import java.util.List; 024 025import org.apache.maven.plugin.AbstractMojo; 026import org.apache.maven.plugin.MojoExecutionException; 027import org.apache.maven.plugin.MojoFailureException; 028import org.apache.maven.plugins.annotations.Mojo; 029import org.apache.maven.plugins.annotations.Parameter; 030import org.eclipse.aether.RepositorySystem; 031import org.eclipse.aether.RepositorySystemSession; 032import org.eclipse.aether.artifact.Artifact; 033import org.eclipse.aether.artifact.DefaultArtifact; 034import org.eclipse.aether.repository.RemoteRepository; 035import org.eclipse.aether.resolution.ArtifactRequest; 036import org.eclipse.aether.resolution.ArtifactResolutionException; 037import org.eclipse.aether.resolution.ArtifactResult; 038import org.slf4j.Logger; 039import org.slf4j.LoggerFactory; 040 041/** 042 * Resolves a single artifact (not including its transitive dependencies). 043 */ 044@Mojo(name = "resolve-artifact", threadSafe = true) 045public class ResolveArtifactMojo extends AbstractMojo { 046 private static final Logger LOGGER = LoggerFactory.getLogger(ResolveArtifactMojo.class); 047 048 /** 049 * The current repository/network configuration of Maven. 050 */ 051 @Parameter(defaultValue = "${repositorySystemSession}", readonly = true) 052 private RepositorySystemSession repoSession; 053 054 /** 055 * The project's remote repositories to use for the resolution. 056 */ 057 @Parameter(defaultValue = "${project.remoteProjectRepositories}", readonly = true) 058 private List<RemoteRepository> remoteRepos; 059 060 /** 061 * The {@code <groupId>:<artifactId>[:<extension>[:<classifier>]]:<version>} of the artifact to resolve. 062 */ 063 @Parameter(property = "resolver.artifactCoords", readonly = true) 064 private String artifactCoords; 065 066 /** 067 * The entry point to Maven Artifact Resolver; that is, the component doing all the work. 068 */ 069 private final RepositorySystem repoSystem; 070 071 @Inject 072 public ResolveArtifactMojo(RepositorySystem repoSystem) { 073 this.repoSystem = repoSystem; 074 } 075 076 /** 077 * The actual execution of the mojo. 078 */ 079 public void execute() throws MojoExecutionException, MojoFailureException { 080 Artifact artifact; 081 try { 082 artifact = new DefaultArtifact(artifactCoords); 083 } catch (IllegalArgumentException e) { 084 throw new MojoFailureException(e.getMessage(), e); 085 } 086 087 ArtifactRequest request = new ArtifactRequest(); 088 request.setArtifact(artifact); 089 request.setRepositories(remoteRepos); 090 091 LOGGER.info("Resolving artifact {} from {}", artifact, remoteRepos); 092 093 ArtifactResult result; 094 try { 095 result = repoSystem.resolveArtifact(repoSession, request); 096 } catch (ArtifactResolutionException e) { 097 throw new MojoExecutionException(e.getMessage(), e); 098 } 099 100 LOGGER.info( 101 "Resolved artifact {} to {} from {}", 102 artifact, 103 result.getArtifact().getFile(), 104 result.getRepository()); 105 } 106}