Chapter 49. Web Application Quickstart

Table of Contents

49.1. Building a WAR file
49.2. Running your web application
49.3. Summary

This chapter is a work in progress.

This chapter introduces the Gradle support for web applications. Gradle recommends two plugins for web application development: the War plugin and the Gretty plugin. The War plugin extends the Java plugin to build a WAR file for your project. The Gretty plugin allows you to deploy your web application to an embedded Jetty web container.

49.1. Building a WAR file

To build a WAR file, you apply the War plugin to your project:

Example 49.1. War plugin

build.gradle

apply plugin: 'war'

Note: The code for this example can be found at samples/webApplication/quickstart in the ‘-all’ distribution of Gradle.


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, in the WEB-INF/classes and WEB-INF/lib directories, respectively.

Groovy web applications

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.

49.2. Running your web application

To run your web application, you apply the Gretty plugin to your project:

Example 49.2. Running web application with Gretty plugin

build.gradle

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'org.akhikhl.gretty:gretty:1.4.2'
    }
}
apply plugin: 'org.akhikhl.gretty'

This also applies the War plugin to your project. Running gradle appRun will run your web application in an embedded servlet container. Running gradle appRunWar will build the WAR file, and then run it in an embedded web container.

49.3. Summary

You can find out more about the War plugin in Chapter 50, The War Plugin. You can find more sample Java projects in the samples/webApplication directory in the Gradle distribution.