Close

Spring Boot - Running main class from IDE with Spring Boot War packaging and with spring-boot-starter-tomcat scope=provided

When we package Spring Boot Web application as war (check out the related tutorial here) so that it can be deployed to a Servlet container, we need to exclude spring-boot-starter-tomcat dependency to avoid interfering of the Boot's embedded tomcat and the runtime Servlet container. By doing so we can no longer run the boot application from IDE or run it via boot plugin or as executable war file, that's because all of these methods need Spring Boot's embedded tomcat server. Instead of adding/removing spring-boot-starter-tomcat to work in all environments, we can use maven profiles as follows:

pom.xml

<project .....>
<modelVersion>4.0.0</modelVersion>

<groupId>com.logicbig.example</groupId>
<artifactId>traditional-war-packing-running-main-method</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
</dependencies>

<profiles>
<profile>
<id>warBuild</id>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/</path>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

Now we can run the web application from IDE, via spring-boot:run or via executable war file as usual.

To deploy to a Servlet container or run via tomcat maven plugin (included in pom.xml), we need to specify the profile 'warBuild':

mvn -P warBuild tomcat7:run-war

Example Project

Dependencies and Technologies Used:

  • Spring Boot 1.5.9.RELEASE
    Corresponding Spring Version 4.3.13.RELEASE
  • spring-boot-starter-web : Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container.
  • spring-boot-starter-tomcat : Starter for using Tomcat as the embedded servlet container. Default servlet container starter used by spring-boot-starter-web.
  • JDK 1.8
  • Maven 3.3.9

traditional-war-packing-running-main-method Select All Download
  • traditional-war-packing-running-main-method
    • src
      • main
        • java
          • com
            • logicbig
              • example
    • pom.xml

    See Also