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.
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.
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?
I think you have to construct it yourself, but it should be straightforward.
Ok, I did some fast spaghetti coding It seems to work. Thank you for the help. Here is my code if somebody else needs something similar.
project.uploadArchives {
onlyIf {
doUpload(project)
}
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) {
if(project.name.endsWith(“SNAPSHOT”)){
return true
}
def jarName = project.name + ‘-’ + project.version + ‘.jar’
def repositoryPath = project.group.replace(".", “/”) + “/” + project.name + “/” + 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
connection.setConnectTimeout(timeoutInMillis)
connection.setReadTimeout(timeoutInMillis)
connection.setRequestMethod(“HEAD”)
def responseCode = connection.getResponseCode()
return (200 <= responseCode && responseCode >= 399)
} catch (IOException ignored) {
return false
} }
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.
Yes, I noticed that it didn’t fully work. But now it fixed I also edited the code above. Thank you again for the help.