【问题标题】:Groovy execute().text combinationGroovy execute().text 组合
【发布时间】:2020-02-01 18:05:18
【问题描述】:

我在工作中发现了这段代码

def getGitHash = { -> "git rev-parse --short HEAD".execute([], project.rootDir).text.trim() }

我不明白的是.text 调用,我还没有找到关于它是如何工作的清晰简洁的解释。能给我解释一下吗?

【问题讨论】:

    标签: gradle groovy closures


    【解决方案1】:

    看看groovy string中提供的额外方法

    execute 方法返回一个Process,它有一个getText() 方法

    可以改写成

    def getGitHash = { -> 
       Process process = "git rev-parse --short HEAD".execute([], project.rootDir)
       return process.getText().trim() 
    }
    

    【讨论】:

    • .text 是否等同于 getText()?
    • 是的,在 groovy 中你可以将 a.setName(b.getName()) 重写为 a.name = b.name
    猜你喜欢
    • 2023-04-01
    • 1970-01-01
    • 2011-01-23
    • 1970-01-01
    • 2017-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多