Ear 外掛程式新增了組裝 Web 應用程式 EAR 檔案的支援。它新增了一個預設的 EAR 歸檔任務。它不需要 Java 外掛程式,但對於也使用 Java 外掛程式的專案,它會停用預設的 JAR 歸檔產生。

用法

若要使用 Ear 外掛程式,請在您的建置腳本中包含以下內容

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

任務

Ear 外掛程式將以下任務新增至專案。

earEar

取決於compile(僅當也套用了 Java 外掛程式時)

組裝應用程式 EAR 檔案。

新增至其他任務的相依性

Ear 外掛程式將以下相依性新增至由 Base Plugin 新增的任務。

assemble

取決於ear

專案版面配置

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

相依性管理

Ear 外掛程式新增了兩個相依性配置:deployearlibdeploy 配置中的所有相依性都放置在 EAR 歸檔的根目錄中,並且是遞移性的。earlib 配置中的所有相依性都放置在 EAR 歸檔的 'lib' 目錄中,並且是遞移性的

慣例屬性 (ear)

appDirNameString

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

libDirNameString

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

deploymentDescriptorDeploymentDescriptor

產生部署描述符檔案 (例如 application.xml) 的中繼資料。預設值:名為 application.xml` 且具有合理預設值的部署描述符。如果此檔案已存在於 `appDirName/META-INF 中,則將使用現有的檔案內容,並且會忽略 ear.deploymentDescriptor 中的明確配置。

generateDeploymentDescriptorBoolean

指定是否應產生 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 中的明確配置。