Only execute uploadArchives if version not already exists in the repository

(Martin Odhelius) #1

Is there any easy way to execute uploadArchives (using mavenDeployer) only if the current version of the artifact does not already exist in the repository? I have until now used outputs.upToDateWhen { true } but the problem with that is that I can not run a clean before the build can result in incidents if the component has been refactored. I have tried to find any way for the uploadArchives task to not fail the build, but just give a warning if it fails to upload to the repository, but I have not succeeded.

(Peter Niederwieser) #2

This isn’t something that’s supported out-of-the-box, but you could implement it on your own. For example, you could use the ‘uploadArchives.onlyIf { … }’ hook to construct the repository artifact URL and do a HTTP HEAD request to see if the artifact is there.

(Martin Odhelius) #3

Can I from the maven deployer get the full path to where it will upload the artifact or do I have to build hat myself?

(Peter Niederwieser) #4

I think you have to construct it yourself, but it should be straightforward.

(Martin Odhelius) #5

Ok, I did some fast spaghetti coding :wink: It seems to work. Thank you for the help. Here is my code if somebody else needs something similar.

project.uploadArchives {

onlyIf {



outputs.upToDateWhen { project.onlyUploadArchivesWhenUpdated }

repositories.mavenDeployer {

repository(url: project.repoReleaseUpload) {

authentication(userName: project.repoUsername, password: project.repoPassword)


snapshotRepository(url: project.repoSnapshotUpload) {

authentication(userName: project.repoUsername, password: project.repoPassword)


} }

private static boolean doUpload(Project project) {


return true


def jarName = + ‘-’ + project.version + ‘.jar’

def repositoryPath =".", “/”) + “/” + + “/” + project.version + “/” + jarName

def repositoryUrl = project.repoReleaseUpload + repositoryPath

return !urlExists(repositoryUrl) }

private static boolean urlExists(String repositoryUrl) {

try {

def connection = (HttpURLConnection) new URL(repositoryUrl).openConnection()

def timeoutInMillis = 10000




def responseCode = connection.getResponseCode()

return (200 <= responseCode && responseCode >= 399)

} catch (IOException ignored) {

return false

} }

(Peter Niederwieser) #6

Looks good. Some hints:

  • Method ‘doUpload’ should declare return type ‘boolean’ (or none at all). In its current form, the method will always return ‘null’, which is ‘false’ according to Groovy truth. * ‘true’ needs to be replaced with ‘return true’ (otherwise it’s a noop). * The ‘return’ keyword in ‘return doUpload’ can be omitted.

(Martin Odhelius) #7

Yes, I noticed that it didn’t fully work. But now it fixed :slight_smile: I also edited the code above. Thank you again for the help.