4

I am using this question Execute shell script in Gradle as a reference, however, I cannot figure out how to get it work.

This is my gradle file:

...

task myPrebuildTask(type: Exec) {
    println "Hello world from gradle"
    commandLine 'sh', './myScript.sh'
}

build.dependsOn myPrebuildTask

I have this in myScript.sh

#!/bin/sh
echo "Hello world from the script file"

However, whenever I run the script gradle assembleDebug, I can only see "Hello world from gradle" but not "Hello world from the script file".

Yuchens-iMac:MyApplication yuchen$ gradle assembleDebug
Hello world from gradle
Incremental java compilation is an incubating feature.
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
...

Why?

3
  • 1
    try calling your task explicitly as gradle myPrebuildTask Commented Apr 22, 2016 at 22:10
  • 1
    The Hello world from gradle is being printed during the config phase. I suspect your task ins't being executed at all when you call gradle assembleDebug Commented Apr 22, 2016 at 22:13
  • You might be looking for the printed string in the wrong place. "Hello world from gradle" is printed in config phase, but "Hello world from script file" is printed in execution phase, so it should be further down the output. Commented Apr 22, 2016 at 22:15

1 Answer 1

3

I put just this in a new build.gradle

defaultTasks 'myPrebuildTask'

task myPrebuildTask(type: Exec){
    println "Hello world from gradle"
    commandLine 'sh', './myScript.sh'
}

running gradle -q results in:

> gradle -q
Hello world from gradle
Hello world from the script file

Clearly, the task prints the command output just fine.

A couple of things could be hapenning in your case. The task dependency you set up might not be adequate to trigger your custom task when you call gradle assembleDebug or, since the two print lines actually print in different gradle phases, the second line may be elsewhere in your log - you haven't posted the entire output.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the tips for defaultTasks and -q. I tried with your scripts and they do work. But that was not what causing my problem here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.