美文网首页
SpringBoot项目打包成war包

SpringBoot项目打包成war包

作者: singleZhang2010 | 来源:发表于2020-09-27 15:43 被阅读0次

1、在pom.xml文件中,修改打包形式

<packaging>war</packaging>

2、在pom.xml文件中,移除嵌入式tomcat插件,或将原来的内置tomcat依赖scope改为provided

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

<!-- 将原来的内置tomcat依赖scope改为provided-->
<!-- 内置tomcat -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

<!-- 按照SpringBoot项目结构是没有Web目录的,maven插件需要增加这行:failOnMissingWebXml-->
    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>

添加javax.servlet-api或者tomcat-servlet-api依赖

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version>
</dependency>

<!-- 或tomcat-servlet-api-->
<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-servlet-api</artifactId>
    <version>8.0.36</version>
    <scope>provided</scope>
</dependency>

4、修改项目默认启动方式,启动类继承SpringBootServletInitializer类重写configure()方法,或新建类继承SpringBootServletInitializer类重写configure()方法

package com.single.zxblog;


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


@SpringBootApplication
public class ZxblogApplication extends SpringBootServletInitializer {
    
    public static void main(String[] args) {
        SpringApplication.run(ZxblogApplication.class, args);
    }
    
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(ZxblogApplication.class);
    }
}

或在原启动类同级下新建ZxblogStartApplication启动类,重写configure()方法并指向ZxblogApplication类

package com.single.zxblog;


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


public class ZxblogStartApplication extends SpringBootServletInitializer {
    /**
     * 默认返回return super.configure(builder);
     * 现指向ZxblogApplication.main()启动方法
     * @param builder
     * @return
     */
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(ZxblogApplication.class);
    }
}

5、打包

E:\java_project\zxblog>mvn clean package -Dmaven.test.skip=true
打包成功

放入tomcat的webapps目录下即可

相关文章

网友评论

      本文标题:SpringBoot项目打包成war包

      本文链接:https://www.haomeiwen.com/subject/kohluktx.html