如何在Gradle中重用任务配置?

时间:2016-06-08 21:37:39

标签: gradle task code-reuse

我有许多from/include/exclude的任务战配置:

task war {
    exclude
    exclude
    ... 
    into ... from ... 
    into ... from ... 
}

我有另一个任务战配置,除了一个exclude之外是相同的。 我不想复制这些配置。如何重用第一个配置?

2 个答案:

答案 0 :(得分:3)

尝试:

ext.sharedCopyConf = { task, to ->
  configure(task) {
    into to
    from 'a'
  }
}

task copy1(type: Copy) { t ->
  sharedCopyConf(t, 'b')
}

task copy2(type: Copy) { t ->
  sharedCopyConf(t, 'c')
}

查看demo

答案 1 :(得分:0)

ext.sharedWarConfig = { task->
    configure(task) {
        from ... include ...

    }}

task warWithoutFile(type: War) {  task ->
    sharedWarConfig(task)

    exclude ...
}

task warWithFile(type: War) {   task -> sharedWarConfig(task) }

jettyRunWar {
    dependsOn warWithFile
    dependsOn.remove("war")

    webApp = warWithFile.archivePath
}