Springboot打成war包并在tomcat中运行

  1. 在pom.xml里设置
    <packaging>war</packaging>

2.加依赖

 <!--声明spring boot内嵌tomcat的作用范围  在运行时不起作用-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

四、修改启动类

package com.xyy.medical;

import org.mybatis.spring.annotation.MapperScan;
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;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@MapperScan("com.xyy.dao")
@ComponentScan(basePackages = {"com"})
public class MedicalApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(MedicalApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        // 注意这里要指向原先用main方法执行的Application启动类
        return builder.sources(MedicalApplication.class);
    }

}

五、打包部署 mvn clean package

image.png

BUILD SUCCESS ~
打包成功

image.png

如果发现打包多了个 original结尾的war包
把pom文件的 这个注释掉就可以了

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

参考了
https://yq.aliyun.com/articles/319770

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。