There is some weird behaviour that I was wondering about. I have this Gradle task:
task testPaths {
println System.getProperty('user.dir')
println new File('x').getCanonicalPath()
println file('x').getCanonicalPath()
println new File('x').toPath().toAbsolutePath()
}
If I play with it this is what I get:
PS C:\dev\gradle-test> gradle --no-daemon testPaths
C:\dev\gradle-test
C:\dev\gradle-test\x
C:\dev\gradle-test\x
C:\dev\gradle-test\x
:testPaths UP-TO-DATE
BUILD SUCCESSFUL
Total time: 1.407 secs
PS C:\dev\gradle-test> gradle --daemon testPaths
C:\dev\gradle-test
C:\dev\gradle-test\x
C:\dev\gradle-test\x
C:\Users\Joan\.gradle\daemon\3.0\x
:testPaths UP-TO-DATE
BUILD SUCCESSFUL
Total time: 0.708 secs
When running in daemon mode Path
behaves differently from everything else. I’ve seen people saying that file(...)
should always be used instead of new File(...)
for relative paths. However in that case it is a Path
object that behaves weirdly. Furthermore - it appears that the working directory is not used as the base directory for the relative path.
I need to construct a Path
object since I need it to run some 3rd party code in a Gradle task. I was able to come up with some hackish ways to do so. But can someone explain this behaviour to me?
This is what I am running on:
Groovy: 2.4.7
Ant: Apache Ant™ version 1.9.6 compiled on June 29 2015
JVM: 1.8.0_66 (Oracle Corporation 25.66-b18)
OS: Windows 10 10.0 amd64