OSS Nexus: how to use REST API to retrieve last ve

2019-06-18 06:44发布

问题:

I'd like to retrieve the latest version name (as text) to be able to rename the artificats retrieved from Nexus which have timestamps.

What I do is create an archive of several archives containing in-house jar projects, dependencies, related scripts, ... But if the packaged jars are snapshots, the archives end up with timestamps when downloaded. Those timestamps replace the X.X.X-SNAPSHOT extension of the archive and I cannot make any automated script to perform some tasks like extracting the archive, renaming the directory, make some symbolic links, ...

I did not find anything related to this in the rest api documentation. Is there a simple way to do this with the rest api or some kind of scripting?

Thanks.

Edit:

From the below answer I managed to retrieve the latest snapshot version using LATEST instead of the version name:

Then using a script I can retrieve the base version.

#!/bin/bash
VERSION=`curl --silent "http://redmine.saic.int:8081/nexus/service/local/artifact/maven/resolve?r=snapshots&g=com.g2mobility&a=G2-Modem-Mgr&v=LATEST&c=executable&e=tar.gz" | sed -n 's|<baseVersion>\(.*\)</baseVersion>|\1|p'`

VERSION=`echo "$VERSION" | tr -d ' '`

echo "Version is $VERSION"

Thanks!

回答1:

Nexus has the following REST API for describing how Maven modules are resolved:

  • Artifact Maven Resolve

Example

To obtain the details about the following artifact:

<groupId>org.cometd.jetty</groupId>
<artifactId>cometd-jetty-client</artifactId>
<version>1.0-SNAPSHOT</version>

Use the following REST API:

https://oss.sonatype.org/service/local/artifact/maven/resolve?r=cometd-snapshots&g=org.cometd.jetty&a=cometd-jetty-client&v=1.0-SNAPSHOT&e=jar

Returns the following report:

<artifact-resolution>
  <data>
    <presentLocally>true</presentLocally>
    <groupId>org.cometd.jetty</groupId>
    <artifactId>cometd-jetty-client</artifactId>
    <version>1.0-20090313.100344-2</version>
    <baseVersion>1.0-SNAPSHOT</baseVersion>
    <extension>jar</extension>
    <snapshot>true</snapshot>
    <snapshotBuildNumber>2</snapshotBuildNumber>
    <snapshotTimeStamp>1236938624000</snapshotTimeStamp>
    <sha1>0cbf7163f19bf4586e27632a1f742dd0c0e0036d</sha1>
    <repositoryPath>/org/cometd/jetty/cometd-jetty-client/1.0-SNAPSHOT/cometd-jetty-client-1.0-20090313.100344-2.jar</repositoryPath>
  </data>
</artifact-resolution>


回答2:

This was an eariler deleted posting proposing an alternative way of assembling distributions from Maven repository content:

Ivy is an alternative dependency management client, which can be run from the command-line as follows:

java -jar ivy.jar -settings ivysettings.xml -dependency org.cometd.jetty cometd-jetty-client 1.0-SNAPSHOT -retrieve "distrib/[artifact]-[revision](-[classifier]).[ext]"

The retrieve option of the ivy command details how the downloaded files should be stored locally:

-- distrib
   |-- cometd-api-1.0-SNAPSHOT.jar
   |-- cometd-jetty-client-1.0-SNAPSHOT.jar
   |-- cometd-jetty-client-1.0-SNAPSHOT-javadoc.jar
   |-- cometd-jetty-client-1.0-SNAPSHOT-sources.jar
   |-- cometd-jetty-server-1.0-SNAPSHOT.jar
   |-- jetty-6.1.15.jar
   |-- jetty-client-6.1.15.jar
   |-- jetty-sslengine-6.1.15.jar
   |-- jetty-util5-6.1.15.jar
   |-- jetty-util-6.1.15.jar
   `-- servlet-api-2.5-20081211.jar

The correct timestamped artifact is retrieved but the "SNAPSHOT" revision number is preserved, which is what I understand you're trying to do.

The ivysettings file details the repositories to be used when downloading artifacts:

<ivysettings>
    <settings defaultResolver="repos"/>
    <resolvers>
        <chain name="repos">
            <ibiblio name="central" m2compatible="true"/>
            <ibiblio name="cometd-snapshot" root="https://oss.sonatype.org/content/repositories/cometd-snapshots/" m2compatible="true"/>
        </chain>
    </resolvers>
</ivysettings>


回答3:

The documentation for the Maven Resolve Nexus REST API can be found here: https://maven.java.net/nexus-core-documentation-plugin/core/docs/rest.artifact.maven.resolve.html