Android 在 Gradle 中执行 shell 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25562207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Execute shell script in Gradle
提问by Paul Latzelsperger
I have a gradle build setup at the beginning of which I want to execute a shellscript in a subdirectory that prepares my environment.
我在开始时有一个 gradle 构建设置,我想在准备我的环境的子目录中执行一个 shellscript。
task build << {
}
task preBuild << {
println 'do prebuild stuff:'
}
task myPrebuildTask(type: Exec) {
workingDir "$projectDir/mySubDir"
commandLine './myScript.sh'
}
build.dependsOn preBuild
preBuild.dependsOn myPrebuildTask
However, when I execute the task either by calling gradle myPrebuildTask
or by simply building the project, the following error occurs:
但是,当我通过调用gradle myPrebuildTask
或简单地构建项目来执行任务时,会发生以下错误:
> A problem occurred starting process 'command './myScript.sh''
Unfortunately, thats all I get.
I have also tried the following - same error.
不幸的是,这就是我所得到的。
我也尝试了以下 - 同样的错误。
commandLine 'sh mySubDir/myScript.sh'
I use Gradle 1.10 (needed by Android) on Windows, inside a Cygwin shell. Any ideas?
我在 Windows 上的 Cygwin shell 中使用 Gradle 1.10(Android 需要)。有任何想法吗?
回答by Rene Groeschke
use
用
commandLine 'sh', './myScript.sh'
your script itself is not a program itself, that's why you have to declare 'sh' as the program and the path to your script as an argument.
您的脚本本身不是程序本身,这就是为什么您必须将“sh”声明为程序并将脚本的路径声明为参数。
回答by Patrice M.
A more generic way of writing the exec task, but portable for Windows/Linux, if you are invoking a command file on the PATH:
一种更通用的编写 exec 任务的方法,但对于 Windows/Linux 是可移植的,如果您在 PATH 上调用命令文件:
task myPrebuildTask(type: Exec) {
workingDir "$projectDir/mySubDir"
if (System.getProperty('os.name').toLowerCase(Locale.ROOT).contains('windows')) {
commandLine 'cmd', '/c', 'mycommand'
} else {
commandLine 'sh', '-c', 'mycommand'
}
}
This doesn't directly address the use case for the OP (since there is script file in the working directory), but the title of the question is more generic (and drew me here), so it could help someone maybe.
这并没有直接解决 OP 的用例(因为工作目录中有脚本文件),但问题的标题更通用(并将我吸引到这里),因此它可能可以帮助某人。
回答by panser
unfortunately options with commandLinenot worked for me in any way and my friend find other way with executable
不幸的选项命令行不会以任何方式工作,我和我的朋友找到其他办法的可执行
executable "./myScript.sh"
and full task would be
完整的任务是
task startScript() {
doLast {
exec {
executable "./myScript.sh"
}
}
}
回答by pigswig
This works for me in my Android project
这在我的 Android 项目中对我有用
preBuild.doFirst {
println("Executing myScript")
def proc = "mySubDir/myScript.sh".execute()
proc.waitForProcessOutput(System.out, System.err)
}
See here for explanation: How to make System command calls in Java/Groovy?
请参阅此处的说明: 如何在 Java/Groovy 中进行系统命令调用?
回答by Sattar
for kotlin gradle you can use
对于 kotlin gradle,您可以使用
Runtime.getRuntime().exec("./my_script.sh")
回答by Ayush Goyal
I copied my shell scipt to /usr/local/bin
with +x
permission and used it as just another command:
我/usr/local/bin
经+x
许可复制了我的 shell scipt并将其用作另一个命令:
commandLine 'my_script.sh'