Ear 插件增加了对组装 Web 应用程序 EAR 文件的支持。它增加了一个默认的 EAR 归档任务。它不要求 Java 插件,但对于也使用 Java 插件的项目,它会禁用默认的 JAR 归档生成。
用法
要使用 Ear 插件,请在你的构建脚本中包含以下内容
示例 1. 使用 Ear 插件
build.gradle.kts
plugins {
    ear
}build.gradle
plugins {
    id 'ear'
}项目布局
.
└── src
    └── main
        └── application (1)
| 1 | Ear 资源,例如 META-INF 目录 | 
依赖管理
Ear 插件添加了两个依赖配置:deploy 和 earlib。deploy 配置中的所有依赖项都放置在 EAR 归档的根目录中,并且是非传递的。earlib 配置中的所有依赖项都放置在 EAR 归档的“lib”目录中,并且是传递的。
Ear
Ear 任务的默认行为是将 src/main/application 的内容复制到归档的根目录。如果你的 application 目录不包含 META-INF/application.xml 部署描述符,则会为你生成一个。
API 文档中的 Ear 类包含其他有用信息。
自定义
这是一个包含最重要自定义选项的示例
示例 2. Ear 插件的自定义
build.gradle.kts
plugins  {
    ear
    java
}
repositories { mavenCentral() }
dependencies {
    // The following dependencies will be the ear modules and
    // will be placed in the ear root
    deploy(project(path = ":war", configuration = "war"))
    // The following dependencies will become ear libs and will
    // be placed in a dir configured via the libDirName property
    earlib(group = "log4j", name = "log4j", version = "1.2.15", ext = "jar")
}
tasks.ear {
    appDirectory = file("src/main/app")  // use application metadata found in this folder
    libDirName = "APP-INF/lib" // put dependent libraries into APP-INF/lib inside the generated EAR
    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
        // defaults to project.description if not set
        description = "My customized EAR for the Gradle documentation"
//      libraryDirectory = "APP-INF/lib"  // not needed, above libDirName setting does this
//      module("my.jar", "java")  // won't deploy as my.jar isn't deploy dependency
//      webModule("my.war", "/")  // won't deploy as my.war isn't deploy dependency
        securityRole("admin")
        securityRole("superadmin")
        withXml { // add a custom node to the XML
            asElement().apply {
                appendChild(ownerDocument.createElement("data-source").apply { textContent = "my/data/source" })
            }
        }
    }
}build.gradle
plugins {
    id 'ear'
    id 'java'
}
repositories { mavenCentral() }
dependencies {
    // The following dependencies will be the ear modules and
    // will be placed in the ear root
    deploy project(path: ':war', configuration: 'war')
    // The following dependencies will become ear libs and will
    // be placed in a dir configured via the libDirName property
    earlib group: 'log4j', name: 'log4j', version: '1.2.15', ext: 'jar'
}
tasks.named('ear') {
    appDirectory = file('src/main/app')  // use application metadata found in this folder
    libDirName = 'APP-INF/lib' // put dependent libraries into APP-INF/lib inside the generated EAR
    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
        // defaults to project.description if not set
        description = "My customized EAR for the Gradle documentation"
//      libraryDirectory = "APP-INF/lib"  // not needed, above libDirName setting does this
//      module("my.jar", "java")  // won't deploy as my.jar isn't deploy dependency
//      webModule("my.war", "/")  // won't deploy as my.war isn't deploy dependency
        securityRole("admin")
        securityRole("superadmin")
        withXml { provider -> // add a custom node to the XML
            provider.asNode().appendNode("data-source", "my/data/source")
        }
    }
}你还可以使用 Ear 任务提供的自定义选项,例如 from 和 metaInf。
使用自定义描述符文件
你可能已经在 application.xml 文件中拥有适当的设置,并希望使用它而不是配置构建脚本的 ear.deploymentDescriptor 部分。为实现此目标,请将 META-INF/application.xml 放置在你的源文件夹中的正确位置(参见 appDirName 属性)。文件内容将被使用,并且 ear.deploymentDescriptor 中的显式配置将被忽略。