Why doesn't my file get renamed while copying?

Hello all,

Currently I have the following task:

task testBackup(type: Copy) {
    from '.../testfile.xml'
    into '.../Backups/'
    rename { String fileName ->
        fileName -= ".xml"
        fileName += "-" + new Date().format('dd-MM-yyyy-hh:mm:ss') + ".xml"
        println "fileName: " + fileName
    }
}

The problem is, the println displays the correct name in the terminal, but when I go check out the file, it’s still the same name as the original. Am I forgetting something here? I’m sure it’s small and I’m just not seeing it.

Thanks!

~ Joe

Strings are immutable; operations like ‘+=’ return new Strings. Hence you have to return the new filename from the closure, either by adding an explicit ‘return fileName’, or by removing the ‘println’ (in which case the previous line becomes an implicit return).

I didn’t know these kind of operations returned new Strings. Thanks for that! For the interested, this is the task I ended up with (24hr notation):

task testBackup(type: Copy) {
    from '.../testfile.xml'
    into '.../Backups/'
    rename { String fileName ->
        fileName -= ".xml"
        fileName += "-" + new Date().format('dd-MM-yyyy-HH:mm:ss') + ".xml"
        println "fileName: " + fileName
        return fileName
    }
}