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