blob: 57f18d68c8bebf6e88b1a99c465739f52c8f89d6 [file] [log] [blame]
package org.apache.maven.resolver.examples.maven;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
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 Maven Artifact Resolver, 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 property="resolver.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() );
}
}