gitgradleartifactorygradle-release-plugin

Gradle release plugin which modify version in gradle.properties can support Jfrog artifactory publish with release version


All steps required are below.

  1. remove '-SNAP' suffix from the version and commit to git repo
  2. create new git tag on this commit
  3. Artifactory Deploy on Jfrog with release version not with -SNAP version
  4. bump version in build.gradle (like pom.xml), and add '-SNAP' suffix.

I used the most popular Gradle Release Plugin (https://github.com/researchgate/gradle-release) but not able to deploy artifactory on Jfrog with release version (it's always taking SNAP version).

Below is my sample code...

gradle.build file

publishing {
    publications {
        mavenJava(MavenPublication) {
            groupId 'in.org'
            artifactId 'some-service'
            version "${version}"
            from components.java
        }
    }
}

artifactory {
    contextUrl = "${artifactory_contextUrl}"
    publish {
        repository {
            repoKey = version.endsWith('SNAP') ? "${snapshot_repository}" : "${release_repository}"
            username = "${artifactory_user}"
            password = "${artifactory_password}"
            maven = true
        }
        defaults {
            publications("mavenJava")
            publishArtifacts = true
            publishPom = true
        }
    }
    resolve {
        repository {
            repoKey = "${release_repository}"
            username = "${artifactory_user}"
            password = "${artifactory_password}"
            maven = true
        }
    }
}

release {
    versionPropertyFile = 'gradle.properties'
    failOnCommitNeeded = false
    failOnPublishNeeded = false
    git {
        requireBranch.set('release')
    }
}

artifactoryDeploy.shouldRunAfter preTagCommit, checkCommitNeeded

updateVersion.doLast {
    def buildGradle = file('gradle.properties')
    buildGradle.text = buildGradle.text.replaceFirst(~/version = (\d++\.\d++\.\d++)/, 'version = \'$1\'')
}

gradle.properties file

artifactory_user=*****
artifactory_password=*****
artifactory_contextUrl=https://jfrog.in/artifactory
version=1.0.1-SNAPSHOT
snapshot_repository=gradle-dev-local
release_repository=gradle-release-local

Solution

  • We need to make project module version dynamic based on environment. If env is prod we should remove -SNAP from the version as mentioned below.

        buildscript {
        repositories {
            jcenter()
            mavenCentral()
            maven {
                url 'https://plugins.gradle.org/m2/'
            }
        }
        dependencies {
            classpath("org.springframework.boot:spring-boot-gradle-plugin:2.7.9")
            classpath(group: 'org.jfrog.buildinfo', name: 'build-info-extractor-gradle', version: '4.29.0')
            classpath 'net.researchgate:gradle-release:3.0.2'
        }
    }
        
    apply plugin: 'maven-publish'
    apply plugin: 'application'
    apply plugin: "com.jfrog.artifactory"
    apply plugin: 'org.springframework.boot'
    apply plugin: 'net.researchgate.release'
    
    def springboot_version = '2.7.3'
    def environment = hasProperty('env') ? env : null
    def artifactory_publish_version = "${environment}" != "prod" ? "${version}" : "${version}".replace("-SNAP", "")
    
    repositories {
        jcenter()
        mavenCentral()
        maven { //Url for local dependencies in dev/qa env
            url "${artifactory_contextUrl}/gradle-dev-local"
            credentials {
                username = "${artifactory_user}"
                password = "${artifactory_password}"
            }
        }
    }
    def artifactory_publish_version = "${environment}" != "prod" ? "${version}" : "${version}".replace("-SNAP", "")
    
    configurations { // this needs to be add for springboot projects
       [apiElements, runtimeElements].each {
           it.outgoing.artifacts.removeIf 
           { it.buildDependencies.getDependencies(null).contains(jar) }
           it.outgoing.artifact(bootJar)
       }
    }
    
    jar {
        enabled = false
    }
    
    publishing {
        publications {
            mavenJava(MavenPublication) {
                groupId 'your-org'
                artifactId 'your-service'
                version "${artifactory_publish_version}"
                from components.java
            }
        }
    }
    
    artifactory {
        contextUrl = "${artifactory_contextUrl}"
        publish {
            repository {
                repoKey = artifactory_publish_version.endsWith('SNAP') ? "${snapshot_repository}" : "${release_repository}"
                username = "${artifactory_user}"
                password = "${artifactory_password}"
                maven = true
            }
            defaults {
                publications("mavenJava")
                publishArtifacts = true
                publishPom = true
            }
        }
        resolve {
            repository {
                repoKey = "${release_repository}"
                username = "${artifactory_user}"
                password = "${artifactory_password}"
                maven = true
            }
        }
    }
    
    release { // To create git tag
        git {
            requireBranch.set('release')
        }
    }