maanantai 29. joulukuuta 2014

Sonatype Nexus not returning the latest releases through REST interface

Sonatype's Nexus Maven repository behaves strangely after using Rebuild Metadata feature from the Nexus UI. For some reason this feature updates all maven-metadata.xml files under repository and adds this line to all of them

<latest>0.0.48</latest>

The added line breaks Nexus's REST query API so that it always returns the version given in the latest element instead of the real latest released version. So, for example this query would always return version 0.0.48, if the latest element is in maven-metadata.xml file.

$ curl
"http://<hostname>:<port>/nexus/service/local/artifact/maven/redirect?r=releases-repository-name&g=com.group&a=artifact-name&e=jar.md5&v=LATEST

To fix this, you can run the following command under the broken repository to remove all latest elements.

$ find . -name maven-metadata.xml -exec sed -i -r "/<latest>.+<\/latest>/d" \{\} \;

An alternative way to query the latest releases is the following.

$ curl
"http://<hostname>:<port>/nexus/service/local/artifact/maven/redirect?r=releases-repository-name&g=com.group&a=artifact-name&e=jar.md5&v=RELEASE

This query works even with the latest element.

It seems that latest element is not affecting queries of the latest SNAPSHOTs.

For more information about this subject.