Reputation: 408
In build.gradle I am building WAR for tomcat and wildfly by single script. Wildfly has provided dependency to "javax.mail:mail:1.4.7'. But tomcat is missing this jar. So I have always added this jar to ${CATALINA_HOME}/lib/ . Currently I am trying to migrate from both of them to Amazon AWS Elastic Beanstalk and I don't want to mess with ${CATALINA_HOME}/lib/. How to write universal gradle script for wildfly with:
dependencies {
....
providedCompile group: 'javax.mail', name: 'mail', version: '1.4.7'
providedCompile group: 'javax.activation', name: 'activation', version: '1.1.1'
...
}
and for tomcat with:
dependencies {
...
compile group: 'javax.mail', name: 'mail', version: '1.4.7'
compile group: 'javax.activation', name: 'activation', version: '1.1.1'
...
}
I am not an expert with gradle.
@RaGe solved my problem. Code below is an ultimate solution as "42" number.
configurations {
tomcatLibs
}
dependencies {
...
providedCompile group: 'javax.mail', name: 'mail', version: '1.4.7' //provided for wildfly
providedCompile group: 'javax.activation', name: 'activation', version: '1.1.1' //provided for wildfly
tomcatLibs group: 'javax.mail', name: 'mail', version: '1.4.7' //only for tomcat
tomcatLibs group: 'javax.activation', name: 'activation', version: '1.1.1' //only for tomcat
providedCompile group: 'javax.mail', name: 'javax.mail-api', version: '1.5.0'
....
}
//default war for wildfly
war {
....
}
task createTomcatWar(type: War, dependsOn: classes) {
archiveName = 'app-tomcat.war';
classpath configurations.tomcatLibs // adds a configuration to the WEB-INF/lib dir
}
....
Upvotes: 2
Views: 976
Reputation: 23755
Add an additional config to hold the dependency who's scope changes:
configurations {
optLibs
}
add the dependency to the config just created:
dependencies{
...
optLibs 'javax.mail:mail:1.4.7'
optLibs 'javax.activation:activation:1.1.1'
compile 'foo'
runtime 'bar'
...
providedCompile.extendsFrom(optLibs)
}
Now for a task that builds a war with optLibas as compile
:\
task createTomcatWar(type: War, dependsOn: classes) {
baseName = 'app-wildfly'
destinationDir = file("$buildDir/dist")
classpath = configurations.optLibs //This should include optLibs jars in WEB-INF/lib
}
The standard war task builds a war without the optLibs included, so that can be your wildfly war, you don't need another explicit task. If you want your custom task to run automatically everytime you build, you can also add to the rootlevel of your build.gradle:
assemble.dependsOn createTomcatWar
Upvotes: 1