ranyeli
9/29/2017 - 7:45 PM

SpringBoot to Tomcat WAR

Creating a springboot war that can be run on tomcat

1.  First your pom.xml must be similar to this with packaging as war and the repackage goal:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.project</groupId>
	<artifactId>invoiceBackend</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>

	<name>invoiceBackend</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.6.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
			<!-- <version>1.5.6.RELEASE</version> -->
		</dependency>

		<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>
			<scope>provided</scope>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<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>
<!-- Note: when changing the packaging from jar to war and vice versa always update the project on maven  -->
    
2.  Your main class or applicaciont class must extend SpringBootServletInitializer
    and add the configure method like this:
    
    package com.project;

import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

@SpringBootApplication
public class InvoiceBackendApplication extends SpringBootServletInitializer {
	
	// to be able to use the war file
	protected SpringApplicationBuilder configure(SpringApplicationBuilder app) {
		return app.sources(InvoiceBackendApplication.class).bannerMode(Banner.Mode.OFF);
	}

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

3.1.  To avoid the app name on the url go to your IDE and:
      1.  double click on the server
      2.  scroll down to the and select the module tab
      3.  select your app and edit the path to "/"
      
      this modifies the server.xml with the following configuration:
      
      <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t &quot;%r&quot; %s %b" prefix="localhost_access_log" suffix=".txt"/>

      <Context docBase="invoiceBackend" path="/" reloadable="true" source="org.eclipse.jst.jee.server:invoiceBackend"/></Host>
      
3.2 run "mvn packaging" on the project root directory to create the .war file in the target folder
    and rename the .war file to "ROOT.war" to make sure the app name is removed from url