本章介紹了Gradle對Web工程的相關支持。Gradle爲Web開發提供了兩個主要插件,War plugin 和 Jetty plugin。 其中War plugin繼承自Java plugin,可以用來打war包。jetty plugin繼承自War plugin作爲工程部署的容器.
This chapter introduces some of the Gradle's support for web applications. Gradle provides two plugins for web application development: the War plugin and the Jetty plugin. The War plugin extends the Java plugin to build a WAR file for your project. The Jetty
plugin extends the War plugin to allow you to deploy your web application to an embedded Jetty web container.
需要打包War文件,需要在腳本中使用War plugin:
To build a WAR file, you apply the War plugin to your project:
例 10.1. War plugin
Example 10.1. War plugin
build.gradle
apply plugin: 'war'
備註: 本示例代碼可以在Gradle發行包中的 samples/webApplication/quickstart
路徑下找到
Note: The
code for this example can be found at samples/webApplication/quickstart
which
is in both the binary and source distributions of Gradle.
由於繼承自Java插件,當你執行 gradle
build
時,將會編譯、測試、打包你的工程. Gradle會在 src/main/webapp
下尋找Web工程文件.編譯後的classes文件以及運行時依賴也都會被包含在War包中.
This also applies the Java plugin to your project. Running gradle
build
will compile, test and WAR your project. Gradle will look for the source files to include in the WAR file in src/main/webapp
.
Your compiled classes, and their runtime dependencies are also included in the WAR file.
Groovy web構建
Groovy web applications
在一個工程中你可以採用多個插件.比如你可以在web工程中同時使用War plugin和Groovy plugin. 插件會將Gradle依賴添加到你的War包中.
You can combine multiple plugins in a single project, so you can use the War and Groovy plugins together to build a Groovy based web application. The appropriate groovy libraries will be added to the WAR file for you.
要啓動Web工程,只需使用Jetty plugin即可:
To run your web application, you apply the Jetty plugin to your project:
例 10.2. 採用Jetty plugin啓動web工程
Example 10.2. Running web application with Jetty plugin
build.gradle
apply plugin: 'jetty'
由於Jetty plugin繼承自War plugin.調用gradle
jettyRun
將會把你的工程啓動部署到jetty容器中. 調用gradle
jettyRunWar
會打包並啓動部署到jetty容器中.
This also applies the War plugin to your project. Running gradle
jettyRun
will run your web application in an embedded Jetty web container. Running gradle
jettyRunWar
will build the WAR file, and then run it in an embedded web container.
待添加:使用哪個URL,配置端口,使用源文件的 地方,可編輯你的文件,以及重新加載的內容。
TODO: which url, configure port, uses source files in place and can edit your files and reload.
瞭解更多關於War plugin和Jetty plugin的應用請參閱第 26 章, War
Plugin以及 第 28 章, Jetty
Plugin . 你可以在發行包的samples/webApplication
下找到更多示例.
You can find out more about the War plugin in Chapter 26, The
War Plugin and the Jetty plugin in Chapter 28, The
Jetty Plugin . You can find more sample Java projects in the samples/webApplication
directory
in the Gradle distribution.