Gradle script to append timeStamp to files
build.gradle
Solution
def getDate() {
new Date().format('yyyyMMddHHmmssSSS')
}
task renameForMigration() {
println(" :migrate : renameForMigration")
def files = fileTree(dir: "${rootDir}/migration/resources/sql", includes: ['V*.sql'])
doLast {
files.each { file ->
def newFileName = "${getDate()}__${file.getName()}"
ant.move(file: file, toFile:"${file.parent}/${newFileName}")
sleep(1000)
}
}
}
Problem
Very simple question but somehow does not work ! First time working with Gradle The task is to write a gradle task that iterates files in a directory and prefixes timeStamps to the fileNames. I have a script but when I run Gradle with the target, fileNames are sometimes listed and sometimes not. No changes to directories done! The exclude is to exclude the files that were renamed earlier - basically regex to check files starting with digits. Here is a script ``` task renameSqlFiles(type: Copy) { from "${rootDir}/migration/resources" into "${rootDir}/migration/resources" include '**/*.sql' exclude '^\\d+__.sql' rename { fileName -> println(fileName) } ```