Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

Web application resources or web application archives are commonly called WAR files. A WAR file is used to deploy a Java EE web application in an application server. Inside a WAR file, all the web components are packed into one single unit. These include JAR files, JavaServer Pages, Java servlets, Java class files, XML files, HTML files, and other resource files that we need for web applications.

Maven is a popular build management tool that is widely used in Java EE projects to handle build tasks like compilation, packaging, and artifact management. We can use the Maven WAR plugin to build the project as a WAR file.

In this tutorial, we’re going to consider the usage of the Maven WAR plugin with a Java EE application. For that, we’re going to create a simple Maven Spring Boot web application and generate a WAR file from it.

2. Setting up a Spring Boot Web Application

Let’s create a simple Maven, Spring Boot, and Thymeleaf web application to demonstrate the WAR file generating process.

First, we’re going to add dependencies to the pom.xml file needed to build our Spring Boot web application:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</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>

Next, let’s create our MainController class. In this class, we’re going to create a single GET controller method to view our HTML file:

@Controller
public class MainController {

    @GetMapping("/")
    public String viewIndexPage(Model model) {
        model.addAttribute("header", "Maven Generate War");
        return "index";
    }
}

Finally, it’s time to create our index.html file. Bootstrap CSS files are also included in the project, and some CSS classes are used in our index.html file:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Index</title>
    <!-- Bootstrap core CSS -->
    <link th:href="@{/css/bootstrap.min.css}" rel="stylesheet">
</head>
<body>
    <nav class="navbar navbar-light bg-light">
        <div class="container-fluid">
            <a class="navbar-brand" href="#">
                Baeldung Tutorial
            </a>
        </div>
    </nav>
    <div class="container">
        <h1>[[${header}]]</h1>
    </div>
</body>
</html>

3. Maven WAR Plugin

The Maven WAR plugin is responsible for collecting and compiling all the dependencies, classes, and resources of the web application into a web application archive.

There are some defined goals in the Maven WAR plugin:

  • war: This is the default goal that is invoked during the packaging phase of the project. It builds a WAR file if the packaging type is war.
  • exploded: This goal is normally used in the project development phase to speed up the testing. It generates an exploded web app in a specified directory.
  • inplace: This is a variant of the exploded goal. It generates an exploded web app inside the web application folder.

Let’s add the Maven WAR plugin to our pom.xml file:

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.4.0</version>
</plugin>

Now, once we execute the mvn install command, the WAR file will be generated inside the target folder.

Using the mvn:war:exploded command, we can generate the exploded WAR as a directory inside the target directory. This is a normal directory, and all the files inside the WAR file are contained inside the exploded WAR directory.

4. Include or Exclude WAR File Content

Using the Maven WAR plugin, we can filter the contents of a WAR file. Let’s configure the Maven WAR plugin to include an additional_resources folder inside the WAR file:

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.4.0</version>
    <configuration>
        <webResources>
            <resource>
                <directory>additional_resources</directory>
            </resource>
        </webResources>
    </configuration>
</plugin>

Once we execute the mvn install command, all the contents under the additional_resources folder will be available inside the WAR file. This is useful when we need to add some additional resources – like reports, for example – to the WAR file.

5. Edit Manifest File

The Maven WAR plugin allows customizing the manifest file. For example, we can add the classpath to the manifest file. This is very helpful when the WAR file is under a more complex structure and when we need to share the project dependencies among several modules.

Let’s configure the Maven WAR plugin to add the classpath to the manifest file:

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.4.0</version>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
            </manifest>
        </archive>
    </configuration>
</plugin>

6. Conclusion

In this short tutorial, we discussed how to generate a WAR file using the Maven build tool. We created a Maven Spring Boot web application to demonstrate the work. To generate the WAR file, we used a special plugin called the Maven WAR plugin.

The full source code example is available over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – Maven (eBook) (cat=Maven)
Comments are closed on this article!