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 java.util.List;
022
023import org.apache.maven.plugin.AbstractMojo;
024import org.apache.maven.plugin.MojoExecutionException;
025import org.apache.maven.plugin.MojoFailureException;
026import org.apache.maven.plugins.annotations.Component;
027import org.apache.maven.plugins.annotations.Mojo;
028import org.apache.maven.plugins.annotations.Parameter;
029import org.eclipse.aether.RepositorySystem;
030import org.eclipse.aether.RepositorySystemSession;
031import org.eclipse.aether.artifact.Artifact;
032import org.eclipse.aether.artifact.DefaultArtifact;
033import org.eclipse.aether.repository.RemoteRepository;
034import org.eclipse.aether.resolution.ArtifactRequest;
035import org.eclipse.aether.resolution.ArtifactResolutionException;
036import org.eclipse.aether.resolution.ArtifactResult;
037import org.slf4j.Logger;
038import org.slf4j.LoggerFactory;
039
040/**
041 * Resolves a single artifact (not including its transitive dependencies).
042 */
043@Mojo(name = "resolve-artifact", threadSafe = true)
044public class ResolveArtifactMojo extends AbstractMojo {
045    private static final Logger LOGGER = LoggerFactory.getLogger(ResolveArtifactMojo.class);
046    /**
047     * The entry point to Maven Artifact Resolver, i.e. the component doing all the work.
048     */
049    @Component
050    private RepositorySystem repoSystem;
051
052    /**
053     * The current repository/network configuration of Maven.
054     */
055    @Parameter(defaultValue = "${repositorySystemSession}", readonly = true)
056    private RepositorySystemSession repoSession;
057
058    /**
059     * The project's remote repositories to use for the resolution.
060     */
061    @Parameter(defaultValue = "${project.remoteProjectRepositories}", readonly = true)
062    private List<RemoteRepository> remoteRepos;
063
064    /**
065     * The {@code <groupId>:<artifactId>[:<extension>[:<classifier>]]:<version>} of the artifact to resolve.
066     */
067    @Parameter(property = "resolver.artifactCoords", readonly = true)
068    private String artifactCoords;
069
070    /**
071     * The actual execution of the mojo.
072     */
073    public void execute() throws MojoExecutionException, MojoFailureException {
074        Artifact artifact;
075        try {
076            artifact = new DefaultArtifact(artifactCoords);
077        } catch (IllegalArgumentException e) {
078            throw new MojoFailureException(e.getMessage(), e);
079        }
080
081        ArtifactRequest request = new ArtifactRequest();
082        request.setArtifact(artifact);
083        request.setRepositories(remoteRepos);
084
085        LOGGER.info("Resolving artifact {} from {}", artifact, remoteRepos);
086
087        ArtifactResult result;
088        try {
089            result = repoSystem.resolveArtifact(repoSession, request);
090        } catch (ArtifactResolutionException e) {
091            throw new MojoExecutionException(e.getMessage(), e);
092        }
093
094        LOGGER.info(
095                "Resolved artifact {} to {} from {}",
096                artifact,
097                result.getArtifact().getFile(),
098                result.getRepository());
099    }
100}