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

查看:24
本文介绍了如何通过 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/maven-artifact/apidocs/org/apache/maven/artifact/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 插件指南以及来自 ma​​ven-dependency-plugin<的源代码/strong>.

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
", a.getGroupId(), a.getArtifactId(), a.getVersion());
            for (ArtifactRepository r : remoteRepos) {
                System.out.printf("%s/%s
", 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天全站免登陆