isobretatel
isobretatel

Reputation: 3930

How do I reuse task configuration in Gradle?

I have task war configuration with many from/include/exclude:

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

I have another task war configuration which is the same except one exclude. I don't want to duplicate those configurations. How can I reuse the first configuration?

Upvotes: 2

Views: 809

Answers (2)

isobretatel
isobretatel

Reputation: 3930

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
}

Upvotes: 2

Opal
Opal

Reputation: 84786

Try:

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')
}

Have a look at the demo.

Upvotes: 5

Related Questions