Nexus 3 : how to get latest snapshot?

前端 未结 5 1647
日久生厌
日久生厌 2021-02-10 05:20

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 snap

5条回答
  •  终归单人心
    2021-02-10 05:47

    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 : --header "Content-Type:application/json" http:///nexus/service/siesta/rest/v1/script -d @version.json
    

    You can then test it easily with :

    curl -v -X POST -u : --header "Content-Type: text/plain" "http:///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 !

提交回复
热议问题