【问题标题】:Nexus REST API query artifacts within a group组内的 Nexus REST API 查询工件
【发布时间】:2014-09-17 09:34:45
【问题描述】:

我有一个 Nexus maven 存储库,我想利用 REST API 来查询位于我的特定组中的工件列表。我偶然发现了这个文档,但它似乎非常简洁,我在那里找不到我需要的东西。

https://oss.sonatype.org/nexus-restlet1x-plugin/default/docs/rest.html

我想要这样的东西

http://mydomain:8081/nexus/service/local/repositories/list?groupId=com.test.superproduct&repo=snapshots

它会输出一个列表

  • product-1.0.0-SNAPSHOT
  • product-1.0.1-SNAPSHOT
  • product-1.0.2-SNAPSHOT .....

更具体地说,我需要一组工件的版本列表,但我也可以从工件名称中提取版本。

【问题讨论】:

    标签: rest nexus


    【解决方案1】:

    结果表明,我所需要的只是获取包含可用于该工件的所有版本的 ˇmaven-metadata.xml` 文件。例如,

    https://oss.sonatype.org/service/local/repositories/snapshots/content/com/alibaba/rocketmq/rocketmq-all/maven-metadata.xml
    

    包含

    <?xml version="1.0" encoding="UTF-8"?>
    <metadata modelVersion="1.1.0">
      <groupId>com.alibaba.rocketmq</groupId>
      <artifactId>rocketmq-all</artifactId>
      <versioning>
        <latest>3.1.8-SNAPSHOT</latest>
        <release></release>
        <versions>
          <version>3.0.2-open-SNAPSHOT</version>
          <version>3.0.10-ALIYUN-SNAPSHOT</version>
          <version>3.0.11-SNAPSHOT</version>
          <version>3.1.8-SNAPSHOT</version>
        </versions>
        <lastUpdated>20140807060304</lastUpdated>
      </versioning>
    </metadata>
    

    【讨论】:

    • 你是如何获取 xml 文件的?
    • ajc,你是什么意思?这取决于您使用的语言/技术,但在所有情况下,这只是执行常规 http 请求并处理响应的问题。我刚刚用我的 Gradle 脚本加载了它,并使用内置的 XML 解析器提取了数据。
    【解决方案2】:

    无需手动解析 maven-metadata.xml。无需手动解析目录名或文件名。

    http://localhost/nexus/service/local/lucene/search?g=com.foo&a=foo-bar
    

    不仅返回每个&lt;version&gt;,而且作为奖励,对于此版本的每个工件,获取驻留在此 Nexus 实例上的任何单个文件的唯一下载 URL 所需的所有标识符。下载 URL 所需的标识符是:&lt;groupId&gt;&lt;artifactId&gt;(你说你已经知道这两个)、&lt;version&gt;&lt;repositoryId&gt;&lt;extension&gt;(和&lt;classifier&gt;,在我的示例中是可选且未定义的):

    ...
    <artifact>
      <groupId>com.foo</groupId>
      <artifactId>foo-bar</artifactId>
      <version>2.8.1</version>
      <latestSnapshot>2.8.5-SNAPSHOT</latestSnapshot>
      <latestSnapshotRepositoryId>snapshots</latestSnapshotRepositoryId>
      <latestRelease>2.8.3</latestRelease>
      <latestReleaseRepositoryId>releases</latestReleaseRepositoryId>
      <artifactHits>
        <artifactHit>
          <repositoryId>releases</repositoryId>
          <artifactLinks>
            <artifactLink>
              <extension>pom</extension>
            </artifactLink>
            <artifactLink>
              <extension>war</extension>
            </artifactLink>
          </artifactLinks>
        </artifactHit>
      </artifactHits>
    </artifact>
    <artifact>
      <groupId>com.foo</groupId>
      <artifactId>foo-bar</artifactId>
      <version>2.8.0</version>
      <latestSnapshot>2.8.5-SNAPSHOT</latestSnapshot>
      <latestSnapshotRepositoryId>snapshots</latestSnapshotRepositoryId>
      <latestRelease>2.8.3</latestRelease>
      <latestReleaseRepositoryId>releases</latestReleaseRepositoryId>
      <artifactHits>
        <artifactHit>
          <repositoryId>releases</repositoryId>
          <artifactLinks>
            <artifactLink>
              <extension>pom</extension>
            </artifactLink>
            <artifactLink>
              <extension>war</extension>
            </artifactLink>
          </artifactLinks>
        </artifactHit>
      </artifactHits>
    </artifact>
    

    解析lucene/search 响应后,一个好主意是通过repositoryId 过滤它,或者releasessnapshots

    此答案适用于 Nexus 2.11。

    【讨论】:

    【解决方案3】:

    通常您会希望使用为存储库维护的 lucene 索引来进行这样的查找。见the REST documentation for the indexer plugin,可以在这里搜索groupId和artifactId。

    【讨论】:

      【解决方案4】:

      我知道自 OP 以来已经过去了 5 年,但这里是用于 nexus 3 的 url,以防有人需要它:http://nexus.domain/service/rest/v1/search/assets/download?repository=maven-snapshots&amp;maven.groupId=com.example.pack&amp;maven.artifactId=apo&amp;maven.baseVersion=1.0.0-SNAPSHOT&amp;maven.extension=jar

      【讨论】:

      • 您能否提供一个有效的链接,因为答案中提供的链接无效。
      • 您必须为您的 nexus 实例更改 nexus.domain。默认情况下它应该被替换为 localhost:8081 -> http://localhost:8081/service/rest/v1/search/assets/download?repository=maven-snapshots&amp;maven.groupId=com.example.pack&amp;maven.artifactId=apo&amp;maven.baseVersion=1.0.0-SNAPSHOT&amp;maven.extension=jar 你也应该替换 maven.baseVersion 键。
      猜你喜欢
      • 2017-08-09
      • 2017-01-22
      • 2012-06-17
      • 2017-06-28
      • 2018-10-16
      • 2016-02-19
      • 2012-06-06
      • 1970-01-01
      • 2017-10-06
      相关资源
      最近更新 更多