如何通过 Maven API 获取 Artifact 下载 URL?

发布于 2024-09-02 19:21:46 字数 584 浏览 1 评论 0原文

我正在尝试创建一个 Maven 插件来生成一个文件,其中包含项目中所有依赖项的 URL。我已经能够获取依赖项及其工件,但在获取下载 URL 时遇到问题。

使用 ArtifactResolver 和 ArtifactMetadataSource 我获得了一些工件信息。但是我无法获取所有依赖项的所有信息。我无法找到有关解析逻辑的文档,因此我可以从我的插件中调用它。 我可以使用 ArtifactResolver 来下载工件,但我真正想要的只是 URL。

Maven Artifact API 有一个名为 getDownloadURL 的方法(请参阅 http://maven.apache.org/ref/2.0.4/maven-artifact/apidocs/org/apache/maven/artifact/Artifact.html)。然而我似乎找不到一种方法来获得真正的价值。我总是得到一个空值。有没有办法解决它(无论是否下载)并获取文件来源的 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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荆棘i 2024-09-09 19:21:46

我必须承认我以前从未编写过 Maven 插件,并且将您的问题视为一个很好的学习练习。我大量借鉴了 Maven Guide to Developing Java Plugins以及来自 ma​​ven-dependency-plugin< 的源代码/strong>

我对此可能是错误的,但我不认为工件和 Maven 存储在任何地方的存储库之间存在直接映射。

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

我希望这有帮助。

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

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);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文