Using the Nexus rest API to get latest artifact version for given groupid/artifactId

后端 未结 6 1180
走了就别回头了
走了就别回头了 2020-11-27 14:53

I am trying to use the nexus REST api to get the latest version of a maven artifact. I am able to browse to the specific version I am looking for using http://repo.loc

相关标签:
6条回答
  • 2020-11-27 15:20

    For latest versions of Nexus (since 3.16.0): Example of downloading the latest version (stored in format of zip file) from maven releases repository, using browser:

    http://<YourNexusUrl>/service/rest/v1/search/assets/download?sort=version&repository=maven-releases&maven.groupId=<yourGroupID>&maven.artifactId=<yourArtifactID>&maven.extension=zip
    
    

    Using curl:

    curl -L -X GET "http://<YourNexusUrl>/service/rest/v1/search/assets/download?sort=version&repository=maven-releases&maven.groupId=<yourGroupID>&maven.artifactId=<yourArtifactID>&maven.extension=zip" --output myZip.zip
    
    
    0 讨论(0)
  • 2020-11-27 15:24

    In Nexus LATEST is designed to work with maven plugins rather than with regular artifacts. Nexus simply doesn't guarantee the LATEST to work in other cases. If right now it returns you the correct version of the artifact, tomorrow this may stop working e.g. if you run Rebuild Metadata for the Nexus repository. Do you want to rely on the mechanism that may break at any moment (e.g. during the release process?). I doubt. Read this article for more insight.

    In order to find the LATEST artifact version you should either write your own script to invoke Search API and sort artifact versions as you want. Or you can write your own plugin for Nexus.

    Alternatively, if your workflow allows that, you can use SNAPSHOTs instead of release versions. If you don't increment numeric part, then x.y.z-SNAPSHOT will always return the latest binary.

    Last point: do not use latest versions of artifacts, in the vast majority of cases if you have such a use case, then something is wrong with your deployments (or whatever you're doing). In general you should know the exact version you're going to use.

    0 讨论(0)
  • 2020-11-27 15:30

    After trying the REST service with the LATEST version, and discovering it doesn't always work (See @Stanislav response) 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
    

    Just change the to the appropiate url, and it should work for you.

    Cheers

    0 讨论(0)
  • 2020-11-27 15:31

    This answer has been copied from: https://stackoverflow.com/a/39485361/1450799

    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 --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
    
    0 讨论(0)
  • 2020-11-27 15:37

    The following URL will retrieve the latest version of log4j 1.2.x:

    http://repository.sonatype.org/service/local/artifact/maven/redirect?r=central-proxy&g=log4j&a=log4j&v=LATEST

    Documented here

    Update

    Example using curl:

    curl -L "http://repository.sonatype.org/service/local/artifact/maven/redirect?r=central-proxy&g=log4j&a=log4j&v=LATEST" -o log4j.jar
    

    Update for Log4j2

    Log4j 1.2 is EOL since summer 2015 and is known to be broken in Java 9.

    Here is the link for the Log4j artifacts:

    • log4j-api: https://repository.sonatype.org/service/local/artifact/maven/redirect?r=central-proxy&g=org.apache.logging.log4j&a=log4j-api&v=LATEST
    • log4j-core: https://repository.sonatype.org/service/local/artifact/maven/redirect?r=central-proxy&g=org.apache.logging.log4j&a=log4j-core&v=LATEST
    0 讨论(0)
  • 2020-11-27 15:47

    In the last version of Nexus API is documented the way to retrieve the latest version of a component, filtering by repository, group, artifact and base version

    Example: http://localhost:8081/service/rest/v1/search/assets/download?sort=version&repository=maven-snapshots&maven.groupId=org.foo.bar&maven.artifactId=project&maven.baseVersion=1.2.3-SNAPSHOT&maven.extension=jar

    Reference documentation: https://help.sonatype.com/repomanager3/rest-and-integration-api/search-api#SearchAPI-SearchComponents

    0 讨论(0)
提交回复
热议问题