如何通过 Maven API 获取 Artifact 下载 URL?
我正在尝试创建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我必须承认我以前从未编写过 Maven 插件,并且将您的问题视为一个很好的学习练习。我大量借鉴了 Maven Guide to Developing Java Plugins以及来自 maven-dependency-plugin< 的源代码/strong>。
我对此可能是错误的,但我不认为工件和 Maven 存储在任何地方的存储库之间存在直接映射。
我已经成为 Maven 用户一段时间了,您经常会看到 Maven 查询每个远程存储库以确定给定工件的位置。因此,在下面的代码中,您不会获得工件的单个 URL,您将获得与远程存储库一样多的 URL。您始终可以扩展此代码以尝试下载工件并保留下载成功的 URL。
我希望这有帮助。
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.