/*******************************************************************************
* Copyright (c) 2010, 2013 Sonatype, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Sonatype, Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.aether.examples.maven;
import java.util.List;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.resolution.ArtifactRequest;
import org.eclipse.aether.resolution.ArtifactResolutionException;
import org.eclipse.aether.resolution.ArtifactResult;
/**
* Resolves a single artifact (not including its transitive dependencies).
*
* @goal resolve-artifact
*/
public class ResolveArtifactMojo
extends AbstractMojo
{
/**
* The entry point to Aether, i.e. the component doing all the work.
*
* @component
*/
private RepositorySystem repoSystem;
/**
* The current repository/network configuration of Maven.
*
* @parameter default-value="${repositorySystemSession}"
* @readonly
*/
private RepositorySystemSession repoSession;
/**
* The project's remote repositories to use for the resolution.
*
* @parameter default-value="${project.remoteProjectRepositories}"
* @readonly
*/
private List<RemoteRepository> remoteRepos;
/**
* The {@code <groupId>:<artifactId>[:<extension>[:<classifier>]]:<version>} of the artifact to resolve.
*
* @parameter expression="${aether.artifactCoords}"
*/
private String artifactCoords;
public void execute()
throws MojoExecutionException, MojoFailureException
{
Artifact artifact;
try
{
artifact = new DefaultArtifact( artifactCoords );
}
catch ( IllegalArgumentException e )
{
throw new MojoFailureException( e.getMessage(), e );
}
ArtifactRequest request = new ArtifactRequest();
request.setArtifact( artifact );
request.setRepositories( remoteRepos );
getLog().info( "Resolving artifact " + artifact + " from " + remoteRepos );
ArtifactResult result;
try
{
result = repoSystem.resolveArtifact( repoSession, request );
}
catch ( ArtifactResolutionException e )
{
throw new MojoExecutionException( e.getMessage(), e );
}
getLog().info( "Resolved artifact " + artifact + " to " + result.getArtifact().getFile() + " from "
+ result.getRepository() );
}
}