Spring Boot - Spring Boot FreeMarker War packaging

The following example shows how to package FreeMarker based Spring Boot application as war so that it can be deployed to a Servlet container server. Also check out our FreeMarker Jar packaging Boot tutorial here.

pom.xml

<project .....>
<modelVersion>4.0.0</modelVersion>
<groupId>com.logicbig.example</groupId>
<artifactId>spring-boot-freemarker-war-example</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-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>

Boot main config class

@SpringBootApplication
public class ExampleMain extends SpringBootServletInitializer {
  @Override
  protected SpringApplicationBuilder configure (SpringApplicationBuilder builder) {
      return builder.sources(ExampleMain.class);
  }

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

To run as war via tomcat maven plugin (included in pom.xml):

mvn tomcat7:run-war

Output

Example Project

Dependencies and Technologies Used :

  • spring-boot-starter-freemarker 1.5.9.RELEASE: Starter for building MVC web applications using FreeMarker views.
    Corresponding Spring version: 4.3.13.RELEASE
  • spring-boot-starter-tomcat 1.5.9.RELEASE: Starter for using Tomcat as the embedded servlet container. Default servlet container starter used by spring-boot-starter-web [Description from spring-boot-starter-tomcat-1.5.9.RELEASE.pom]
    Corresponding Spring version: 4.3.13.RELEASE
  • JDK 1.8
  • Maven 3.3.9

spring-boot-freemarker-war-example Select All Download
  • spring-boot-freemarker-war-example
    • .settings
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • resources
          • templates

See Also