May 25, 2021 Gradle
War's plug-in inherits from the Java plug-in and adds support for assembling WAR files for web applications. It disables the Java plug-in to generate the default JAR archive and adds a default WAR archive task.
To use War's plug-in, include the following statement in the build script:
Use the War plug-in
build.gradle
apply plugin: 'war'
The War plug-in adds the following tasks to the project.
Table 26.1. War plug-in - task
The name of the task | Depends on | Type | Describe |
war
|
compile
|
War | Assemble the application WAR file. |
The War plug-in adds the following dependencies to the tasks that the Java plug-in joins.
Table 26.2. War plug-in - additional task dependency
The name of the task | Depends on |
assemble | war |
Figure 26.1. War plug-in - tasks
Table 26.3. War plug-in - project layout
Directory | Significance |
from
<s1>'src/main/webapp'</s1>
|
Web application source code |
The War plug-in adds two dependency configurations: providedCompile and providedRuntime. A lthough they have their own compile and runtime configurations, they have the same scope, but they are not added to the WAR file. I t is important to note that these provided configurations are used for delivery. S uppose you add commons-httpclient:commons-httpclient:3.0 dependent on any one of the provided configurations. T his dependency, in turn, depends on commons-codec. T his means that between httpclient and commons-codec are not added to your WAR, even if commons-codec is a display dependency on the compile configuration. If you don't want this delivery behavior, just declare the provided dependency as commons-httpclient:commons-httpclient:3.0@jar the same.
Table 26.4. War Plug-in - Directory Properties
The name of the property | Type | The default | Describe |
webAppDirName
|
String
|
from
<s1>'src/main/webapp'</s1>
|
The name of the web application source directory, which is a directory name relative to the project directory. |
webAppDir
|
File
(read-only)
|
webAppDirName
|
The source directory of the Web application. |
These properties are provided by a WarPluginConvention convention object.
The default behavior of War task is to copy the contents of the src/main/webapp to the root of the archive. Y our webapp directory may naturally contain a WEB-INF subdirecte, which may also contain a web .xml file. C ompiled classes are compiled into WEB-INF/classes. All runtime configuration dependencies are copied to WEB-INF/lib.
Here's an example of the most important customization options:
The customization of the war plug-in
build.gradle
configurations {
moreLibs
}
repositories {
flatDir { dirs "lib" }
mavenCentral()
}
dependencies {
compile module(":compile:1.0") {
dependency ":compile-transitive-1.0@jar"
dependency ":providedCompile-transitive:1.0@jar"
}
providedCompile "javax.servlet:servlet-api:2.5"
providedCompile module(":providedCompile:1.0") {
dependency ":providedCompile-transitive:1.0@jar"
}
runtime ":runtime:1.0"
providedRuntime ":providedRuntime:1.0@jar"
testCompile 'junit:junit:4.11'
}
moreLibs ":otherLib:1.0"
}
war {
from 'src/rootContent' // adds a file-set to the root of the archive
webInf { from 'src/additionalWebInf' } // adds a file-set to the WEB-INF dir.
classpath fileTree('additionalLibs') // adds a file-set to the WEB-INF/lib dir.
classpath configurations.moreLibs // adds a configuration to the WEB-INF/lib dir.
webXml = file('src/someWeb.xml') // copies a file to WEB-INF/web.xml
}
Of course, you can configure different sets of files with a closure that defines excludes and includes.