Nexus REST API query artifacts within a group

2019-04-27 03:55发布

I have a Nexus maven repo and I would like to leverage REST API to query the list of artifacts lying in my specific group. I stumbled upon this documentation but it seems to be very concise and I cannot find there what I need.

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

I want something like this

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

And it would output me a list

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

To be more specific I need a list of versions of artifacts lying in a group, but I can extract versions from artifact names too.

标签: rest nexus
3条回答
仙女界的扛把子
2楼-- · 2019-04-27 04:06

It came out that everything I needed is to fetch ˇmaven-metadata.xml` file that comprises all versions available for this artifact. For example,

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

contains

<?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>
查看更多
Summer. ? 凉城
3楼-- · 2019-04-27 04:08

No need to manually parse maven-metadata.xml. No need to manually parse directory names or file names.

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

returns not only every <version>, but as a bonus, for every artifact of this version, all the identifiers needed to obtain an unique download URL for any single file residing on this Nexus instance. The identifiers required for download URL are: <groupId>, <artifactId> (both of which you say you already know), <version>, <repositoryId>, <extension> (and <classifier> which is optional and undefined in my example):

...
<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>

After you parse lucene/search response, a good idea would be to filter it by repositoryId being either releases or snapshots.

This answer is for Nexus 2.11.

查看更多
Ridiculous、
4楼-- · 2019-04-27 04:19

Usually you would want to use the lucene index maintained for the repositories for lookups like this. See the REST documentation for the indexer plugin, you can search for groupId and artifactId here.

查看更多
登录 后发表回答