在Gradle上参数化任务的最佳方法是什么?

时间:2016-10-14 14:10:06

标签: gradle parameters task parameter-passing

如果它们不存在,我需要从两个不同的.properties.dist创建两个不同的.properties文件,所以我正在使用一个Copy任务,我正在相应地指定from和the。

目前我必须创建两个不同的任务,每个任务都创建一个这样的文件:

task copyAndRenameDialling(type: Copy){
    if(!file("./properties/dialling.properties").exists()){

        from './dist/dialling.properties.dist'
        into './properties/'
        rename{ String fileName ->
            fileName.replace('.dist','')
        }
    }
}

task copyAndRenameFiles(type: Copy){
    if(!file("./properties/file.properties").exists()){

        from './dist/files.properties.dist'
        into './properties/'
        rename{ String fileName ->
            fileName.replace('.dist','')
        }
    }
}


task copyAndRenameProperties {
    dependsOn << copyAndRenameDialling
    dependsOn << copyAndRenameFiles
}

我用gradle copyAndRenameProperties运行任务。 是否可以根据文件名参数化两个复制任务,以便我只有一个通用的copyAndRename? 如果是这样,我如何将参数传递给任务?

1 个答案:

答案 0 :(得分:1)

我这样做:

task copyAndRenameProperties(type: Copy) {
    from 'dist'
    include '*.properties.dist'
    into 'properties'
    rename { it - ~/\.dist$/ }
    eachFile { if (file("properties/$it.name").file) it.exclude() }
}

或者如果你真的只想要那两个特定的文件

task copyAndRenameProperties(type: Copy) {
    from 'dist/dialling.properties.dist'
    from 'dist/file.properties.dist'
    into 'properties'
    rename { it - ~/\.dist$/ }
    eachFile { if (file("properties/$it.name").file) it.exclude() }
}

task copyAndRenameProperties(type: Copy) {
    from 'dist'
    include 'dialling.properties.dist', 'file.properties.dist'
    into 'properties'
    rename { it - ~/\.dist$/ }
    eachFile { if (file("properties/$it.name").file) it.exclude() }
}