It doesn't help that the Java runtime (maybe assisted by Windows) claims that junction points are a directory.
Create a new freestyle project named JENKINS-17267, add a Windows Batch build step:
mklink /d s c:\existingTargetFolder
mklink /j j c:\existingTargetFolder
md d
Build once. Then open Script Console and run the following script:
def s = new File(Jenkins.instance.getItemByFullName('JENKINS-17267').someWorkspace.child('s').getRemote())
def j = new File(Jenkins.instance.getItemByFullName('JENKINS-17267').someWorkspace.child('j').getRemote())
def d = new File(Jenkins.instance.getItemByFullName('JENKINS-17267').someWorkspace.child('d').getRemote())
[s, j, d].each {
println java.nio.file.Files.isDirectory(it.toPath())
println java.nio.file.Files.isDirectory(it.toPath(), java.nio.file.LinkOption.NOFOLLOW_LINKS)
println java.nio.file.Files.isSymbolicLink(it.toPath())
println ""
}
return
Output:
true
false
true
true
true
false
true
true
false
In other words, a pure Java program has no chance to distinguish between a junction and an actual folder.