简体   繁体   English

为什么gradle没有运行我的shell脚本?

[英]Why does gradle not run my shell script?

I'm almost certain i am overlooking something. 我几乎可以肯定我会忽略一些东西。

I have an android gradle project with a build.gradle file. 我有一个带有build.gradle文件的android gradle项目。 Inside here, I specify the task: 在这里,我指定任务:

task doSomething(type: Exec) {
    println("okay clearly you have got to be getting in here")
    commandLine 'sh /Users/dzt/Desktop/create_a_file_on_desktop.sh'
}

and that doesn't run at all. 那根本不运行。 the shell file just literally does: shell文件只是按字面意思执行:

#!/bin/sh
echo "hi" > /Users/dzt/Desktop/i_am_a_byproduct.txt

and i ran chmod u+x on it so it is executable (i double checked on regular bash shell). 我在它上面运行了chmod u+x ,因此它是可执行的(我在常规的bash shell上进行了双重检查)。

I also tried to use the groovy command: 我也尝试使用groovy命令:

"cd ../ && sh /Users/dzt/Desktop/create_a_file_on_desktop.sh".execute()

which does not work either. 这也不起作用。 I'm a little stumped. 我有点难过。 i do NOT see the output file. 我没有看到输出文件。 however, i do see my print statement in the gradle console. 但是,我确实在gradle控制台中看到了我的print语句。

What is going on here? 这里发生了什么?

** EDIT ** **编辑**

okay, i drilled it down more -> 好吧,我钻了更多 - >

cd ../ does not work at all. cd ../根本不起作用。 why is this? 为什么是这样? i need to use a relative path, at least relative to this directory 我需要使用相对路径,至少相对于此目录

The call must be 电话必须是

commandLine 'sh', '/Users/dzt/Desktop/create_a_file_on_desktop.sh'

or else this is considered one command. 否则这被认为是一个命令。 But you want to start the sh with the script as param. 但是你想用脚本作为参数来启动sh On the other hand, since you have set the execute-bit, you can as well just call the shell script directly. 另一方面,由于您已设置了execute-bit,因此您也可以直接调用shell脚本。

See http://gradle.org/docs/current/dsl/org.gradle.api.tasks.Exec.html 请参阅http://gradle.org/docs/current/dsl/org.gradle.api.tasks.Exec.html

Running cd like you want with cd ../ && sh script does also not work like this, since && is a shell script command. 使用cd ../ && sh script运行cd也不会像这样工作,因为&&是一个shell脚本命令。 If you want to run like this, you have to run the shell and make it run as a command. 如果要像这样运行,则必须运行shell并使其作为命令运行。 Eg 例如

commandLine 'sh', '-c', 'cd ~/scripts && sh myscript.sh'

Gradle does not allow cd command for some reason. 由于某种原因,Gradle不允许cd命令。 some commands just do NOT work using groovy. 一些命令只是不使用groovy工作。

instead, i used cd inside my shell script. 相反,我在我的shell脚本中使用了cd that seems to work just fine. 这似乎工作得很好。

First, you have to put in the root level gradle.build file. 首先,您必须放入根级gradle.build文件。 The you need to write it like this, to actually be able to execute the task. 你需要像这样写它,才能真正执行任务。

task doSomething << {
    group 'yourGroupName'
    description 'Runs your bash script'
    exec {
        workingDir "$projectDir/../pathto/"
        commandLine 'bash', '-c', './bashscript.sh'
    }
}

Then you can execute with: ./gradlew -q doSomething . 然后你可以执行: ./gradlew -q doSomething In this case I used bash , but you can use any supported scripting shell, like sh, perl, python etc. 在这种情况下,我使用了bash ,但你可以使用任何支持的脚本shell,比如sh, perl, python等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM