我可以通过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)
}
答案 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(/\./)