Create a directory structure from a path in gradle/groovy

旧巷老猫 提交于 2020-04-07 05:17:02

问题


I am implementing a diff package generation task in my project's gradle build from the git command line output. Currently I have a method which will give me a list of changed files from git diff --name-only. What I would like to do is create a directory structure in a new directory which matches the paths of each file. For example: inputting the string repo/dir/file.java would create in an output directory if not already created and inside it the directories head/repo/dir with the current file.java and prev/repo/dir with the previous file.java.

My current plan is to split the string repo/dir/file.java on the forward slash, and create directories until the last element of the split result, then write the file there. but nothing I have been able to come up with in gradle is nice or clean. I am wondering if there is a nicer way to create directories from a string like that.


回答1:


My current plan is to split the string repo/dir/file.java on the forward slash, and create directories until the last element of the split result

Rather than splitting your string manually, you could try using File.mkdirs():

File newDirectoryStructureParent = new File('some/path/to/parent/dir')

def s = 'repo/dir/file.java'
def newContainer = new File(s, newDirectoryStructureParent).getParent()
newContainer.mkdirs()


来源:https://stackoverflow.com/questions/43356577/create-a-directory-structure-from-a-path-in-gradle-groovy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!