DRY - how to write a war task to create different wars depending on a variable

Hi,

I like gradle very much, but I’m still having many problems when I need to customize something which is out of the box.

I have following code for creating wars at the moment: https://gist.github.com/b1572e80d2e6a40ee1e7

when I run war, it creates all the war files I need. But this functionality repeates in at least 2 more projects and I don’t want to duplicate this code all over.

How can I make an “abstract” task, or a task, which takes different $env?

Thanks for your help

Since Gradle’s build language is based on a general-purpose language, you can use similar strategies as for your production code: generate tasks in a loop, configure multiple tasks with a single configuration block, extract method, extract class, etc. For ‘Copy’ tasks like ‘War’ in particular, there is an additional option, namely to extract common copy logic into a so-called copy spec:

def spec = project.copySpec {
  from "foo"
  into "bar"
}
  war1 {
  from "baz"
  with spec
}
  war2 {
  from "other"
  with spec
}

I found now a solution:

['int', 'test', 'prod', 'local'].each { env ->
 task ("war${env.capitalize()}",type: War) {
  baseName = 'MYAPP'
  version += "-$env"
  include("**/log4j_${env}.conf", "**/web_${env}.xml", '**/templates/**')
  rename("log4j_${env}.conf", 'log4j.conf')
  rename("web_${env}.xml", 'web.xml')
 }
}
  task warAll() {
 description 'The new war task'
}
  warAll.dependsOn {
 tasks.findAll {
  task -> task.name.startsWith('war') && !['war', 'warAll'].contains(task.name)
 }
}