从Gradle任务中执行Git Bash

3

我有一个bash脚本需要作为任务的一部分来执行。这个脚本在git bash中单独运行很好,但是我无法让gradle运行git bash并执行脚本。

我尝试了类似于以下内容:

task example(type:Exec) {
    workingDir '.'
    executable 'C:\\Program Files\\Git\\bin\\bash.exe'
    args './path/to/script.bash'
}

我甚至尝试了一些更简单的命令,例如pwd或者简单的echo

有没有办法让它工作?

4个回答

0

使用脚本执行命令行操作的最简单方法是像gradle文档中所述的那样。

https://docs.gradle.org/current/dsl/org.gradle.api.tasks.Exec.html

// Windows Way --> Use ',' for executable params
// ('--login' will make sure that profile and so on is loaded correctly so that all commands are available)
commandLine '"C:\\Program Files\\Git\\bin\\bash.exe"', '--login'
args './script.sh'

// Unix Way... (Just use the script)
commandLine './script.sh'

然后您可以将它组合起来:

if (System.properties['os.name'].toLowerCase().contains('windows')) {
    // Use git bash on windows...
    commandLine '"C:\\Program Files\\Git\\bin\\bash.exe"', '--login'
    args './script.sh'
} else {
    // UNIX Style Operating Systems
    commandLine './script.sh'
}

但我认为这并不是一个很好的解决方案... 最好有一个插件,以一种通用的、独立于操作系统的方式尝试完成脚本中的任务。 但首先要让它在多个操作系统上运行,可以将其用作起点。


0

我建议你先在命令行中正确执行,然后再尝试在Gradle中执行。从DOS提示符(而不是git bash)执行以下操作是否有效?

"C:\Program Files\Git\bin\bash.exe" ./path/to/script.bash

如果在这里失败了,Gradle 中肯定也会失败


我已经尝试在Windows命令提示符下运行它,并且它可以正常工作,它似乎在全新的项目上也能正常工作,我猜测我的真实项目配置存在问题导致它无法正常工作。感谢您的帮助。 - maydawn

0
我这样做使其可用。它无论如何都会启动 git-bash shell!
exec {
     commandLine '"C:\\Program Files\\Git\\git-bash.exe"'
     args  '-i --cd=." ./encryptkey.sh'
     standardOutput = encryptedKeyStdOut
     standardInput = keyStdIn
}

0

你看过这里了吗? https://docs.gradle.org/current/dsl/org.gradle.api.tasks.Exec.html
我猜你可能是以错误的方式指定参数。

对于 Mac 用户来说,这很容易:

task example(type:Exec) {
    workingDir '.'
    commandLine './echo_example.sh'
}

对于 Windows,只需根据上面的文档在命令行中指定命令。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接