我正在使用这个问题Execute shell script in Gradle但是,作为引用,我不知道如何让它工作。
这是我的 gradle 文件:
...
task myPrebuildTask(type: Exec) {
println "Hello world from gradle"
commandLine 'sh', './myScript.sh'
}
build.dependsOn myPrebuildTask
我在 myScript.sh 中有这个
#!/bin/sh
echo "Hello world from the script file"
但是,每当我运行脚本 gradle assembleDebug
时,我只能看到“Hello world from gradle”,而看不到“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
...
为什么?
最佳答案
我把这个放在一个新的 build.gradle
defaultTasks 'myPrebuildTask'
task myPrebuildTask(type: Exec){
println "Hello world from gradle"
commandLine 'sh', './myScript.sh'
}
运行 gradle -q
结果:
> gradle -q
Hello world from gradle
Hello world from the script file
很明显,该任务可以很好地打印命令输出。
您的情况可能会发生一些事情。当您调用 gradle assembleDebug
时,您设置的任务依赖性可能不足以触发您的自定义任务,或者,由于两条打印行实际上在不同的 gradle 阶段打印,因此第二行可能在您的日志中的其他地方- 你还没有发布完整的输出。
关于android - 在gradle中执行shell脚本看不到echo输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36803742/