Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
693 views
in Technique[技术] by (71.8m points)

groovy - 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/"
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...