Generate a WAR File in Maven – 在Maven中生成一个WAR文件

最后修改: 2021年 10月 7日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

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.

网络应用资源或网络应用档案通常被称为WAR文件。WAR文件用于在应用服务器中部署一个Java EE网络应用。在一个WAR文件中,所有的Web组件都被打包成一个单一的单元。其中包括JAR文件、JavaServer Pages、Java servlets、Java类文件、XML文件、HTML文件和其他我们需要的网络应用的资源文件。

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.

Maven是一个流行的构建管理工具,被广泛用于Java EE项目,以处理编译、打包和工件管理等构建任务。我们可以使用Maven的WAR插件,将项目构建为WAR文件

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.

在本教程中,我们将考虑Maven WAR插件在Java EE应用中的使用。为此,我们将创建一个简单的Maven Spring Boot Web应用,并从中生成一个WAR文件。

2. Setting up a Spring Boot Web Application

2.设置一个Spring Boot Web应用程序

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

让我们创建一个简单的Maven、Spring Boot和Thymeleaf网络应用来演示WAR文件的生成过程。

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

首先,我们要向pom.xml文件添加构建Spring Boot网络应用程序所需的依赖项。

<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:

接下来,让我们创建我们的MainController类。在这个类中,我们将创建一个单一的GET控制器方法来查看我们的HTML文件。

@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:

最后,是时候创建我们的index.html文件了。Bootstrap的CSS文件也包含在项目中,一些CSS类被用于我们的index.html文件中。

<!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

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.

Maven WAR插件负责收集Web应用的所有依赖项、类和资源,并将其编译成一个Web应用档案。

There are some defined goals in the Maven WAR plugin:

在Maven WAR插件中,有一些确定的目标。

  • 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:

让我们在pom.xml文件中添加Maven WAR插件。

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

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

现在,一旦我们执行mvn install命令,WAR文件就会在目标文件夹内生成。

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.

使用mvn:war:exploded命令,我们可以将爆炸的WAR生成为目标目录内的一个目录。这是一个正常的目录,WAR文件内的所有文件都包含在爆炸的WAR目录内。

4. Include or Exclude WAR File Content

4.包括或不包括WAR文件内容

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:

使用Maven WAR插件,我们可以过滤WAR文件的内容。让我们配置Maven WAR插件,在WAR文件中加入一个additional_resources文件夹。

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.3.1</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.

一旦我们执行mvn install命令,additional_resources文件夹下的所有内容都将在WAR文件中可用。当我们需要在WAR文件中添加一些额外的资源–比如说报告–时,这很有用。

5. Edit Manifest File

5.编辑舱单文件

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.

Maven WAR插件允许自定义manifest文件。例如,我们可以在manifest文件中添加classpath。当WAR文件的结构比较复杂,以及我们需要在多个模块之间共享项目依赖时,这一点非常有用。

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

我们来配置Maven WAR插件,将classpath添加到manifest文件中。

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

6. Conclusion

6.结语

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.

在这个简短的教程中,我们讨论了如何使用Maven构建工具生成一个WAR文件。我们创建了一个Maven Spring Boot网络应用程序来演示这项工作。为了生成WAR文件,我们使用了一个名为Maven WAR插件的特殊插件。

The full source code example is available over on GitHub.

完整的源代码示例可在GitHub上获得over