宋子宪博客

Spring Boot项目打包成war包配置

pom.xml文件中,将打包方式改为war

<packaging>war</packaging>

然后添加如下的Tomcat依赖配置,覆盖Spring Boot自带的Tomcat依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

移除tomcat插件

<!-- 移除tomcat插件-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId> <!-- 移除嵌入式tomcat插件 -->
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<build></build>标签内配置项目名(该配置类似于server.context-path=mrbird):(可忽略)

...
<build>
    ...
    <finalName>mrbird</finalName>
</build>
...

修改启动类SpringApplication

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

/**
 * @author greatdata 2018-10-28 00:38:13
 */
@SpringBootApplication
public class XxlJobAdminApplication extends SpringBootServletInitializer {

    /**
     * 住程序入口
     * @param args
     */
    public static void main(String[] args) {
        SpringApplication.run(XxlJobAdminApplication.class, args);
    }

    /**
     * war添加
     * @param application
     * @return
     */
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(XxlJobAdminApplication.class);
    }

}

添加pom

    <!-- tomcat -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <!-- 移除tomcat插件-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId> <!-- 移除嵌入式tomcat插件 -->
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

配置好之后,在DOS窗口进入项目的目录下运行mvn clean package命令即可在target目录下生产war包
因为springboot项目打包不需要web.xml所以,打包得时候需要在pom里面进行声明,添加:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
            <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
    </plugin>

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »