Deployable WARs in Spring Boot, IntelliJ and Gradle

There are some good references on how to launch a deployable WAR in Spring Boot with Gradle and you’ll find some of those in the post. Our main focus here is to cover the highlights of creating a successful WAR in Gradle, then load that artifact in an IntelliJ Tomcat Run configuration.

Hopefully as a result you will be able to break through the proverbial “Requested Resource is not available” error which I personally spent too much time looking at prior to this post.

Why bring an IntelliJ Tomcat Run Configuration into this and not simply use Gradle? The main reason for me is that it automatically launches the browser where Gradle run options do not. I also find it more natural to launch an IntelliJ debugging session with it.

The @SpringBootApplication Class

Here’s the key to launching a successful WAR. Use the @SpringBootApplication annotation on your main class and extend SpringBootServletInitializer.

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
        }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

With this initialization in place you’ll be able to use both $ gradle run and $ gradle bootRun, as well as successfully launch the generated WAR artifact in a Tomcat Run Configuration. Here’s the Spring How-To on creating a deployable WAR file.

Gradle

In the current Spring Reference Guide 59.4 Packaging executable jar and war files it states “To build a war file that is both executable and deployable into an external container, you need to mark the embedded container dependencies as belonging to a configuration named ‘providedRuntime'”, and goes on to show the Gradle code. I found that with the Main Class annotated with @SpringBootApplication and extending SpringBootServletInitializer as shown above this is not necessary, at least when used in the IntelliJ Tomcat launching scenario.

Here are the highlights of build.gradle that pertain to building the deployable WAR file.

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.3.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'spring-boot'
apply plugin: 'war'

bootRun {
    addResources = true
}
...

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-starter-thymeleaf")
    testCompile("junit:junit")
}

jar {
    baseName = 'hello'
    version =  '0.0.1'
}


war {
    baseName = 'hello'
}

Then again, it’s fun to execute a Jar! Simply build the app in Gradle and then go to http://localhost:xxxx/ to view.

You can find the full source from this post in the v0.1.2 branch of my Spring-Data app on GitHub.