Nexus 3 : how to get latest snapshot?
Asked Answered
P

5

7

As we all know Nexus 3 does not have REST API yet, which is very weird for me. I can only download artifacts manually using wget or curl. But as I'm using Maven 3, all the snapshots artifacts are named using timestamps like this :

myartifact-1.0-20161215.141522-4.tar.gz

So I want to know how I can get latest snapshots from a repo? I want to automate the download process of artifacts but as names change I didn't find any way to achieve this.

Thanks.

Pons answered 15/12, 2016 at 14:35 Comment(7)
Just curious: Why do you use Nexus 3 together with Maven?Cecillececily
Because our project is based on maven(Java), what would you do?Pons
We use Nexus 2.x until Nexus 3 fully satisfies our needs.Cecillececily
Just to mention: I'm not sure about Nexus, but it's possible with Artifactory. E.g. I created a tiny script which downloads artifacts for me at work and uses REST API. You can see it here: github.com/pwittchen/ydownloader. If you're able to update your infrastructure, you may consider such option.Danczyk
Sadly we have used Nexus 3 for several months, and cannot go back to Nexus 2Pons
Possible duplicate of Sonatype Nexus 3 - get latest snapshotLeukemia
Nexus 3 now has the REST API you need. #37281318Gillette
L
6

If keeping only one SNAPSHOT in the repository is a valid option

This API call will work: ${nexusUrl}/service/rest/beta/search/assets/download?maven.groupId=${groupId}&maven.artifactId=${artifactId}&maven.baseVersion=${version}

Though there is finally a Nexus3 API (see more on this in Sonatype's blog), the API does not yet provide the means to get the last SNAPSHOT version. This situation will hopefully be improved in the future: NEXUS-14407 is tracking this feature request.

But until then I'm workarounding the problem by defining a Maven - Delete SNAPSHOT Task:

Maven - Delete SNAPSHOT

and configuring it to run every minute deleting all but 1 snapshot versions older than 0 days:

enter image description here

Leukemia answered 27/6, 2018 at 9:23 Comment(1)
This is a very useful answer. Asof April 2019 the feature has been added. The documentation is here community.sonatype.com/t/….Gillette
L
6

You can create a script in Groovy and upload it to Nexus to do what you want.

Here's an example of a script I used to write to return all versions for a given group and repository.

Content of version.json :

{
  "name": "version",
  "type": "groovy",
  "content": "import org.sonatype.nexus.repository.storage.Query;
    import org.sonatype.nexus.repository.storage.StorageFacet;
    import groovy.json.JsonOutput;

    def groupId = args.split(',')[0];
    def repositoryId = args.split(',')[1];

    def repo = repository.repositoryManager.get(repositoryId);
    StorageFacet storageFacet = repo.facet(StorageFacet);
    def tx = storageFacet.txSupplier().get();

   tx.begin();
   def components = tx.findComponents(Query.builder().where('group = ').param(groupId).build(), [repo]);
   def found = components.collect {
   def baseVersion = it.attributes().child('maven2').get('baseVersion');
   \"${baseVersion}\"
   };
   found = found.unique();
   tx.commit();
   def result = JsonOutput.toJson(found);

   return result;"
}

The interesting part here is the tx.findComponents() that returns generic Component class. This class provides extra information about its container with the function attributes(). You can then use it to get the baseVersion i.e. the version Maven used to use (with the -SNAPSHOT suffix).

To install this script, just run the following :

curl -v -X POST -u <NEXUS_ADMIN>:<NEXUS_PWD> --header "Content-Type:application/json" http://<SERVER_NEXUS>/nexus/service/siesta/rest/v1/script -d @version.json

You can then test it easily with :

curl -v -X POST -u <NEXUS_ADMIN>:<NEXUS_PWD> --header "Content-Type: text/plain" "http://<SERVER_NEXUS>/nexus/service/siesta/rest/v1/script/version/run" -d "com.my.groupid,snapshots"

This will return all versions you want as you wanted :

{
  "name" : "version",
  "result" : "[\"1.5.2-SNAPSHOT\",\"1.5.3-SNAPSHOT\",\"1.6.1-SNAPSHOT\",\"1.5.0-SNAPSHOT\"]"
}

Hope this'll help !

Luisluisa answered 2/5, 2017 at 12:31 Comment(1)
I would highly recommend running this inside a try/finally block and closing the transaction (via tx.close()) inside the finally portion. I used this as the starting point for a script and after 12-48 hours of running my Nexus installation would hang and no longer respond to artifact requests. This script is even included in a ticket that describes my exact situation.Lucielucien
L
6

If keeping only one SNAPSHOT in the repository is a valid option

This API call will work: ${nexusUrl}/service/rest/beta/search/assets/download?maven.groupId=${groupId}&maven.artifactId=${artifactId}&maven.baseVersion=${version}

Though there is finally a Nexus3 API (see more on this in Sonatype's blog), the API does not yet provide the means to get the last SNAPSHOT version. This situation will hopefully be improved in the future: NEXUS-14407 is tracking this feature request.

But until then I'm workarounding the problem by defining a Maven - Delete SNAPSHOT Task:

Maven - Delete SNAPSHOT

and configuring it to run every minute deleting all but 1 snapshot versions older than 0 days:

enter image description here

Leukemia answered 27/6, 2018 at 9:23 Comment(1)
This is a very useful answer. Asof April 2019 the feature has been added. The documentation is here community.sonatype.com/t/….Gillette
G
3

This is currently not possible. We are looking at use cases like this one specifically as we build the REST API and you should expect this functionality in the nearish future.

As alluded to in the comments, you might have a use case where it would be best to still use Nexus Repository Manager 2, for the time being. If you are handy with Groovy and scripting, you might also extend Nexus Repository Manager 3 via the Integration API to do what you describe (and I for one would love to see that). More info on using that is here: https://books.sonatype.com/nexus-book/reference3/scripting.html

Glauconite answered 15/12, 2016 at 15:29 Comment(4)
Hi are you Nexus developer? Yes I'm currently looking at the Groovy REST API, I made a Maven project and I would like to know do I suppose to upload the script to my Nexus repo and run the script by curl?Pons
I am SORT OF a developer, meaning I write bad code from time to time. There are a plethora of scripts here (including shell scripts for upload, etc...) that you can lean on to do what you want to do: github.com/sonatype/nexus-book-examples/tree/nexus-3.x/…Glauconite
haha thank you, I already did a git clone but there's almost nothing in the example, only a javadoc which is not so useful.Pons
You will want to use this script to upload a file (changing the username/password if necessary) github.com/sonatype/nexus-book-examples/blob/nexus-3.x/… and an example json file that you could modify with your script would be something akin to this: github.com/sonatype/nexus-book-examples/blob/nexus-3.x/…Glauconite
D
2

Using the new REST API on Nexus v3 (exact version 3.17.0) the following request to retrieve the latest snapshot worked for me:

/service/rest/v1/search/assets/download?sort=version&maven.artifactId=artifactId&maven.baseVersion=x.y.z-SNAPSHOT&maven.extension=jar
Dietrich answered 12/12, 2019 at 17:35 Comment(0)
P
0

I found the answer in another post here: Nexus 3 Rest api to check if component exist

I commented in the

found = found.unique().sort(); 

line and called it with the following parameters:

-d'<reponame>,<groupid>,<artifactid>,<version>-SNAPSHOT,latest'

YMMV but this did it for me. I was then able to construct a wget with the result of this query.

Perichondrium answered 20/10, 2017 at 18:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.