Ear 插件添加了对组装 Web 应用程序 EAR 文件的支持。它添加了一个默认的 EAR 存档任务。它不需要 Java 插件,但对于也使用 Java 插件的项目,它会禁用默认的 JAR 存档生成。
用法
要使用 Ear 插件,请在您的构建脚本中包含以下内容
plugins {
ear
}
plugins {
id 'ear'
}
项目布局
. └── src └── main └── application (1)
1 | Ear 资源,例如 META-INF 目录 |
依赖管理
Ear 插件添加了两个依赖配置:deploy
和 earlib
。deploy
配置中的所有依赖项都放置在 EAR 存档的根目录中,并且不是传递的。earlib
配置中的所有依赖项都放置在 EAR 存档中的 'lib' 目录中,并且是传递的。
约定属性 (ear)
appDirName
—String
-
应用程序源目录的名称,相对于项目目录。默认值:`src/main/application`。
libDirName
—String
-
生成的 EAR 中 lib 目录的名称。默认值:`lib`。
deploymentDescriptor
— DeploymentDescriptor-
用于生成部署描述符文件(例如
application.xml
)的元数据。默认值:一个具有合理默认值的部署描述符,名为application.xml`
。如果此文件已存在于 `appDirName/META-INF` 中,则将使用现有文件内容,并且将忽略
ear.deploymentDescriptor
中的显式配置。 generateDeploymentDescriptor
—Boolean
-
指定是否应生成 deploymentDescriptor。默认值:`true`。
这些属性由 EarPluginConvention 约定对象提供。
通过插件的约定属性配置 ear 任务已弃用。如果您需要更改默认值,请直接配置相应的任务。如果您想配置项目中的所有 Ear
任务,请使用 tasks.withType(Ear.class).configureEach(…).
Ear
Ear 任务的默认行为是将 src/main/application
的内容复制到存档的根目录。如果您的 application
目录不包含 META-INF/application.xml
部署描述符,则会为您生成一个。
API 文档中的 Ear 类包含更多有用的信息。
自定义
以下是一个包含最重要的自定义选项的示例
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" })
}
}
}
}
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
中的显式配置。