Reputation: 24464
In a piece of some Gradle code I want to get a current folder name without splitting the path. But all attempts that I tried:
Paths.get(".").getFileName()
new File(".").getName()
new File(".").name
, return a dot "." instead of the name. Is there some function that gives the name, not another string by which the folder could be addressed?
What is interesting, if I use:
String currentDirPath = new File(".").absolutePath
println currentDirPath
currentDirPath = currentDirPath.substring(0,currentDirPath.lastIndexOf("\\"))
println currentDirPath
String currentDir = currentDirPath.substring(currentDirPath.lastIndexOf("\\")+1)
, it is seen, that the path string looks as:
C:\Users\543829657\workspace\dev.appl.ib.cbl\application\.
So, it is simply incorrect to take the last substring after '\'. But all those three functions take not the name of the name of the really actual folder, but the last "."!
Upvotes: 0
Views: 183
Reputation: 7211
We should not really retrieve the absolute path that way.
In Gradle, you can use project.projectDir
to get the project path or rootProject
if its a multiproject or if you want to get a path of the file project.file('yourfile')
Upvotes: 0
Reputation: 14493
Gradle is build upon Groovy, which is a JVM language, just like Java. So to get the current working directory, you can simply use the same ways you would use in Java. As an example, the following code will give you the name of the working directory, not the full path (check Frans answer).
new File('').absoluteFile.name
However, please mention that, in Gradle, you should not create or access files from the current working directory, but from the project (project.projectDir
) or build (project.buildDir
) directories, since you might accidentally build projects from lower directories, e.g. because Gradle checks for settings.gradle
scripts in parent directories.
Upvotes: 3
Reputation: 2050
Is not
new File("").absolutePath
what you are looking for?
Upvotes: 0