I am looking for a solution that would recursively remove a folder created with Files.createTempDirectory()
when my (Jenkins) Groovy script ends.
If you read the documentation createTempDirectory() does not remove folder and even if you try to use the delete-on-exit, it will fail if the folder has other files inside.
Please note that I am looking for a solution that would not have to add extra code at the end of the Groovy script or to add try/catch methods. That's because these Groovy codes are compiled from multiple re-usable parts.
A working solution should not need to add extra code at the end of the script, probably using a hooking mechanism to register the directory removal operation.
import java.nio.file.Files
x = Files.createTempDirectory()
// <-- add some magic hook to tell to remove 'x' folder recursively on exit
// a lot of code I cannot touch