What you should know going into this guide


You can use basic authentication with the API
Authorization: Basic <BASE64 Goes Here(user:pass)>
You can use shell commands with Gradle
def result = ["ls"].execute()
You can use curl to fetch URLs
curl http://google.com

Using this bit of knowledge we can retrieve specific data needed during the build process. In the case of this guide we will retrieve the current version name for a JIRA project that is currently unreleased. This probably will not be what you will want to do, but it can be done nonetheless.

First you will need your projects key. And assuming you already know how to create the basic authentication base64 encoded "user:pass" string we can move on.


Inside build.gradle

ext {
    def request = [
        "curl", // curl executable 
        "-H", "Authorization: Basic <BASE64 Goes Here(user:pass)>", 
        "-X", "GET", 
        "-H", "Content-Type: application/json",
        "http://issues.mysite.com/jira/rest/api/2/project/<project key>/versions"
    ].execute()
    def versionArray = new JsonSlurper().parseText(request.text) // parse json and return object

    for(def version:versionArray){
        if(!version.released){
            ext.jiraVersionName = version.name
            break;
        }
    }
}

That is it, this should get the first unreleased version name and it can be accessed by the jiraVersionName variable

jar {
    baseName = 'projectname'
    version = "${jiraVersionName}"
}