Run Groovy process directly in Gradle


(Sverre Moe) #1

I wanted to share a solution I have been working on, running a groovy process in gradle. All examples I could find revolved around a task with JavaExec. As this is not what I wanted I tried to find other examples, but no such luck (perhaps I did not search with the right phrase).

I did not want it as a task. It should not be available as a task.
I ended up trying to just call a simple groovy method, which worked fine. i wasn’t quite sure if I could run groovy directly in Gradle like that.

I needed to run a command to initialise an ext variable.

ext {
    protobufVersion = getProtobufVersion()
}

dependencies {
    compile group: 'com.google.protobuf', name: 'protobuf-java', version: protobufVersion
}

def getProtobufVersion() {
    final def protobuf = "protoc --version".execute().text
    return protobuf.substring(protobuf.indexOf(" ") + 1).trim()
}