*本章未完,還在進行中*
本章介紹了 Gradle 對 Web 應用的相關支持。 Gradle 為 Web 開發提供了兩個主要插件,War 插件 和 Jetty 插件。 其中 War 插件繼承自 Java 插件,可以用來生成 WAR 文件。Jetty 插件 繼承自 War 插件 作為工程部署的容器。
## [](https://github.com/waylau/Gradle-2-User-Guide/blob/master/Chapter%2010.%20Web%20Application%20Quickstart%20%E5%BF%AB%E9%80%9F%E5%BC%80%E5%A7%8B%20Web%20%E5%BA%94%E7%94%A8.md#101-building-a-war-file-構建-war-文件)10.1\. Building a WAR file 構建 WAR 文件
應用 War 插件 來構建 WAR 文件:
Example 10.1\. War plugin
build.gradle
~~~
apply plugin: 'war'
~~~
*注意,完整的項目源碼見[https://github.com/waylau/Gradle-2-User-Guide-Demos](https://github.com/waylau/Gradle-2-User-Guide-Demos)?中 webApplication/quickstart*
同時應用 Java 插件,當你執行?`gradle build`?時,將會編譯、測試、打包工程成為一個 WAR 文件。 Gradle 會在 WAR 中 src/main/webapp 下尋找 源文件。編譯后的classes文件以及運行時依賴也都會被包含在 WAR 包中,分別在 WEB-INF/classes 和 WEB-INF/lib 目錄下。
## [](https://github.com/waylau/Gradle-2-User-Guide/blob/master/Chapter%2010.%20Web%20Application%20Quickstart%20%E5%BF%AB%E9%80%9F%E5%BC%80%E5%A7%8B%20Web%20%E5%BA%94%E7%94%A8.md#102-running-your-web-application-運行應用)10.2\. Running your web application 運行應用
需要應用 Jetty 插件來運行應用。
Example 10.2\. Running web application with Jetty plugin
build.gradle
~~~
apply plugin: 'jetty'
~~~
同樣需要應用 WAR 插件,當你執行?`gradle jettyRun`?時,將會運行應用在一個內嵌的 Jetty Web 容器里。運行?`gradle jettyRunWar`將會構建成 WAR 文件,接著運行在內嵌 的 Web 容器。
TODO:url,端口,以及源文件位置都可以在腳本中進行指定修改并重載。
*Groovy web 應用*
*在一個項目中你可以采用多個插件。比如你可以在 web 項目中同時使用War 插件和 Groovy 插件來構建基于 web 應用的 Groovy。適當的 Groovy 庫將被添加到 WAR 的文件中。*
## [](https://github.com/waylau/Gradle-2-User-Guide/blob/master/Chapter%2010.%20Web%20Application%20Quickstart%20%E5%BF%AB%E9%80%9F%E5%BC%80%E5%A7%8B%20Web%20%E5%BA%94%E7%94%A8.md#103-summary-總結)10.3\. Summary 總結
了解更多關于 War 插件 和 Jetty 插件的請參閱[Chapter 26\. The War Plugin 關于 War 插件](https://github.com/waylau/Gradle-2-User-Guide/blob/master/Chapter%2026.%20The%20War%20Plugin%20%E5%85%B3%E4%BA%8E%20War%20%E6%8F%92%E4%BB%B6.md)以及?[Chapter 28\. The Jetty Plugin 關于 Jetty 插件](https://github.com/waylau/Gradle-2-User-Guide/blob/master/Chapter%2028.%20The%20Jetty%20Plugin%20%E5%85%B3%E4%BA%8E%20Jetty%20%E6%8F%92%E4%BB%B6.md)。你可以在[https://github.com/waylau/Gradle-2-User-Guide-Demos](https://github.com/waylau/Gradle-2-User-Guide-Demos)?中 webApplication 下找到更多示例.
- 關于
- 第1章 Introduction 介紹
- 第2章 Overview 總覽
- 第3章 Tutorials 教程
- 第4章 Installing Gradle 安裝
- 第5章 Troubleshooting 問題解決
- 第6章 Build Script Basics 構建腳本的基礎識
- 第7章 Java Quickstart 快速開始 Java
- 第8章 Dependency Management Basics 依賴管理的基礎知識
- 第9章 Groovy Quickstart 快速開始 Groovy
- 第10章 Web Application Quickstart 快速開始 Web 應用
- 第11章 Using the Gradle Command-Line 使用 Gradle 命令行
- 第12章 Using the Gradle Graphical User Interface 使用 Gradle 圖形化用戶界面
- 第13章 Writing Build Scripts 編寫構建腳本
- 第14章 Tutorial - 'This and That' 教程-這個那個
- 第15章 More about Tasks 更多關于任務
- 第16章 Working With Files 跟文件工作
- 第17章 Using Ant from Gradle 從 Gradle 使用 Ant
- 第18章 Logging 日志.md
- 第19章 The Gradle Daemon 守護進程
- 第20章 The Build Environment 構建環境
- 第21章 Gradle Plugins 插件
- 第22章 Standard Gradle plugins 標準 Gradle 插件
- 附錄E Existing IDE Support and how to cope without it 支持的 IDE 以及如何應對沒有它