在 windows 机器上用 gradle 执行 shell 脚本(从 gradle 调用 sbt)

Execute shell script with gradle on windows machine (to call sbt from gradle)

在 windows 机器上 gradle 运行ning 是否能够执行 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."
            )
        }
    }
}

Gradle 本身不包含 Bourne shell 解释器。您需要将 sh 单独安装到 运行 sh 脚本,以及 bash 到 运行 Bash 脚本等