Close

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 1.5.9.RELEASE
    Corresponding Spring Version 4.3.13.RELEASE
  • spring-boot-starter-freemarker : Starter for building MVC web applications using FreeMarker views.
  • 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

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

    See Also