Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create multiple .WAR files with different dependencies in Gradle

I am using the war plugin to generate a simple .WAR file for my project in gradle. I'd like to know how to configure gradle so that I can create 4 different .WAR files with different dependencies.

I've configured the dependency compile configuration with the jars that are needed to go into the distribution. None of the code in the src depends on a couple of these jars but I would like to know how to configure the project to create

  • a standard.WAR file that contains all of the jars in the dependency graph (Even though they aren't used - that is OK - I am testing something)
  • another standard-qas-only.WAR file that only contains the qas.jar
  • another standard-qas-log4j.WAR file that contains qas.jar and log4j

What tasks do i configure to have the artifact generated use a particular dependency configuration?

FYI: The only jar that is required for compilation is qas.jar in this case.

My example below creates a war file that only includes one jar but i'd like to have 5 different .war files generated with different jars.

build.gradle

apply plugin: 'java'
apply plugin: 'war'

dependencies {
    compile files('/lib/qas.jar','/lib/axis1-1.4.jar','/lib/axis2-kernel-1.3.jar','/lib/dom4j-1.6.1.jar','/lib/log4j-1.2.14.jar')
    providedCompile files('/lib/j2ee-1.4.03.jar')
}

war {
    classpath = ['/lib/qas.jar']
}

task dist(dependsOn: 'war') << {
    copy {
        from war.archivePath
        into "dist/"
    }
}
like image 911
Shaun Avatar asked Oct 25 '12 21:10

Shaun


1 Answers

I got a bit confused on how many WAR distributions you are actually trying to build. You can easily modify it to create additional WAR files. Here's one approach to make this happen:

task createStandardWar(type: War, dependsOn: classes) {
    baseName = 'standard'
    destinationDir = file("$buildDir/dist")
}

task createStandardWarQasOnly(type: War, dependsOn: classes) {
    baseName = 'standard-qas-only'
    destinationDir = file("$buildDir/dist")
    classpath = war.classpath.minus(files('/lib/axis1-1.4.jar','/lib/axis2-kernel-1.3.jar','/lib/dom4j-1.6.1.jar','/lib/log4j-1.2.14.jar'))
}

task createStandardWarQasAndLog4J(type: War, dependsOn: classes) {
    baseName = 'standard-qas-log4j'
    destinationDir = file("$buildDir/dist")
    classpath = war.classpath.minus(files('/lib/axis1-1.4.jar','/lib/axis2-kernel-1.3.jar','/lib/dom4j-1.6.1.jar'))
}

task createDists(dependsOn: [createStandardWar, createStandardWarQasOnly, createStandardWarQasAndLog4J])

This build script excerpt creates three different WAR files by declaring enhanced tasks of type War. It assumes that you still want to have your compiled source files under WEB-INF/classes within the WAR files so I didn't remove it from the classpath. The distributions end up in the directory build/dist. The task createDists creates all of them.

like image 185
Benjamin Muschko Avatar answered Oct 27 '22 11:10

Benjamin Muschko