69

I have a gradle build setup at the beginning of which I want to execute a shellscript in a subdirectory that prepares my environment.

task build << {
}
task preBuild << {
    println 'do prebuild stuff:'
}
task myPrebuildTask(type: Exec) {
    workingDir "$projectDir/mySubDir"
    commandLine './myScript.sh'
}

build.dependsOn preBuild
preBuild.dependsOn myPrebuildTask

However, when I execute the task either by calling gradle myPrebuildTask or by simply building the project, the following error occurs:

> A problem occurred starting process 'command './myScript.sh''

Unfortunately, thats all I get.
I have also tried the following - same error.

commandLine 'sh mySubDir/myScript.sh'

I use Gradle 1.10 (needed by Android) on Windows, inside a Cygwin shell. Any ideas?

Jk1
  • 11,233
  • 9
  • 54
  • 64
Paul Latzelsperger
  • 711
  • 1
  • 6
  • 8

7 Answers7

46

use

commandLine 'sh', './myScript.sh'

your script itself is not a program itself, that's why you have to declare 'sh' as the program and the path to your script as an argument.

Rene Groeschke
  • 27,999
  • 10
  • 69
  • 78
45

A more generic way of writing the exec task, but portable for Windows/Linux, if you are invoking a command file on the PATH:

task myPrebuildTask(type: Exec) {
    workingDir "$projectDir/mySubDir"
    if (System.getProperty('os.name').toLowerCase(Locale.ROOT).contains('windows')) {
        commandLine 'cmd', '/c', 'mycommand'
    } else {
        commandLine 'sh', '-c', 'mycommand'
    }
}

This doesn't directly address the use case for the OP (since there is script file in the working directory), but the title of the question is more generic (and drew me here), so it could help someone maybe.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Patrice M.
  • 4,209
  • 2
  • 27
  • 36
18

unfortunately options with commandLine not worked for me in any way and my friend find other way with executable

executable "./myScript.sh"

and full task would be

task startScript() {
  doLast {
     exec {
          executable "./myScript.sh"
      }
  }
}
panser
  • 1,949
  • 22
  • 16
9

This works for me in my Android project

preBuild.doFirst {
    println("Executing myScript")
    def proc = "mySubDir/myScript.sh".execute()
    proc.waitForProcessOutput(System.out, System.err)
}

See here for explanation: How to make System command calls in Java/Groovy?

pigswig
  • 351
  • 3
  • 7
4

This is a solution for Kotlin DSL (build.gradle.kts) derived from Charlie Lee's answer:

task<Exec>("MyTask") {
    doLast {
        commandLine("git")
            .args("rev-parse", "--verify", "--short", "HEAD")
            .workingDir(rootProject.projectDir)
    }
}

Another approach using the Java standard ProcessBuilder API:

tasks.create("MyTask") {
    val command = "git rev-parse --verify --short HEAD"
    doLast {
        val process = ProcessBuilder()
            .command(command.split(" "))
            .directory(rootProject.projectDir)
            .redirectOutput(Redirect.INHERIT)
            .redirectError(Redirect.INHERIT)
            .start()
        process.waitFor(60, TimeUnit.SECONDS)
        val result = process.inputStream.bufferedReader().readText()
        println(result)
    }
}

For more information see:

Mahozad
  • 18,032
  • 13
  • 118
  • 133
2

for kotlin gradle you can use

 Runtime.getRuntime().exec("./my_script.sh")
Sattar
  • 2,453
  • 2
  • 33
  • 47
-10

I copied my shell scipt to /usr/local/bin with +x permission and used it as just another command:

commandLine 'my_script.sh' 
Ayush Goyal
  • 2,079
  • 8
  • 32
  • 47
  • 2
    This option works to, but i dont recomend put many files in /usr/local/bin – Edgard Leal Jan 28 '17 at 14:26
  • 3
    You sure you don't want to delete this answer? – Gray Jun 08 '18 at 17:24
  • 4
    If I delete, one wouldn't know how not to do it. Plus, it did work for me. – Ayush Goyal Jun 09 '18 at 09:29
  • 5
    While the suggested action "works" it doesn't allow for the project to be portable and will require manual steps from anyone who needs to run the gradle build for the project. – JoeyG Mar 28 '19 at 20:39
  • 1
    @JoeyG I guess this can stay here. It is downvoted enough to give anyone seeking answer a hint that this recipe should be taken with caution :) – Den Drobiazko Jul 06 '21 at 16:20