War 外掛擴充 Java 外掛,以新增組建 Web 應用程式 WAR 檔案的支援。它會停用 Java 外掛的預設 JAR 檔案產生,並新增預設 WAR 檔案工作。

用法

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

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

專案配置

除了 標準 Java 專案配置 之外,War 外掛會新增

src/main/webapp

Web 應用程式來源

工作

War 外掛會新增和修改下列工作

warWar

相依於compile

組裝應用程式 WAR 檔案。

assemble - 生命週期任務

依賴於: war

War 外掛會將下列依賴關係新增至 Java 外掛新增的任務中;

warPluginTasks
圖 1. War 外掛 - 任務

依賴關係管理

War 外掛會新增兩個依賴關係設定

providedCompile

此設定應使用於編譯時所需的依賴關係,但由 WAR 部署的環境提供。在此宣告的依賴關係因此會顯示在 maintest 編譯類別路徑中。

providedRuntime

此設定應使用於執行時所需的依賴關係,但由 WAR 部署的環境提供。在此宣告的依賴關係僅會顯示在 maintest 執行時類別路徑中。

請務必注意,這些 provided 設定會遞迴運作。

假設您將 commons-httpclient:commons-httpclient:3.0 新增至任何 provided 設定。此依賴關係會依賴於 commons-codec。由於這是「provided」設定,表示即使 commons-codec 函式庫是 implementation 設定的明確依賴關係,這些依賴關係也不會新增至您的 WAR 中。

如果您不想要這種遞迴行為,只要將您的 provided 依賴關係宣告為 commons-httpclient:commons-httpclient:3.0@jar 即可。

發布

components.web

SoftwareComponent 用於 發布 war 任務建立的生產 WAR。

慣例屬性 (已棄用)

webAppDirName — String

預設值: src/main/webapp

Web 應用程式來源目錄的名稱,相對於專案目錄。

webAppDir — (唯讀) File

預設值: $webAppDirName,例如 src/main/webapp

Web 應用程式來源目錄的路徑。

這些屬性由 WarPluginConvention 物件提供。

透過慣例屬性設定 war 任務已棄用。如果您需要設定 war 任務的預設值,請直接設定任務。如果您想要設定專案中所有 War 類型的任務,請使用 tasks.withType(War.class).configureEach(…​)

War

War 任務的預設行為是將 src/main/webapp 的內容複製到檔案的根目錄。當然,您的 webapp 目錄可能包含 WEB-INF 子目錄,其中可能包含 web.xml 檔案。您的已編譯類別會編譯至 WEB-INF/classesruntime [1] 設定的所有依賴關係都會複製至 WEB-INF/lib

API 文件中的 War 類別有其他有用的資訊。

自訂

以下是包含最重要的自訂選項的範例

build.gradle.kts
repositories {
    mavenCentral()
}

dependencies {
    providedCompile("javax.servlet:servlet-api:2.5")
}

tasks.war {
    webAppDirectory = file("src/main/webapp")
    from("src/rootContent") // adds a file-set to the root of the archive
    webInf { from("src/additionalWebInf") } // adds a file-set to the WEB-INF dir.
    webXml = file("src/someWeb.xml") // copies a file to WEB-INF/web.xml
}
build.gradle
repositories {
    mavenCentral()
}

dependencies {
    providedCompile "javax.servlet:servlet-api:2.5"
}

war {
    webAppDirectory = file('src/main/webapp')
    from 'src/rootContent' // adds a file-set to the root of the archive
    webInf { from 'src/additionalWebInf' } // adds a file-set to the WEB-INF dir.
    webXml = file('src/someWeb.xml') // copies a file to WEB-INF/web.xml
}

當然,可以設定不同的檔案集,並使用封閉函式來定義排除和包含。


1. runtime 設定會延伸 compile 設定。