War 插件擴展了 Java 插件,以增加對組裝 Web 應用程式 WAR 檔案的支援。它停用了 Java 插件的預設 JAR 歸檔產生,並新增了預設的 WAR 歸檔任務。
用法
要使用 War 插件,請在您的建置腳本中包含以下內容
plugins {
war
}
plugins {
id 'war'
}
專案佈局
除了 標準 Java 專案佈局 之外,War 插件還新增了
src/main/webapp
-
Web 應用程式來源
任務
War 插件新增和修改了以下任務
war
— War-
相依於:
compile
組裝應用程式 WAR 檔案。
assemble
- 生命週期任務-
相依於:
war
War 插件將以下相依性新增到 Java 插件新增的任務;

相依性管理
War 插件新增了兩個相依性配置
providedCompile
-
此配置應用於編譯時需要的相依性,但這些相依性由部署 WAR 的環境提供。因此,在此處宣告的相依性對於
main
和test
編譯類別路徑是可見的。 providedRuntime
-
此配置應用於執行時需要的相依性,但這些相依性由部署 WAR 的環境提供。在此處宣告的相依性僅對於
main
和test
執行時類別路徑是可見的。
重要的是要注意,這些 假設您將 如果您不想要此傳遞行為,只需像 |
發布
components.web
-
用於發布由
war
任務建立的生產 WAR 的 SoftwareComponent。
慣例屬性 (已棄用)
webAppDirName
—String
-
預設值:
src/main/webapp
Web 應用程式來源目錄的名稱,相對於專案目錄。
webAppDir
— (唯讀)File
-
預設值:
$webAppDirName
,例如 src/main/webappWeb 應用程式來源目錄的路徑。
這些屬性由 WarPluginConvention 物件提供。
透過慣例屬性配置 war 任務已棄用。如果您需要設定 war
任務的預設值,請直接配置任務。如果您想要配置專案中所有 War
類型的任務,請使用 tasks.withType(War.class).configureEach(…)。
自訂
以下範例包含最重要的自訂選項
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
}
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
}
當然,可以使用閉包配置不同的檔案集,以定義排除和包含。