Skip to content

Instantly share code, notes, and snippets.

@DALDEI
Created December 30, 2015 18:52
Show Gist options
  • Save DALDEI/40925568cef57e16e8a7 to your computer and use it in GitHub Desktop.
Save DALDEI/40925568cef57e16e8a7 to your computer and use it in GitHub Desktop.
Gradle Artifactory Publish - Conditionally skip publishing

Conditionally publishing with Artifactory in Gralde

The artifactory gradle plugin ( https://www.jfrog.com/confluence/display/RTF/Gradle+Artifactory+Plugin ) documents a property to skip publications.
artifactoryPublish.skip = true However this doesnt fully disable artifactory. It still publishes a "Build" with no artifacts. To conditionally skip a publication the following code block seems to work. This is useful, for example, if you want the same build to run in a CI server and standalone and/or if you want to run test or dev builds without publishing then rerun the same build with only a property change to have it publish - reguardless of if artifactoryPublish task is invoked. CI Servers like TeamCity will always pass artifactoryPublish if you enable artifactory for a build, this allows the publication to be skipped conditionally.

Witout Line 63 the code snippet above the following results

Note that the build descriptor is actually published even though artifactoryPublish.skip = true

$ gradle   build artifactoryPublish
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar UP-TO-DATE
:startScripts UP-TO-DATE
:distTar UP-TO-DATE
:distZip UP-TO-DATE
:war UP-TO-DATE
:assemble UP-TO-DATE
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:buildDashboard UP-TO-DATE
:check UP-TO-DATE
:build UP-TO-DATE
:artifactoryPublish
Deploying build descriptor to: http://foobar.artifactoryonline.com/nexstra/api/build
Build successfully deployed. Browse it in Artifactory under http://foobar.artifactoryonline.com/nexstra/webapp/builds/myproduct/1451501182455

BUILD SUCCESSFUL

Total time: 2.349 secs
$

With line 62

$ gradle   build artifactoryPublish
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar UP-TO-DATE
:startScripts UP-TO-DATE
:distTar UP-TO-DATE
:distZip UP-TO-DATE
:war UP-TO-DATE
:assemble UP-TO-DATE
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:buildDashboard UP-TO-DATE
:check UP-TO-DATE
:build UP-TO-DATE
:artifactoryPublish SKIPPED

BUILD SUCCESSFUL

Total time: 2.23 secs

And to do a full publish

$ gradle   build artifactoryPublish -Partifactory
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar UP-TO-DATE
:startScripts UP-TO-DATE
:distTar UP-TO-DATE
:distZip UP-TO-DATE
:war UP-TO-DATE
:assemble UP-TO-DATE
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:buildDashboard UP-TO-DATE
:check UP-TO-DATE
:build UP-TO-DATE
:generateDescriptorFileForIvyJavaPublication
:generatePomFileForMavenJavaPublication
:artifactoryPublish
Deploying artifact: http://foobar.artifactoryonline.com/foobar/libs-snapshot-local/com/foobar/vendorserver/2.0.0-SNAPSHOT/vendorserver-2.0.0-SNAPSHOT.pom
Deploying artifact: http://foobar.artifactoryonline.com/foobar/libs-snapshot-local/com/foobar/vendorserver/2.0.0-SNAPSHOT/vendorserver-2.0.0-SNAPSHOT.war
Deploying artifact: http://foobar.artifactoryonline.com/foobar/libs-snapshot-local/com/foobar/vendorserver/ivy-2.0.0-SNAPSHOT.xml
Deploying build descriptor to: http://foobar.artifactoryonline.com/foobar/api/build
Build successfully deployed. Browse it in Artifactory under http://foobar.artifactoryonline.com/foobar/webapp/builds/vendorserver/1451501357555

BUILD SUCCESSFUL

Total time: 3.069 secs
$
/*
* The artifactory gradle plugin ( https://www.jfrog.com/confluence/display/RTF/Gradle+Artifactory+Plugin )
* documents a property to skip publications.
* artifactoryPublish.skip = true
* However this doesnt fully disable artifactory. It still publishes a "Build" with no artifacts.
* To conditionally skip a publication the following code block seems to work.
* This is useful, for example, if you want the same build to run in a CI server and standalone
* and/or if you want to run test or dev builds without publishing then rerun the same build
* with only a property change to have it publish.
*/
// Include the Artifactory plugin as documented
plugins {
id "com.jfrog.artifactory" version "4.0.0"
}
// Use a publishing mechanims
apply plugin: 'maven-publish'
artifactory {
/*
* Configure artifatory as documented
*/
publish {
// ...
// repository {
// ..
}
defaults {
// define your publications
publications( 'ivyJava' , 'mavenJava' )
}
}
// ... rest of build script
// Declare your publications -- as documented
publishing {
publications {
mavenJava(MavenPublication) {
from components.java // components.web etc
}
ivyJava(IvyPublication) {
revision '123'
//...
artifact {
name "project"
// ..
}
}
}
}
/*
* Lastly -- conditionally disable publicaiton
*
* For example: require -Partifactory in order to publish
*/
if( ! rootProject.hasProperty("artifactory") ){
artifactoryPublish.skip = true ; // as documented
artifactoryPublish.onlyIf { false } ; // NOT documented
}
@DALDEI
Copy link
Author

DALDEI commented Jan 6, 2016

While this does stop the publication, it doesn't work as well as I thought. The Artifactory plugin to TeamCity gets very confused messing up future builds until a complete clean is done.

@eyalbe4
Copy link

eyalbe4 commented Jan 9, 2016

Here's something that might help:.
You can disable or enable the build-info publication to Artifactory by setting publishBuildInfo to false or true. This property is documented as part of the plugin's DSL in the Gradle Artifactory Plugin Documentation.

@IceNine46
Copy link

IceNine46 commented Oct 4, 2017

I was running into this issue as well and found adding this to the publishTask will stop the artifacts and the build info from being published.
artifactory {
publish {
if (!rootProject.hasProperty("artifactory")) {
logger.lifecycle("Disabling publication settings.")
publishBuildInfo = false //Publish build-info to Artifactory (true by default)
publishArtifacts = false //Publish artifacts to Artifactory (true by default)
publishPom = false //Publish generated POM files to Artifactory (true by default).
publishIvy = false
}
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment