我可以通过groovy更简单(更短)地实现此代码吗?

时间:2016-08-16 14:28:21

标签: gradle groovy

我可以通过groovy更简单(更短)地实现这段代码吗?

task uninstall(type: Delete) {
    def dirToDelete = System.properties['....'] + File.separator + ... + "..."

    def paths = project.group.toString().split("\\.")

    for (def p : paths)
        dirToDelete = dirToDelete + File.separator + p

    dirToDelete = dirToDelete + ...
    somethingDid(dirToDelete)
}

1 个答案:

答案 0 :(得分:0)

您可以通过将所有路径元素收集到列表中并将其连接到分隔符来删除重复的File.separator

task uninstall(type: Delete) {
    def paths = [System.properties['....'], "..."]

    paths << project.group.split(/\./)

    paths << ...
    somethingDid paths.flatten().join(File.separator)
}

备注

    拆分toString()时不需要
  • project.group,因为它已经是字符串
  • split("\\.")可以用斜线字符串替换,因此反斜杠不需要转义:split(/\./)