Sonatype Nexus REST Api fetch latest build version

2019-01-22 08:45发布

问题:

How can I can use the Sonatype REST Api to fetch the build with the highest version (latest temporal build)?

http://MY_REPOSITORY/nexus/service/local/lucene/search?a=ARTIFACT_NAME&v=ARTIFACT_VERSION

Passing a build version as ARTIFACT_VERSION works. Passing v=LATEST or v=latest does NOT return the latest build.

回答1:

It is not documented that /service/local/lucene/search support "LATEST" as version parameter [link] The OSS rest api documentation states that /service/local/artifact/maven [link] (to get the artifact pom file) and /service/local/artifact/maven/content [link] (to get the actual file content) does support it:

Version of the artifact (Required) Supports resolving of "LATEST", "RELEASE" and snapshot versions ("1.0-SNAPSHOT") too.

So I think you should use one of them (you will have to supply them also with repositoryId and groupId) for example:

http://MY_REPOSITORY/nexus/service/local/artifact/maven/content?r=repoId&g=groupName&a=art&v=LATEST


回答2:

I had the same problem and solved it like this using the lucene search api:

if [[ "${REPO}" == "snapshots" ]]; then
  version=$( curl --silent "http://${HOST}/nexus/service/local/lucene/search?g=${GROUP_ID}&a=${ARTIFACT}" | sed -n 's|<latestSnapshot>\(.*\)</latestSnapshot>|\1|p' | sed -e 's/^[ \t]*//' | tail -1 )
else
  version=$( curl --silent "http://${HOST}/nexus/service/local/lucene/search?g=${GROUP_ID}&a=${ARTIFACT}" | sed -n 's|<latestRelease>\(.*\)</latestRelease>|\1|p' | sed -e 's/^[ \t]*//' | tail -1 )
fi

curl -o ~/${ARTIFACT}-${VERSION}.zip -L -#  "http://${HOST}/nexus/service/local/artifact/maven/redirect?r=${REPO}&g=${GROUP_ID}&a=${ARTIFACT}&e=zip&v=${VERSION}"


回答3:

Lucene search API also allow keyword search for version:

http://<nexus_repository>/nexus/service/local/lucene/search?a=ARTIFACT_NAME&v=1.0.*


回答4:

I have Linux OS and I do not have access to REST API, so I used following commands to get the latest version of snapshots from Nexus:

An example snapshots maven-metadata.xml from WSO2 repository:

$ curl -s "http://maven.wso2.org/nexus/content/repositories/snapshots/org/wso2/is/wso2is/maven-metadata.xml"
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
  <groupId>org.wso2.is</groupId>
  <artifactId>wso2is</artifactId>
  <versioning>
    <latest>5.3.0-SNAPSHOT</latest>
    <release></release>
    <versions>
      <version>5.1.0-SNAPSHOT</version>
      <version>5.2.0-SNAPSHOT</version>
      <version>5.3.0-SNAPSHOT</version>
    </versions>
    <lastUpdated>20160914062755</lastUpdated>
  </versioning>
</metadata>

Extracting from latest XML tag inside maven-metadata.xml:

curl -s "http://maven.wso2.org/nexus/content/repositories/snapshots/org/wso2/is/wso2is/maven-metadata.xml" | \
grep "<latest>.*</latest>" | \
sed -e "s#\(.*\)\(<latest>\)\(.*\)\(</latest>\)\(.*\)#\3#g"

Extracting from version XML tag inside maven-metadata.xml:

curl -s "http://maven.wso2.org/nexus/content/repositories/snapshots/org/wso2/is/wso2is/maven-metadata.xml" | \
grep "<version>.*</version>" | \
sort | uniq | tail -n1 | \
sed -e "s#\(.*\)\(<version>\)\(.*\)\(</version>\)\(.*\)#\3#g"

The result of both of the commands until today 14 Sep 2016 is:

5.3.0-SNAPSHOT


回答5:

After trying the REST service with the LATEST version (and discovering it doesn't always work) I ended up creating this one-liner Linux command for parsing the metadata.xml file:

wget -O - -o /dev/null https://repo1.maven.org/maven2/org/brutusin/wava/maven-metadata.xml | grep -Po '(?<=<version>)([0-9\.]+(-SNAPSHOT)?)' | sort --version-sort -r| head -n 1