从gradle/groovy中的路径创建目录结构 [英] Create a directory structure from a path in gradle/groovy
问题描述
我正在从git命令行输出在我的项目的gradle构建中实现diff软件包生成任务.目前,我有一种方法可以为我提供git diff --name-only
中已更改文件的列表.我想做的是在一个与每个文件的路径匹配的新目录中创建一个目录结构.例如:输入字符串repo/dir/file.java
将在输出目录中创建(如果尚未创建),并且在其中包含当前file.java
的目录head/repo/dir
和先前file.java
的目录prev/repo/dir
.
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
.
我目前的计划是在正斜杠上分割字符串repo/dir/file.java
,并创建目录,直到分割结果的最后一个元素,然后将文件写入此处.但是我无法在gradle中提出的任何东西都是好还是干净的.我想知道是否有更好的方法从这样的字符串创建目录.
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.
推荐答案
我当前的计划是在正斜杠上分割字符串
repo/dir/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
您可以尝试使用 File.mkdirs()
:
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()
这篇关于从gradle/groovy中的路径创建目录结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!