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?

Community
  • 1
  • 1
Yuchen
  • 30,852
  • 26
  • 164
  • 234
  • 1
    try calling your task explicitly as `gradle myPrebuildTask` – RaGe Apr 22 '16 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` – RaGe Apr 22 '16 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. – Tomik Apr 22 '16 at 22:15

1 Answers1

2

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.

RaGe
  • 22,696
  • 11
  • 72
  • 104
  • 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. – Yuchen Apr 23 '16 at 20:08