3

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.

MA citizen
  • 33
  • 1
  • 1
  • 3
  • Groovy, which runs Gradle, is executed in the Java Virtual Machine (JVM). Therefor, you can use the whole JDK, including the [`File`](https://docs.oracle.com/javase/7/docs/api/java/io/File.html) class. You could take a look at [this question](http://stackoverflow.com/questions/3090761/how-to-create-a-new-file-together-with-missing-parent-directories), since the explained Java methods can be used. Groovy also provides [enhancements](http://docs.groovy-lang.org/latest/html/groovy-jdk/overview-summary.html) for some JDK classes, including the `File` class. – Lukas Körfer Apr 12 '17 at 08:26

3 Answers3

10

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()
Armand
  • 23,463
  • 20
  • 90
  • 119
  • 2
    For me, the following change was needed: `def newContainer = new File(new File(s, newDirectoryStructureParent).getParent())` – meolic Oct 25 '19 at 08:14
0

Similar to the accepted answer with a few less hoops.

File file = new File("full/path/to/file.java")
if(!file.getParentFile().exists()) { // Checks for "full/path/to".
    file.getParentFile().mkdirs(); // Creates any missing directories.
}
AnthonyW
  • 1,910
  • 5
  • 25
  • 46
-2

everyone In this part of my code you can just work around Path not File!

At the first you can define Path and second need check that path exist or not, if not mkdirs can make it ;)

Its help when you unknown about that path exist or not /

File fullPath = new File('/tmp/Test1')
if (!fullPath.exists())
    fullPath.mkdirs()
  • 2
    This is the same approach explain in the other answer, but it just solves half of the problem in question. It would also be good if you take some time and explain your answer instead of leaving an "everyone". – BDL Jul 20 '21 at 10:28
  • Hi BDL, I try to add more data ;) – Ali Shirini Jul 20 '21 at 20:23