美文网首页
SpringBoot项目打包去除内嵌的Tomcat

SpringBoot项目打包去除内嵌的Tomcat

作者: 执着的逗比 | 来源:发表于2019-11-30 14:15 被阅读0次

      最近做了一个SpringBoot的小项目,需要发布到Tomcat上去运行起来,但是打war包的时候却发现打包失败,无法打包成war,百度了半天才发现,在我们需要把SpringBoot项目打包成war包的时候,需要把SpringBoot内嵌的Tomcat去除,以下就是具体的修改步骤:

    一、POM(去除内嵌tomcat后,需要添加servlet依赖)

    <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>  
    <!--添加servlet的依赖-->  
    <dependency>  
      <groupId>javax.servlet</groupId>  
      <artifactId>javax.servlet-api</artifactId>  
      <version>3.1.0</version>  
      <scope>provided</scope>  
    </dependency>  
    
      <plugin>  
        <artifactId>maven-war-plugin</artifactId>  
        <version>3.0.0</version>  
      </plugin>
    

    二、继承SpringBootServletInitializer重写configure方法

    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 ExportApplication extends SpringBootServletInitializer {  
        public static void main(String[] args) {  
            SpringApplication.run(ExportApplication.class, args);  
        }  
      
        @Override  
        protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {  
            return builder.sources(this.getClass());  
        }  
      
    }
    

    三、这样再去打war包就可以了

    相关文章

      网友评论

          本文标题:SpringBoot项目打包去除内嵌的Tomcat

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