代码之家  ›  专栏  ›  技术社区  ›  Siva

Gradle-如何在doLast中执行命令行并获取退出代码?

  •  3
  • Siva  · 技术社区  · 7 年前
    task executeScript() {
    
        doFirst {
            exec {
                ignoreExitValue true
                commandLine "sh", "process.sh"
            }
        }
    
        doLast {
            if (execResult.exitValue == 0) {
                print "Success"
            } else {
                print "Fail"
            }
        }
    }
    

    我发现以下错误

    > Could not get unknown property 'execResult' for task ':core:executeScript' of type org.gradle.api.DefaultTask.
    

    如果我移动 commandLine 配置部分一切正常。但我想要 命令行 处于动作块中,这样它就不会每次执行其他gradle任务时都运行。

    2 回复  |  直到 7 年前
        1
  •  2
  •   Daniel Taub    7 年前

    使用渐变 type 对于您的任务

    task executeScript(type : Exec) {
        commandLine 'sh', 'process.sh'
        ignoreExitValue true
    
        doLast {
            if(execResult.exitValue == 0) {
                println "Success"
            } else {
                println "Fail"
            }
        }
    }
    

    这对你有用。。。

    您可以阅读更多关于 Exec task here

        2
  •  0
  •   Dimitar II    3 年前

    执行外部命令并获取其返回代码的替代语法:

    doLast {
        def process = "my command line".execute()
        process.waitFor()
        println "Exit code: " + process.exitValue()
    }
    
    推荐文章