在Windows机器上使用gradle执行shell脚本(从gradle调用sbt)
在Windows机器上运行的gradle能够执行shell脚本吗?
为了澄清,我想运行以下脚本: https://github.com /dwijnand/sbt-extras/blob/master/sbt
它应该看起来有点像这样:
tasks.register('mytask') {
doLast {
exec {
workingDir '.'
command 'sbt'
args 'fastOptJS'
}
}
}
更新
按照 Tripleees 的回答,我找到了针对我的具体问题的可接受的解决方案:
private def buildUiWithInstalledSbt() {
try {
exec {
workingDir '.'
commandLine 'cmd', '/C', 'sbt', 'fastOptJS'
}
return true
} catch (ignored){
logger.info("sbt is not installed on this system, trying to run sbt from sbt-extras ...")
return false
}
}
private def buildUiWithSbtExtras() {
try {
exec {
workingDir '.'
commandLine 'curl', 'https://raw.githubusercontent.com/dwijnand/sbt-extras/master/sbt', '-o', 'sbt'
}
} catch (Exception e){
logger.warn("Unable reach sbt-extras repository. " +
"Failure is imminent if this is the first time this build is executed on this machine. " +
"Reason: " + e.toString())
}
try {
exec {
workingDir '.'
commandLine 'sh', 'sbt', 'fastOptJS'
}
return true
} catch (Exception e){
logger.warn("Unable to execute sbt from sbt-extras. Reason: " + e.toString())
return false
}
}
tasks.register('buildui') {
doLast {
def success = buildUiWithInstalledSbt()
if(!success && !buildUiWithSbtExtras()){
throw new GradleException(
"Unable to build the ui javascript file with sbt. " +
"Possible solutions:\n" +
"1. Install SBT on your machine\n" +
"OR\n" +
"2. Prepare your systme to run 'sh' commands, " +
"e.g. install the git bash & add 'C:\\Program Files\\Git\\bin' to your PATH environment variable."
)
}
}
}
Is gradle running on a windows machine able to execute shell scripts?
To clarify, I want to run the following script: https://github.com/dwijnand/sbt-extras/blob/master/sbt
It should look somewhat like this:
tasks.register('mytask') {
doLast {
exec {
workingDir '.'
command 'sbt'
args 'fastOptJS'
}
}
}
UPDATE
Following tripleees answer, I found an acceptable solution for my specific problem:
private def buildUiWithInstalledSbt() {
try {
exec {
workingDir '.'
commandLine 'cmd', '/C', 'sbt', 'fastOptJS'
}
return true
} catch (ignored){
logger.info("sbt is not installed on this system, trying to run sbt from sbt-extras ...")
return false
}
}
private def buildUiWithSbtExtras() {
try {
exec {
workingDir '.'
commandLine 'curl', 'https://raw.githubusercontent.com/dwijnand/sbt-extras/master/sbt', '-o', 'sbt'
}
} catch (Exception e){
logger.warn("Unable reach sbt-extras repository. " +
"Failure is imminent if this is the first time this build is executed on this machine. " +
"Reason: " + e.toString())
}
try {
exec {
workingDir '.'
commandLine 'sh', 'sbt', 'fastOptJS'
}
return true
} catch (Exception e){
logger.warn("Unable to execute sbt from sbt-extras. Reason: " + e.toString())
return false
}
}
tasks.register('buildui') {
doLast {
def success = buildUiWithInstalledSbt()
if(!success && !buildUiWithSbtExtras()){
throw new GradleException(
"Unable to build the ui javascript file with sbt. " +
"Possible solutions:\n" +
"1. Install SBT on your machine\n" +
"OR\n" +
"2. Prepare your systme to run 'sh' commands, " +
"e.g. install the git bash & add 'C:\\Program Files\\Git\\bin' to your PATH environment variable."
)
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Gradle 本身不包含 Bourne shell 解释器。您需要单独安装
sh
来运行sh
脚本,以及bash
来运行 Bash 脚本等。Gradle itself does not include a Bourne shell interpreter. You need to have
sh
separately installed to runsh
scripts, andbash
to run Bash scripts, etc.