Executing a shell script from gradle

suggest change

A shell script is a very versatile way to extend your build to basically anything you can think of.

As an example, this compiles protobuf files and writes the generated Java into the source tree. afterEvaluate runs the script at the end of configuration, before the Java compile tasks:

def compilePb() {
    exec {
        // NOTICE: gradle will fail if there's an error in the protoc file...
        executable "../pbScript.sh"
    }
}

project.afterEvaluate {
    compilePb()
}

../pbScript.sh is one directory above the build.gradle that runs the task. For a module build file, that is the project root:

#!/usr/bin/env bash
pp=/home/myself/my/proto

/usr/local/bin/protoc -I=$pp \
 --java_out=./src/main/java \
  --proto_path=$pp \
 $pp/my.proto \
 --proto_path=$pp \
 $pp/my_other.proto

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents