Ear 外掛新增支援,可組建 Web 應用程式 EAR 檔案。它新增預設 EAR 封存任務。它不需要 Java 外掛,但對於同時使用 Java 外掛的專案,它會停用預設 JAR 封存產生。

使用方式

若要使用 Ear 外掛,請在建置指令碼中包含下列內容

build.gradle.kts
plugins {
    ear
}
build.gradle
plugins {
    id 'ear'
}

任務

Ear 外掛會將下列任務新增到專案。

earEar

相依於compile(僅在同時套用 Java 外掛時)

組建應用程式 EAR 檔案。

新增到其他任務的相依性

Ear 外掛會將下列相依性新增到 基本外掛新增的任務。

assemble

相依於ear

專案配置

.
└── src
    └── main
        └── application (1)
1 Ear 資源,例如 META-INF 目錄

相依性管理

Ear 外掛程式新增兩個相依性設定:deployearlibdeploy 設定中的所有相依性都會放置在 EAR 封存的根目錄中,且不是遞移的。earlib 設定中的所有相依性都會放置在 EAR 封存中的「lib」目錄中,且遞移的。

慣例屬性 (ear)

appDirName字串

應用程式來源目錄的名稱,相對於專案目錄。預設值:`src/main/application`

libDirName字串

已產生 EAR 內部 lib 目錄的名稱。預設值:`lib`

deploymentDescriptorDeploymentDescriptor

用於產生配置描述檔檔案的元資料,例如 application.xml預設值:具有明智預設值的配置描述檔,稱為 application.xml`。如果此檔案已存在於 `appDirName/META-INF` 中,則會使用現有的檔案內容,並忽略 `ear.deploymentDescriptor` 中的明確設定。

generateDeploymentDescriptor布林值

指定是否應產生 deploymentDescriptor。預設值:`true`

這些屬性是由 EarPluginConvention 慣例物件提供的。

透過外掛程式的慣例屬性設定 ear 工作的行為已過時。如果您需要變更預設值,請直接設定適當的工作。如果您想設定專案中的所有 Ear 工作,請使用 tasks.withType(Ear.class).configureEach(…​)

Ear

Ear 工作的預設行為是將 src/main/application 的內容複製到封存的根目錄。如果您的 application 目錄不包含 META-INF/application.xml 配置描述檔,則會為您產生一個。

API 文件中的 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 工作提供的自訂選項,例如 frommetaInf

使用自訂描述符檔案

您可能已經在 application.xml 檔案中有適當的設定,並希望使用它來取代組建指令碼的 ear.deploymentDescriptor 區段。為達成此目的,請將 META-INF/application.xml 放置在來源資料夾內的正確位置(請參閱 appDirName 屬性)。系統會使用檔案內容,並會忽略 ear.deploymentDescriptor 中的明確組態。