Gradle 2.0 用户指南翻译——第二十六章. War 插件

翻译项目请关注Github上的地址:
https://github.com/msdx/gradledoc
本文翻译所在分支:
https://github.com/msdx/gradledoc/tree/2.0 。
在线浏览地址:
http://gradledoc.qiniudn.com/2.0/userguide/userguide.html 。
另外,Android 手机用户可通过我写的一个程序浏览文档,带缓存功能的,目前0.6开发中版本兼容 Android 2.3以上系统,项目地址如下:
https://github.com/msdx/gradle-doc-apk
翻译不易,转载请注明本文在CSDN博客上的出处:

https://blog.csdn.net/maosidiaoxian/article/details/81281376

关于我对Gradle的翻译,以Github上的项目及http://gradledoc.qiniudn.com 上的文档为准。如发现翻译有误的地方,将首先在以上两个地方更新。因时间精力问题,博客中发表的译文基本不会同步修改。

第二十六章. 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
warcompileWar组装应用程序 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
assemblewar

图 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/webappWeb 应用程序源码
Web application sources

26.4. 依赖管理

26.4. Dependency management

War 插件添加了两个依赖配置:providedCompile 和 providedRuntime。这些配置与 compile 和 runtime 配置一样有各自的作用域,只是它们不会添加到 WAR 归档中。要特别注意那些 provided 配置是有传递性的。假设你添加了 commons-httpclient:commons-httpclient:3.0 到任何一个 provided 的配置,而这个依赖又依赖于 commons-codec,这意味着 httpclient 和 commons-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
webAppDirNameStringsrc/main/webappweb 应用程序源目录的名称,相当于项目目录。 
The name of the web application source directory, relative to the project directory.
webAppDirFile (只读) 
File (read-only)
projectDir/webAppDirNameWeb 应用程序的源目录。 
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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值