Search code examples
gradleartifactory

Controlled Publishing to Artifactory for multiple artifacts (skipped if already published)


I have a list of publications in my project that is generated dynamically. I wanted to ask if there is a way to do a try catch on each of the publications to continue if there is an error in one of them. The reason is that not all versions are incremented, and they should not be, so I want to push only the ones that are not already existing. I was thinking of doing it either in the publication phase or when I'm generating the publication dynamically.

So imagine I have task publish_1, publish_2, publish_3 I want to make sure that publish_3 still happens even if publish_1 and publish_2 fail.

The reason for the failure that I want to control is if the file is already publish. Another option would be for me somehow to query artifactory before to see if the file is existing (not sure how to do that).

This is an idea of my publication is something like this:

HashMap<String,ProtoFile> artifacts = new HashMap<String,ProtoFile>()
List<String> thePublications = new ArrayList<String>()

publishing {
  artifacts.values().each() { f ->
    def pubName = "publish_"+f.name
    publications.create(pubName, MavenPublication) {
      groupId = f.groupId
      artifactId = f.artifactId
      version = f.version
      artifact(file("build/distributions/" + f.zipFileName))
      thePublications.add(pubName)
    }
  }
}

    deploy {
      publications thePublications.toArray(new String[thePublications.size()])
    }

Solution

  • I did not get exactly what I wanted, so I'll leave it open but ended up finding a way to make this work by just checking if the file is existing in artfiactory

    def addPublication(publicationName, fileProperties, artifactoryPublications) {
      final String artifactBaseUrl = 'https://your.repo.here/artifactory'
      final String artifactFile = fileProperties.artifactGroupUrl + '/' + fileProperties.artifactId + '/' + fileProperties.version + '/' +  fileProperties.zipFileName
    
      Artifactory artifactory = ArtifactoryClientBuilder.create()
              .setUrl(artifactBaseUrl)
              .build()
    
      ItemHandle result = artifactory.repository('/local-m2-development/').file(artifactFile)
    
      try {
        result.info();
        println fileProperties.zipFileName + " is being skipped because it's already in artifactory with version " + fileProperties.version
      } catch(Exception e) {
        artifactoryPublications.add(publicationName)
      }
    }