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

发布于 2025-01-11 02:16:35 字数 2176 浏览 0 评论 0原文

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可可 2025-01-18 02:16:35

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

Gradle itself does not include a Bourne shell interpreter. You need to have sh separately installed to run sh scripts, and bash to run Bash scripts, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文