2017-05-05 69 views
0

非常简单的问题,但不知何故不起作用! 第一次使用Gradle 任务是编写一个gradle任务,用于迭代目录中的文件并将timeStamp添加到fileNames前缀。将时间戳添加到文件的Gradle脚本

我有一个脚本,但是当我用目标运行Gradle时,fileNames有时会列出,有时候不会。没有改变目录完成! 排除是排除先前重命名的文件 - 基本上是正则表达式来检查以数字开头的文件。

这里是一个脚本

task renameSqlFiles(type: Copy) { 

from "${rootDir}/migration/resources" 
into "${rootDir}/migration/resources" 

include '**/*.sql' 
exclude '^\\d+__.sql' 
rename { fileName -> println(fileName) 

} 

回答

1
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) 
     } 
    } 
} 
相关问题