第二十七章. Ear 插件

Chapter 27. The Ear Plugin

Ear 插件添加了对组装 Web 应用程序 EAR 文件的支持。它添加了一个默认的 EAR 归档任务。它不需要 Java 插件,但是对于也使用了 Java 插件的项目,它将禁用默认的 JAR 归档生成。
The Ear plugin adds support for assembling web application EAR files. It adds a default EAR archive task. It doesn't require the Java plugin, but for projects that also use the Java plugin it disables the default JAR archive generation.

27.1. 用法

27.1. Usage

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

示例 27.1. 使用 Ear 插件 - Example 27.1. Using the Ear plugin

build.gradle

apply plugin: 'ear'

27.2. 任务

27.2. Tasks

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

表 27.1. Ear 插件——任务 - Table 27.1. Ear plugin - tasks

任务名称
Task name
依赖于
Depends on
类型
Type
描述
Description
ear compile (only if the Java plugin is also applied) Ear 组装应用程序 EAR 文件。
Assembles the application EAR file.

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

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

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

27.3. 项目布局

27.3. Project layout

表 27.3. Ear 插件——项目布局 - Table 27.3. Ear plugin - project layout

目录
Directory
意义
Meaning
src/main/application Ear 资源,如 META-INF 目录
Ear resources, such as a META-INF directory

27.4. 依赖管理

27.4. Dependency management

Ear 插件添加了两个依赖配置:deployearlib。所有在 deploy 配置中的依赖都放在 EAR 存档的根目录中,并且是不具有传递性的。所有在 earlib 配置中的依赖都放在 EAR 归档的 'lib' 目录中,并且是具有传递性的。
The Ear plugin adds two dependency configurations: deploy and earlib. All dependencies in the deploy configuration are placed in the root of the EAR archive, and are not transitive. All dependencies in the earlib configuration are placed in the 'lib' directory in the EAR archive and are transitive.

27.5. 约定属性

27.5. Convention properties

表27.4. Ear 插件​——目录属性 - Table 27.4. Ear plugin - directory properties

属性名称
Property name
类型
Type
默认值
Default value
描述
Description
appDirName String src/main/application 应用程序源目录的名称,相对于项目目录。
The name of the application source directory, relative to the project directory.
libDirName String lib 生成的 EAR 文件里的 lib 目录名称。
The name of the lib directory inside the generated EAR.
deploymentDescriptor org.gradle.plugins.ear.descriptor.DeploymentDescriptor 具有合理默认值,名为 application.xml中 的部署描述符
A deployment descriptor with sensible defaults named application.xml
用于生成部署描述符文件的元数据,如 application.xml中。如果这个文件已经存在于 appDirName/META-INF,那么将使用现有的文件内容,而在 ear.deploymentDescriptor 中的显式配置则将被忽略。
Metadata to generate a deployment descriptor file, e.g. application.xml. If this file already exists in the appDirName/META-INF then the existing file contents will be used and the explicit configuration in the ear.deploymentDescriptor will be ignored.

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

27.6. Ear

27.6. Ear

Ear 任务的默认行为是把 src/main/application 的内容复制到档案的根目录下。如果你的 application 目录没有包含 META-INF/application.xml 部署描述符,那么将会为你生成一个。
The default behavior of the Ear task is to copy the content of src/main/application to the root of the archive. If your application directory doesn't contain a META-INF/application.xml deployment descriptor then one will be generated for you.

另请参阅 Ear
Also have a look at Ear.

27.7. 自定义

27.7. Customizing

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

示例 27.2. ear 插件的自定义 - Example 27.2. Customization of ear plugin

build.gradle

apply plugin: 'ear'
apply plugin: 'java'

repositories { mavenCentral() }

dependencies {
    //following dependencies will become the ear modules and placed in the ear root
    deploy project(':war')

    //following dependencies will become ear libs and placed in a dir configured via libDirName property
    earlib group: 'log4j', name: 'log4j', version: '1.2.15', ext: 'jar'
}

ear {
    appDirName 'src/main/app'  // use application metadata found in this folder
    libDirName 'APP-INF/lib'  // put dependency libraries into APP-INF/lib inside the generated EAR;
                                // also modify the generated deployment descriptor accordingly
    deploymentDescriptor {  // custom entries for application.xml:
//      fileName = "application.xml"  // same as the default value
//      version = "6"  // same as the default value
        applicationName = "customear"
        initializeInOrder = true
        displayName = "Custom Ear"  // defaults to project.name
        description = "My customized EAR for the Gradle documentation"  // defaults to project.description
//      libraryDirectory = "APP-INF/lib"  // not needed, because setting libDirName above did this for us
//      module("my.jar", "java")  // wouldn't deploy since my.jar isn't a deploy dependency
//      webModule("my.war", "/")  // wouldn't deploy since my.war isn't a deploy dependency
        securityRole "admin"
        securityRole "superadmin"
        withXml { provider -> // add a custom node to the XML
            provider.asNode().appendNode("data-source", "my/data/source")
        }
    }
}

你也可以使用 Ear 任务提供的自定义选项,如 frommetaInf
You can also use customization options that the Ear task provides, such as from and metaInf.

27.8. 使用自定义的描述符文件

27.8. Using custom descriptor file

假设你已经有了 application.xml中 并且希望使用它而不是去配置 ear.deploymentDescriptor 代码段,那么就要把 META-INF/application.xml 放在你源文件夹中的正确位置(请参阅 appDirName 属性)。这样现有的文件内容将被使用,而 ear.deploymentDescriptor 中的显式配置则会被忽略。
Let's say you already have the application.xml and want to use it instead of configuring the ear.deploymentDescriptor section. To accommodate that place the META-INF/application.xml in the right place inside your source folders (see the appDirName property). The existing file contents will be used and the explicit configuration in the ear.deploymentDescriptor will be ignored.