Gradle Task

Task: A task is an atomic piece of work which a build performs. Examples include compiling classes, generating javadoc, or packaging compiled files into a JAR file.

Exec Task

Executes some external command.

Simple Exec task demo

In Kotlin Script (.kts)

task<Exec>("myTask") {
    // configuration, sets the command line to later be executed.
    commandLine("echo", "Command line: Hello, world!")

    doFirst {
        println("doFirst: Hello, world!")
    }
    doLast {
        println("doLast: Goodbye, world!")
    }
}

Running:

./gradlew myTask

Output:

> Task :myTask
doFirst: Hello, world!
Command line: Hello, world!
doLast: Goodbye, world!
Configuration Stage Demo/commandLine over-write
task<Exec>("only-single-command-line-will-run"){
    // These two commandLine are meaningless since they will be overwritten, within
    // doFirst, by the commandLine in doFirst.
    //
    // The Exec task holds a single command line to run. commandLine does not
    // run the command, it sets the command line variable on the Exec instance.
    commandLine("echo", "configuration set echo-1")
    commandLine("echo", "configuration set echo-2")

    println("configuration-printl1")
    println("congiguration-printl2")

    doFirst {
        println("doFirst-printl")

        // This is the only commandLine that matters.
        commandLine("echo", "doFirst commandLine echo-1")
    }

    doLast {
        // doLast is executed after the task action has executed, so setting the
        // command line in here won't do anything useful.
        commandLine("echo", "doLast commandLine echo-1")

        println("doLast printl-1")
        println("doLast printl-2")
    }
}

Command to reproduce:

gt.sandbox.checkout.commit b971e77 \
&& cd "${GT_SANDBOX_REPO}/build_system/gradle/example" \
&& cmd.run.announce "gradle only-single-command-line-will-run"

Recorded output of command:


> Configure project :
configuration-printl1
congiguration-printl2

> Task :only-single-command-line-will-run
doFirst-printl
doFirst commandLine echo-1
doLast printl-1
doLast printl-2

BUILD SUCCESSFUL in 590ms
1 actionable task: 1 executed
Example running HelloWorld shell script

Command to reproduce:

gt.sandbox.checkout.commit 9178a8b \
&& cd "${GT_SANDBOX_REPO}/build_system/gradle/example_shell_script_taskExec" \
&& cmd.run.announce "gradle runScript"

Recorded output of command:


> Task :runScript
Hello from shell script.

BUILD SUCCESSFUL in 942ms
1 actionable task: 1 executed
afterEvaluate is in Configuration stage

Command to reproduce:

gt.sandbox.checkout.commit 931385e \
&& cd "${GT_SANDBOX_REPO}/build_system/gradle/example" \
&& cmd.run.announce "gradle afterEvaluate-is-in-config-stage"

Recorded output of command:


> Configure project :
configuration-printl1
congiguration-printl2
afterEvaluate-printl1

> Task :afterEvaluate-is-in-config-stage
doFirst-printl
commandLine-setBy-doFirst
doLast printl-1
doLast printl-2

BUILD SUCCESSFUL in 537ms
1 actionable task: 1 executed

Notes

Operator '<<'

Note: the "<<" is a shorthand notation for "doLast"

task B << {
 println 'action'
}

ref


Children
  1. Task Actions in Gradle
  2. task<Exec>