0

I'm building an app with Gradle. We're not currently versioning releases. (It's on the road-map.)
When Deploying releases into UAT or PROD with Jenkins,
It would be nice to automatically calculate the URL of the latest snapshot release in the Repository instead of having to pass in the file name.

How can I calculate the URL (to the filename) of the latest Snapshot when I have:

  • Group
  • Artifact ID
  • Version

?

Raystorm
  • 6,180
  • 4
  • 35
  • 62
  • Isn't there also a `timestamp `included in the URL, making it almost impossible to calculate ? – amer Dec 23 '19 at 15:10
  • 1
    look at this question https://stackoverflow.com/questions/37697622/gradle-get-url-of-dependency-artifact – Yuri G. Dec 23 '19 at 16:26

2 Answers2

1

Assuming you're using nexus, you can hit the URL "above" the jar url (ie the containing folder) which will return some xml which lists all the snapshots and their URLs. You can then parse the xml to get the latest.

There's also the Nexus REST API which might allow you to get the path

There's also the dependency:get plugin which allows you to download the latest snapshot by firing maven at command line (no pom.xml required).

Eg:

mvn dependency:get \
    -DrepoUrl=url \
    -Dartifact=groupId:artifactId:version
lance-java
  • 25,497
  • 4
  • 59
  • 101
0

Thanks to the Link from @Yuri G. and the hint from @lance-java about parsing the XML I was able to figure out a solution.

We're using gradle for the build, and I'm running this on Jenkins, I don't know if maven is installed.

I started with the Gradle Task from the link and then modified it to read the maven-metadata.xml file in the folder. NOTE: we're using Artifactory not Nexus.

task getLatestSnapshotURL() {
        doLast
        {
           def url = artifactoryURL + config.binaryRepository.snapshotUrl

           def appBaseURL = String.format("%s/%s/%s/%s/", url.toString(),
                                          config.groupId.replace('.', '/'),
                                          config.artifactId, version)
                                  .replace('null','')

           def xmlUrl = appBaseURL + "maven-metadata.xml";
           println('metadata.xml URL: ' + xmlUrl)

           def ts = ''
           def build = ''

           try
           {
              def downloadXmlURL = new URL(xmlUrl)
              def fStream = downloadXmlURL.openStream()
              def slurper = new XmlSlurper().parse(fStream)
              println(slurper)
              println(XmlUtil.serialize(slurper))

              def ver = slurper.versioning
              println("versioning: " + ver)
              println(XmlUtil.serialize(ver))

              ts = ver.snapshot.timestamp.text()
              build = ver.snapshot.buildNumber.text()
           }
           catch(Exception ex)
           {
              println("Unexpected Error getting Snapshot Version "
                     + ex.getMessage() + "\n" + ex.getStackTrace());
           }

           println("ts: " + ts)
           println("ts Type: " + ts.getClass())
           println("build: " + build)
           println("build Type: " + build.getClass())

           def warUrl = appBaseURL + String.format("%s-%s-%s-%s.war",
                                                   config.artifactId,
                                                   version.replace("-SNAPSHOT", ''),
                                                   ts, build)
           println('war URL: ' + warUrl)

           try
           {
              def jarfile = new URL(warUrl)
              def inStream = jarfile.openStream();
              if (inStream != null)
              {
                 println(String.format("%s:%s:%s", config.groupId,
                                       config.artifactId, version)
                        + " -> " + warUrl)
                 return
              }
           }
           catch (Exception ignored) { }
        }
     }
Raystorm
  • 6,180
  • 4
  • 35
  • 62