第二十六章. War 插件

Chapter 26. The War Plugin

War 插件扩展了 Java插件,以添加对组装 Web 应用程序 WAR 文件的支持。它禁用 Java 插件默认的 JAR 归档生成,并添加一个默认的 WAR 归档任务。
The War plugin extends the Java plugin to add support for assembling web application WAR files. It disables the default JAR archive generation of the Java plugin and adds a default WAR archive task.

26.1. 用法

26.1. Usage

要使用 War 的插件,请在构建脚本中包含以下内容:
To use the War plugin, include in your build script:

示例 26.1. 使用 War 插件 - Example 26.1. Using the War plugin

build.gradle

apply plugin: 'war'

26.2. 任务

26.2. Tasks

War 插件向项目中添加了以下任务。
The War plugin adds the following tasks to the project.

表 26.1. War 插件——任务 - Table 26.1. War plugin - tasks

任务名称
Task name
依赖于
Depends on
类型
Type
描述
Description
war compile War 组装应用程序 WAR 文件。
Assembles the application WAR file.

War 插件向 Java 插件所加入的任务添加了以下的依赖。
The War plugin adds the following dependencies to tasks added by the Java plugin.

表 26.2. War 插件——额外的任务依赖 - Table 26.2. War plugin - additional task dependencies

任务名称
Task name
依赖于
Depends on
assemble war

图 26.1. War 插件——任务 - Figure 26.1. War plugin - tasks

War 插件——任务

26.3. 项目布局

26.3. Project layout

表 26.3. War 插件——项目布局 - Table 26.3. War plugin - project layout

目录
Directory
意义
Meaning
src/main/webapp Web 应用程序源码
Web application sources

26.4. 依赖管理

26.4. Dependency management

War 插件添加了两个依赖配置:providedCompileprovidedRuntime。这些配置与 compileruntime 配置一样有各自的作用域,只是它们不会添加到 WAR 归档中。要特别注意那些 provided 配置是有传递性的。假设你添加了 commons-httpclient:commons-httpclient:3.0 到任何一个 provided 的配置,而这个依赖又依赖于 commons-codec,这意味着 httpclientcommons-codec 都不会被添加到你的 WAR 文件中,即使 commons-codec 在你的 compile 的依赖配置中是显式声明的。如果你不想要这种传递行为,只需把你的 provided 依赖声明成 commons-httpclient:commons-httpclient:3.0@jar这样。
The War plugin adds two dependency configurations: providedCompile and providedRuntime. Those configurations have the same scope as the respective compile and runtime configurations, except that they are not added to the WAR archive. It is important to note that those provided configurations work transitively. Let's say you add commons-httpclient:commons-httpclient:3.0 to any of the provided configurations. This dependency has a dependency on commons-codec. This means neither httpclient nor commons-codec is added to your WAR, even if commons-codec were an explicit dependency of your compile configuration. If you don't want this transitive behavior, simply declare your provided dependencies like commons-httpclient:commons-httpclient:3.0@jar.

26.5. 约定属性

26.5. Convention properties

表 26.4. War 插件——目录属性 - Table 26.4. War plugin - directory properties

属性名称
Property name
类型
Type
默认值
Default value
描述
Description
webAppDirName String src/main/webapp web 应用程序源目录的名称,相当于项目目录。
The name of the web application source directory, relative to the project directory.
webAppDir File (只读)
File (read-only)
projectDir/webAppDirName Web 应用程序的源目录。
The web application source directory.

这些属性由一个 WarPluginConvention 的约定对象提供。
These properties are provided by a WarPluginConvention convention object.

26.6. War

26.6. War

War 任务的默认行为把 src/main/webapp 的内容复制到档案的根目录下。你的 webapp 目录自然可能包含一个 WEB-INF 子目录,这个子目录还可能包含着一个 web.xml中 文件。已经编译的类被会被编译进 WEB-INF/classes。所有的 runtime[13] 配置的依赖会被复制到 WEB-INF/lib
The default behavior of the War task is to copy the content of src/main/webapp to the root of the archive. Your webapp directory may of course contain a WEB-INF sub-directory, which again may contain a web.xml file. Your compiled classes are compiled to WEB-INF/classes. All the dependencies of the runtime [13] configuration are copied to WEB-INF/lib.

另请参阅 War
Have also a look at War.

26.7. 自定义

26.7. Customizing

下面是最重要的自定义选项的一个示例:
Here is an example with the most important customization options:

示例 26.2. war 插件的自定义 - Example 26.2. Customization of war plugin

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 one can configure the different file-sets with a closure to define excludes and includes.



[13]runtime 配置继承自 compile 配置。
[13] The runtime configuration extends the compile configuration.