Home > front end >  Execute shell script with gradle on windows machine (to call sbt from gradle)
Execute shell script with gradle on windows machine (to call sbt from gradle)

Time:03-03

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."
            )
        }
    }
}

CodePudding user response:

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.

  • Related