How to make a Spring Boot Jar into a War to deploy on tomcat

Spring boot is a brilliant addition to the Spring Framework however, not everyone wants to use the Jar format that it encourages you to use. Searching on the internet the information is there to switch from Jar to War however, it doesn’t seem to be in one place in the documentation so I thought I’d put it together as a single post.

If you create a WAR project straight away then the following will be provided, however, if you have started off with a Jar packaging type this article will explain how to switch it to war. pom.xml changes The first thing you will need to do is change your pom.xml file. Change <packaging> value from jar to war. Add the following to tell the project that the tomcat server is provided.

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-tomcat</artifactId>
  <scope>provided</scope>
</dependency>

Code changes In your Spring Boot application class that has the @SpringBootApplication annotation you need to extend SpringBootServletInitializer and override the configure method. e.g.

@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer{
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(DemoApplication.class);
    }
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

So now you should be able to deploy this war file to your application server, e.g. tomcat. Also, you will see in your target folder there are two war files, one with .original extension. This is basically because the Spring Boot maven plugin has built the war file such that it can still be run using java. e.g. java -jar demo-0.0.1-SNAPSHOT.war Hope this helps

5 comments

  1. Hi, thanks for the post.

    Very new to spring so please excuse my ignorance.

    I am following the “gs-serving-web-content example from Spring. Been able to create a spring boot JAR executable. Runs fine,

    Now, I have modified Application.java to the following:

    @ComponentScan
    @SpringBootApplication
    public class Application extends SpringBootServletInitializer {

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

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

    Also updated pom.xml …

    war

    org.springframework.boot
    spring-boot-starter-parent
    1.3.1.RELEASE

    org.springframework.boot
    spring-boot-starter-thymeleaf

    org.springframework.boot
    spring-boot-starter-tomcat
    provided

    Get the following error:
    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.
    1:compile (default-compile) on project gs-serving-web-content: Compilation failu
    re: Compilation failure:
    [ERROR] /C:/SpringSource/WORKSPACES/BESPIN/gs-serving-web-content-initial/src/ma
    in/java/hello/Application.java:[19,34] cannot find symbol
    [ERROR] symbol: class SpringBootServletInitializer
    [ERROR] /C:/SpringSource/WORKSPACES/BESPIN/gs-serving-web-content-initial/src/ma
    in/java/hello/Application.java:[17,2] cannot find symbol
    [ERROR] symbol: class ComponentScan
    [ERROR] /C:/SpringSource/WORKSPACES/BESPIN/gs-serving-web-content-initial/src/ma
    in/java/hello/Application.java:[27,47] cannot find symbol
    [ERROR] symbol: class SpringApplicationBuilder
    [ERROR] location: class hello.Application
    [ERROR] /C:/SpringSource/WORKSPACES/BESPIN/gs-serving-web-content-initial/src/ma
    in/java/hello/Application.java:[27,11] cannot find symbol
    [ERROR] symbol: class SpringApplicationBuilder
    [ERROR] location: class hello.Application
    [ERROR] /C:/SpringSource/WORKSPACES/BESPIN/gs-serving-web-content-initial/src/ma
    in/java/hello/Application.java:[26,1] method does not override or implement a method from a supertype

    Not sure what I am missing.

    Would really appreciate your help.

    A

    1. It could be an odd IDE issue. I had the same issue with my IDE today whilst taking a look at your issue. Using Maven on the command line always worked. Can you put your code into GitHub and I can take a quick look? I can’t answer completely right now but I can take a look later?

      Does just mvn install on the command line/terminal work?

    2. Hi please add the following import statements in your Application.java file

      import org.springframework.boot.web.support.SpringBootServletInitializer;
      import org.springframework.boot.builder.SpringApplicationBuilder;

    3. Hi,

      Please include the following import statements in your Aplication.java file

      import org.springframework.boot.web.support.SpringBootServletInitializer;
      import org.springframework.boot.builder.SpringApplicationBuilder;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s