如何通过Maven API获取Artifact下载URL? [英] How to get an Artifact download URL via Maven API?

查看:151
本文介绍了如何通过Maven API获取Artifact下载URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个maven插件来生成一个带有项目所有依赖关系的URL的文件。我已经能够获得依赖和他们的工件,但是我无法获取下载URL。

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.

使用ArtifactResolver和ArtifactMetadataSource我得到一些工件信息。但是,我无法获取所有依赖关系的所有信息。我无法找到关于分辨率逻辑的文档,所以我可以从我的插件中调用它。
我可以使用ArtifactResolver下载这些工件,但是我真正想要的只是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.

Maven Artifact API有一个名为getDownloadURL的方法(见 http:// maven .apache.org / REF / 2.0.4 /行家假象/ apidocs /组织/阿帕奇/行家/伪像/ Artifact.html )。然而,我似乎找不到一种获得真正价值的方法。我总是得到一个空值。有没有办法解决(下载或不下载)并获取文件所在的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?

推荐答案

我不得不承认我以前永远不会写一个Maven插件,并将你的问题看成是一个很好的学习练习。我从 Maven开发Java插件指南中大量借款以及源自 maven-dependency-plugin

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.

我可能错了这个,但我不认为在Maven任何地方存储的工件和存储库之间都存在直接映射。

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.

我曾经是一个Maven用户,您经常会看到Maven查询每个远程存储库,以确定给定工件的位置。因此,在我下面的代码中,您不会得到一个工件的单个URL,您将获得与远程存储库一样多的URL。您可以随时扩展此代码,尝试下载工件并保留下载成功的URL。

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.

我希望这有助于。

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);
        }
    }
}

这篇关于如何通过Maven API获取Artifact下载URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆