Execute shell script in Gradle

前端 未结 6 426
北荒
北荒 2020-12-07 17:20

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 << {
         


        
相关标签:
6条回答
  • 2020-12-07 18:00

    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"
          }
      }
    }
    
    0 讨论(0)
  • 2020-12-07 18:02

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

    commandLine 'my_script.sh' 
    
    0 讨论(0)
  • 2020-12-07 18:09

    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.

    0 讨论(0)
  • 2020-12-07 18:15

    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?

    0 讨论(0)
  • 2020-12-07 18:18

    for kotlin gradle you can use

     Runtime.getRuntime().exec("./my_script.sh")
    
    0 讨论(0)
  • 2020-12-07 18:19

    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.

    0 讨论(0)
提交回复
热议问题