Tasks with different configurations

Hi,

I want to use custom configurations for different tasks.

I have 4 subprojects: - usecase1 - usecase1db - usecase1stub - webapp

I want to use tomcatRun in my webapp task with different configurations: one should be the dev configuration, which is including usecase1stub implementation one should be the prod configuration, which is including usecase1db implementation

apply plugin: ‘tomcat’

configuration {

dev

prod

}

dependencies {

// … other dependencies

compile project(’:usecase1’)

dev project(’:usecase1stub’)

prod project(’:usecase1db’)

}

task devTomcatRun(type: TomcatRun) {

// ??? configuration = dev ???

}

tomcatRun >> {

// configuration = prod ?

}

how to write the tasks for it?

One way to do this is by setting an envrionment parameter to gradle command line, i.e: “gradle -Ptarget=prod …”, and then inserting an if statement into the task:

if (target== 'prod') {
...
}

I understand that, carlo.if .

But how do I get those dependencies (dev/prod) compiled and used by tomcatRun on running devTomcatRun / tomcatRun

How can I get those dependencies (e.g. dev) in a seperate task? I want to switch between dev and prod dependencies when running tomcat run… how to write the script below?

apply plugin: ‘java’

apply plugin: ‘war’

apply plugin: ‘tomcat’

configuration {

dev

prod

}

dependencies {

// … other dependencies

compile project(’:usecase1’)

dev project(’:usecase1stub’)

prod project(’:usecase1db’)

}

task devTomcatRun(type: TomcatRun) {

// ??? configuration = dev ???

}

tomcatRun >> {

// configuration = prod ?

}

I tried something else:

5 project

  1. usecase1 2. usecase1db 3. usecase1stub 4. usecase1service 5. webapp

in usecase1service\build.gradle:

apply plugin: ‘java’

configurations {

dev.extendsFrom compile

prod.extendsFrom compile

}

dependencies {

compile project(’:usecase1’)

dev project(":usecase1stub")

prod project(":usecase1db")

}

// …

in webapp\build.gradle:

apply plugin: ‘java’

apply plugin: ‘war’

apply plugin: ‘tomcat’

configurations {

prod.extendsFrom tomcat

dev.extendsFrom tomcat

}

dependencies {

// … other dependencies

compile project(’:usecase1’)

dev project(path: ‘:usecase1service’, configuration: ‘dev’)

prod project(path: ‘:usecase1service’, configuration: ‘prod’)

}

I was hoping to get the devTomcatRun and prodTomcatRun task, but I didn’t get them.

I see a problem in this approach: if I build a war file, I will miss the usecase1db dependency…

You can create your own TomcatRun tasks etc.

Simplest way would be to look into the source to the tomcat plugin and copy what it is doing.