MENU

SpringBoot打包war

September 1, 2020 • 技术分享,网络资源

说明

本文基于SpringBoot 2.3.3.RELEASE作为演示.

目前SpringBoot 2.3.3.RELEASE 最常见的打包方式有两种:

  • jar方式: 服务化的概念, 后续接触SpringCloud, 所有的服务打包都是以jar的方式存在.
  • war方式: 应用程序的概念, 也可以向外提供服务和接口.

本文只记录war包的打包方式, 如果你的项目已经修改过那么可以跳过本文大部分内容, 如果你的项目从 Spring Initializr 拉下来就没修改过那么就可以参照本文实现war打包.

1. 修改项目pom.xml文件

1.1 修改pom中的打包方式为war, 原来为jar.

<?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.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
</project>

其中第10行打包常用的类型如下(排名不分先后):

  • pom: 父类型都为pom类型, 常用于 maven 多模块开发.
  • war: 需要部署的项目.
  • jar: 内部调用或者是作服务使用.
  • ear: 包含全部企业应用程序. 在这种情形下,一个企业应用程序被定义为多个jar文件, 资源, 类和Web应用程序的集合.

1.2 排除SpringBoot starter自带的Tomcat

具体在spring-boot-starter还是spring-boot-starter-web排除取决于你当前项目有哪些依赖, 如果两个都有那就都排除.

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <groupId>org.springframework.boot</groupId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

1.3 添加Servlet依赖

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <scope>provided</scope>
</dependency>

无用的依赖请忽略, 完整pom.xml如下:

<?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.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>demo</name>
    <description>demo</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
        <relativePath/>
    </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-web</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                    <groupId>org.springframework.boot</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2. 修改启动类

新建一个Java类, 继承自 org.springframework.boot.web.servlet.support.SpringBootServletInitializer.

重写 SpringApplicationBuilder configure(SpringApplicationBuilder builder) 方法.

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

/**
 * War包启动类
 *
 * @author biuaxia
 * @date 2020/9/1
 */
public class WarStarterApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        // 指向本项目的启动类
        return builder.sources(BootstrapApplication.class);
    }
}

在类 WarStarterApplication 的第15行, 原来的代码为 return super.configure(builder);.

3. 打包

我们可以通过IDEA的自带的Maven实现打包, 也可以通过命令行实现, 下面展示命令行方式.

在项目空间根目录下CMD执行:

mvn clean install package -DskipTests

其中的 -DskipTests 参数为 跳过SpringBoot的单元测试, 它也有两种常用的方式:

mvn clean install -DskipTests
mvn clean install -Dmaven.test.skip=true

不发散内容了, 继续来看打包的输出结果:

C:\Users\Administrator\IdeaProjects\demo>mvn clean install package -DskipTests
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< com.example:demo >--------------------------
[INFO] Building demo 0.0.1-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ demo ---
[INFO] Deleting C:\Users\Administrator\IdeaProjects\demo\target
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to C:\Users\Administrator\IdeaProjects\demo\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\Administrator\IdeaProjects\demo\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:\Users\Administrator\IdeaProjects\demo\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ demo ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-war-plugin:3.2.3:war (default-war) @ demo ---
[INFO] Packaging webapp
[INFO] Assembling webapp [demo] in [C:\Users\Administrator\IdeaProjects\demo\target\demo-0.0.1-SNAPSHOT]
[INFO] Processing war project
[INFO] Webapp assembled in [561 msecs]
[INFO] Building war: C:\Users\Administrator\IdeaProjects\demo\target\demo-0.0.1-SNAPSHOT.war
[INFO]
[INFO] --- spring-boot-maven-plugin:2.3.3.RELEASE:repackage (repackage) @ demo ---
[INFO] Replacing main artifact with repackaged archive
[INFO]
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ demo ---
[INFO] Installing C:\Users\Administrator\IdeaProjects\demo\target\demo-0.0.1-SNAPSHOT.war to C:\Users\Administrator\Java\Maven3.6.2\local\repo\com\example\demo\0.0.1-SNAPSHOT\demo-0.0.1-SNAPSHOT.war
[INFO] Installing C:\Users\Administrator\IdeaProjects\demo\pom.xml to C:\Users\Administrator\Java\Maven3.6.2\local\repo\com\example\demo\0.0.1-SNAPSHOT\demo-0.0.1-SNAPSHOT.pom
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to C:\Users\Administrator\IdeaProjects\demo\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\Administrator\IdeaProjects\demo\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:\Users\Administrator\IdeaProjects\demo\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ demo ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-war-plugin:3.2.3:war (default-war) @ demo ---
[INFO] Packaging webapp
[INFO] Assembling webapp [demo] in [C:\Users\Administrator\IdeaProjects\demo\target\demo-0.0.1-SNAPSHOT]
[INFO] Processing war project
[INFO] Webapp assembled in [186 msecs]
[INFO] Building war: C:\Users\Administrator\IdeaProjects\demo\target\demo-0.0.1-SNAPSHOT.war
[INFO]
[INFO] --- spring-boot-maven-plugin:2.3.3.RELEASE:repackage (repackage) @ demo ---
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  11.389 s
[INFO] Finished at: 2020-09-01T14:13:27+08:00
[INFO] ------------------------------------------------------------------------
C:\Users\Administrator\IdeaProjects\demo>cd target

C:\Users\Administrator\IdeaProjects\demo\target>dir
 驱动器 C 中的卷是 系统
 卷的序列号是 0809-0513

 C:\Users\Administrator\IdeaProjects\demo\target 的目录

2020/09/01  14:13    <DIR>          .
2020/09/01  14:13    <DIR>          ..
2020/09/01  14:13    <DIR>          classes
2020/09/01  14:13    <DIR>          demo-0.0.1-SNAPSHOT
2020/09/01  14:13        14,386,334 demo-0.0.1-SNAPSHOT.war
2020/09/01  14:13        12,601,594 demo-0.0.1-SNAPSHOT.war.original
2020/09/01  14:13    <DIR>          generated-sources
2020/09/01  14:13    <DIR>          generated-test-sources
2020/09/01  14:13    <DIR>          maven-archiver
2020/09/01  14:13    <DIR>          maven-status
2020/09/01  14:13    <DIR>          test-classes
               2 个文件     26,987,928 字节
               9 个目录 269,520,134,144 可用字节

我们可以看到在项目的 target 目录下已经有 war 包的输出文件 demo-0.0.1-SNAPSHOT.war 文件了.

这个 demo-0.0.1-SNAPSHOT.war 文件就是我们需要的东西, 可以拿到服务器的 Tomcatwebapps 直接启动啦~

ArchivesQR Code
QR Code for this page
Tipping QR Code