今天给大家解释一下,如何利用IDEA将SpringBoot的项目打包成war文件。
关于为什么要打包成war文件,我这里就不多介绍了,大家有兴趣的可以自己去Google一下。
下面就给出具体的操作步骤:
步骤一:修改pox.xml文件
1.首先将 <packaging>jar</packaging>修改为 <packaging>war</packaging>
2.在dependencies里面添加以下代码:
<dependency>步骤二:修改SpringBoot中的启动文件
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-legacy</artifactId>
<version>1.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
package example;步骤三:也是最重要的步骤,就是修改完后不要运行该项目,会报错的。
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文件的所有步骤就完成了。
如果大家对博客有啥异议或者想向我提什么问题都可以加我QQ:208017534 欢迎打扰哦!!!