SpringBoot JAR to WAR

add the following line to the pom.xml

<packaging>war</packaging>

then add the following within the dependencies

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

<scope>provided</scope> means the dependency:

  • are provided at runtime by JDK or a container
  • are available only at compile-time and in the test classpath of the project

Running SpringBoot’s Embedded Tomcat Servlet

even though the pom.xml specifies a war package, we can still compile and run the SpringBoot application with it’s embedded tomcat servlet

  1. mvn clean compile
  2. mvn spring-boot:run

Running Packaged WAR on External Tomcat Servlet

now let’s package the SpringBoot application as a .war file and deploy it on an external Tomcat servlet

  1. mvn clean package
  2. retrieve spring-boot-war-example.war file from target/ directory
  3. place it into tomcat’s libexec/webapps
  4. start tomcat server
  5. open browser to http://localhost:8080/spring-boot-war-example
  6. if you see hello world then success!