七和,七和读书七和,七和读书

Hi, 请登录     注册    退出

IDEA中的spring boot项目打包成war包

SpringBoot项目在打包的时候默认是jar包的形式。但在有的时候,我们可能需要将一个 Spring Boot 项目打成 WAR 包,然后使用传统的方式进行部署到Tomcat上面。打成 WAR 包的具体步骤如下:

1)首先修改 pom.xml 文件,添加如下高亮部分配置,将默认的 jar 方式改为 war 方式:<packaging>war</packaging>

<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<packaging>war</packaging>

2) 修改后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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
  <groupId>com.example</groupId>
  <artifactId>demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <!-- 打包方式为war包 -->
    <packaging>war</packaging>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <profiles>
        <profile>
            <id>local</id>
            <properties>
                <profileActive>local</profileActive>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>dev</id>
            <properties>
                <profileActive>dev</profileActive>
            </properties>
        </profile>
        <profile>
            <id>pro</id>
            <properties>
                <profileActive>pro</profileActive>
            </properties>
        </profile>
    </profiles>
    <build>
        <!-- 修改打包后war包的名称 -->
        <finalName>springboot-profile</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!-- 打包时报缺失web.xml时可加此插件 -->
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.2.0</version>
      </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/webapp</directory>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>application-${profileActive}.yml</include>
                    <include>application.yml</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>
</project>

3)提供一个 SpringBootServletInitializer 的子类,可以直接修改启动类使其实现SpringBootServletInitializer;也可在于SpringBoot启动类同级的目录下新建一个类去实现SpringBootServletInitializer,并覆盖其 configure 方法,完成初始化操作,代码如下:

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

class SpringbootProfileServletInitializer extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(SpringbootProfileApplication.class);
    }}


IDEA中的spring boot项目打包成war包(图1)

4)在idea中打war包的方式有两种

    1.通过idea提供的图形化工具打包,在maven窗口

双击idea中右上角maven下的package或者install即可完成打包

IDEA中的spring boot项目打包成war包(图2)

    2.通过idea的命令行打包,需打开Terminal窗口

IDEA中的spring boot项目打包成war包(图3)

如果SpringBoot项目为多环境项目,打包的时候只需要特定的配置文件的时候需要通过命令行加参数的方式进行打包,打开idea下方的Terminal,然后执行命令 mvn clean package -P 环境名称,也可以进行打包。

5)打包成功后即可在项目的Target下面找到打包完成的war包,产生BUILD SUCCESS 即表示成功

IDEA中的spring boot项目打包成war包(图4)

IDEA中的spring boot项目打包成war包(图5)

6)打包成功后会在 target 目录下生成一个 WAR 包。将该文件复制到 Tomcat 的 webapps 目录下,启动 Tomcat 即可。

未经允许不得转载:七和,七和读书 » IDEA中的spring boot项目打包成war包
加载中~