Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get an Artifact download URL via Maven API?

I'm trying to create a maven plugin to generate a file with the URL to all the dependencies in a project. I have been able to get the dependencies and their artifact, but I'm having trouble getting the download URL.

Using ArtifactResolver and ArtifactMetadataSource I get some of the artifact information. However I fail to get all the information for all the dependencies. I haven't been able to find documentation on the resolution logic, so that I can call it form my plugin. I can use an ArtifactResolver to download the artifact, but what I really wanted was just the URL.

The Maven Artifact API has a a method called getDownloadURL (see http://maven.apache.org/ref/2.0.4/maven-artifact/apidocs/org/apache/maven/artifact/Artifact.html). However I cant seem to find a way to get a real value into it. I always get a null value. Is there a way to have it resolved (downloading or not) and get the URL for where the file came from?

like image 452
Thomas Avatar asked May 26 '10 05:05

Thomas


1 Answers

I have to admit I have never written a Maven plugin before and saw your question as an good learning exercise. I borrowed heavily from the Maven Guide to Developing Java Plugins and the source code from the maven-dependency-plugin.

I may be wrong about this but I do not think there is a direct mapping between artifact and repository that is stored anywhere by Maven.

I have been a Maven user for sometime and you often see Maven querying every remote repository to ascertain the location of a given artifact. Therefore, in my code below you will not get a single URL for an artifact you will get as many URLs as there are remote repositories. You could always extend this code to attempt to download the artifact and retaining the URLs where the download is successful.

I hope this helps.

package sample.plugin;

import java.util.List;
import java.util.Set;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectBuilder;
import org.apache.maven.project.ProjectBuildingException;
import org.apache.maven.project.artifact.InvalidDependencyVersionException;

/**
 * Says "Hi" to the user.
 * @goal sayhi
 */
public class GreetingMojo extends AbstractMojo {

    /**
     * @parameter expression="${localRepository}"
     * @readonly
     * @required
     */
    protected ArtifactRepository local;
    /**
     * @parameter expression="${project.remoteArtifactRepositories}"
     * @readonly
     * @required
     */
    protected List<ArtifactRepository> remoteRepos;
    /**
     * @component role="org.apache.maven.project.MavenProjectBuilder"
     * @required
     * @readonly
     */
    protected MavenProjectBuilder mavenProjectBuilder;
    /**
     * @component
     */
    protected ArtifactFactory factory;
    /**
     * @component
     */
    protected MavenProject project;

    public void execute() throws MojoExecutionException {
        try {
            resolveDependencies(project);
        } catch (Exception ex) {
            getLog().error(ex);
        }
    }

    private void resolveDependencies(MavenProject theProject)
            throws ArtifactResolutionException, ArtifactNotFoundException, InvalidDependencyVersionException, ProjectBuildingException {
        Set<Artifact> artifacts = theProject.createArtifacts(this.factory, Artifact.SCOPE_TEST,
                new ScopeArtifactFilter(Artifact.SCOPE_TEST));
        for (Artifact a : artifacts) {
            System.out.printf("%s : %s : %s\n", a.getGroupId(), a.getArtifactId(), a.getVersion());
            for (ArtifactRepository r : remoteRepos) {
                System.out.printf("%s/%s\n", r.getUrl(), r.pathOf(a));
            }
            System.out.println();
            Artifact pomArtifact = this.factory.createArtifact(a.getGroupId(), a.getArtifactId(), a.getVersion(), "", "pom");
            MavenProject pomProject = mavenProjectBuilder.buildFromRepository(pomArtifact, remoteRepos, local);
            resolveDependencies(pomProject);
        }
    }
}
like image 119
Mark McLaren Avatar answered Dec 27 '22 05:12

Mark McLaren