How to access final name of a file (specified in a rename closure) inside eachFile method


(Nishant Ghan) #1

Lets say I create a copy task A from a file as below

task A (type: Copy) {
     from 'abc.txt'
     into buildDir
     eachFile { fileCopyDetails ->
         println "Copying ${fileCopyDetails.getName()} from ${fileCopyDetails.getSourceName()}"}
}

And in a different file or the same file, I wish to do as below

 A.configure {
     rename 'xyz.txt'
 }

I am expecting the output to be :
> Copying xyz.txt from abc.txt
But it prints
> Copying abc.txt from abc.txt
According to https://docs.gradle.org/current/javadoc/org/gradle/api/file/FileCopyDetails.html , getName() returns the base name of this file at the copy destination. But that is not how it is working in my case.


(William Lichtenberger) #2

Hello Nishant,

This appears to be a bug. Both rename{} and eachFile{} add closures that are executed in order before the copy happens. So if you happen to call rename{} first, it will work as you would expect. But if you call eachFile{} first, then the fileCopyDetails may be incorrect. It should be noted that the actual copy action will be correct either way.

Could you please open an issue for this and reference this forum post?

Thanks,

Will


(Nishant Ghan) #3

Thanks Will. Submitted the issue: https://github.com/gradle/gradle/issues/2721