电光石火-穿越时空电光石火-穿越时空


利用IDEA将SpringBoot的项目打包成war文件

步骤一:修改pox.xml文件



1.首先将 <packaging>jar</packaging>修改为 <packaging>war</packaging>


2.在dependencies里面添加以下代码:

       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-legacy</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
        </dependency>

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

步骤二:修改SpringBoot中的启动文件

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ActiveProfiles;
@Configuration
@ComponentScan
@EnableAutoConfiguration
@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(DemoApplication.class);
    }

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

步骤三:也是最重要的步骤,就是修改完后不要运行该项目,会报错的。
因为这个是为打包而设计的方案,如果大家想要运行项目的话,一定要把spring-boot-starter-tomcat中 <scope>provided</scope>注释掉才可以运行,不然肯定会出错,这点大家一定要注意。
正确的操作步骤应该是在IDEA中找到Build--》Build Artifacts--》点击生成war包,这样利用IDEA将SpringBoot的项目打包成war文件的所有步骤就完成了。

本博客所有文章如无特别注明均为原创。作者:似水的流年
版权所有:《电光石火-穿越时空》 => 利用IDEA将SpringBoot的项目打包成war文件
本文地址:http://www.ilkhome.cn/index.php/archives/332/
欢迎转载!复制或转载请以超链接形式注明,文章为 似水的流年 原创,并注明原文地址 利用IDEA将SpringBoot的项目打包成war文件,谢谢。

评论