第二十七章. Ear 插件

Chapter 27. The Ear Plugin

Ear 插件添加了用于组装 web 应用程序的 EAR 文件的支持。它添加了一个默认的EAR archive task。它不需要 Java 插件,但是对于使用了 Java 插件的项目,它将禁用默认的 JAR archive 的生成。
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. Tasks

27.2. Tasks

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

表 27.1. Ear 插件 - tasks
Table 27.1. Ear plugin - tasks

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

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

表 27.2. Ear 插件 - 额外的task 依赖
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 task 的默认行为是将 src/main/application的内容复制到archive 的根目录下。如果你的 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:

示例 26.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.