使用gradle复制放置在drectory中的最新文件

时间:2018-12-14 09:51:57

标签: gradle

所以我在目录中有很多档案,如下所示:

test_61995.zip test_61234.zip test_61233.zip

我只想使用Gradle从这里复制最新文件。 possilbe是对文件和日期和时间进行排序并复制usng gradle吗?

1 个答案:

答案 0 :(得分:3)

当然可以。这是一个例子

科特琳DSL:

tasks {
    val cp by creating(Copy::class.java) {
        from(File("/home/madhead/Downloads/").listFiles().sortedBy { it.lastModified() }.last())
        into(File("/home/madhead/Downloads/so53777253/"))
    }
}

Groovy DSL:

task cp(type: Copy) {
    from(new File("/home/madhead/Downloads/").listFiles().sort{ it.lastModified() }[0])
    into(new File("/home/madhead/Downloads/so53777253/"))
}

这会将最新的修改文件从/home/madhead/Downloads/复制到/home/madhead/Downloads/so53777253/