spring boot 用war包部署到tomcat下详细教程(解决缺少web.xml报错的问题)

1.首先 修改pom.xml下的打包方式

<packaging>warpackaging>

2.添加servlet-api依赖

 
<dependency>
    <groupId>javax.servletgroupId>
    <artifactId>javax.servlet-apiartifactId>
    <version>3.1.0version>
    <scope>providedscope>
dependency>

3.去除springboot 内嵌的tomcat模块

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-webartifactId>
    
    <exclusions>
        <exclusion>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-tomcatartifactId>
        exclusion>
    exclusions>
dependency>

4.修改编译设置

方法1:
<build>
  <plugins>
    <plugin>
      <artifactId>maven-war-pluginartifactId>
      <version>2.6version>
      <configuration>
              
        <failOnMissingWebXml>falsefailOnMissingWebXml>                        
      configuration>
    plugin>
  plugins>
build>

方法2:
添加以下配置到pom.xml文件下

版本3.0.0的插件 web.xml不存在问题,所以可以通过升级插件来解决问题

<plugin>
  <artifactId>maven-war-pluginartifactId>
  <version>3.0.0version>
plugin>

5.修改启动类

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

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

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(this.getClass());
    }
}

6.打包编译

你可能感兴趣的:(JAVA)