Preserving symlinks in a copy

This is done with version 2.6 and Java 7.

Running a simple copy task where the source folder has symlinks will resolve them in the target folder.

So in the example below, all the symlinks created by the npm install in the staging folder will be files in the “lookat” folder.

I have seen discussions from 2013 about handling this, but definite resolution found. Maybe I could use the eachFile method?

BTW, this need is actually related to the nebula ospackage plugin, but my guess is that it’s behaviour is tied to gradle core behaviour, and if I can get a copy to work, then I’ll know how to handle this plugin.

installNpm { dependsOn stageFiles installDir stagingFolder }

task (‘myTest’, type: Copy) {
dependsOn installNpm
from stagingFolder
into “$buildDir/lookat”
}

I haven’t figured how to preserve the symlinks during the copy, but have some code that will at least skip the source symlinks and identify what the pieces need to create the symlink. See below

import java.nio.file.FileSystems import java.nio.file.Files import java.nio.file.Path

task (‘myTest’, type: Copy) {
dependsOn installNpm

from stagingFolder
into "$buildDir/lookat"
eachFile { details ->
Path pathFile = FileSystems.getDefault().getPath(details.file.path)
if(Files.isSymbolicLink(pathFile)) {
details.exclude()
String symLinkPath = "/${details.relativePath}"
String targetPath = Files.readSymbolicLink(pathFile)
println “$symLinkPath -> $targetPath”
}
}
}