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