提到intellij idea,相信做java开发的工程师对它并不陌生。intellij idea的强大之处就不做阐述了。因为它的强大,越来越多的人慢慢抛弃Eclipse而选用了intellij idea,但对基于Springboot的项目,在工作中出现了好多人不会打war包,或者打出的war包有问题而导致无法部署等各种情况。那么我们如何正确的打出war包,这是本博文要介绍的重点。
1.打开pom文件,将pom.xml中的打包方式修改为war,如下:
<packaging>war</packaging>
2.在pom.xml中添加并开启依赖,将scope状态修改为provided
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
3.修改src/main/java下的application启动项
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.support.SpringBootServletInitializer; @SpringBootApplication public class Application extends SpringBootServletInitializer{ @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application){ return application.sources(Application.class); } public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
4.配置Artifacts选项,配置如下:
选择“file->Project Structure->Artifacts”,打开Project Structure弹框,如图:

选择WEB-INFO/classes,再点击上方的‘+’按钮,如图:

选择”Module Output”,在弹框中,将所有文件都选中,点击”OK”,如下图:

5.配置资源文件。根据项目编写的静态资源文件,选取对应的文件夹。比如我的项目资源文件在app-admin下面。选中”<output root>”,然后点击”+”按钮,选择”Directory Content”,在弹框中选择”app-admin>src>main>resources”文件夹,点击确定,如下图所示:

结果如下图:

6.配置已经完成,接下来就可以打包了。选择菜单”Build>Build Arifacts”,弹框中双击”All Arifacts”,等待一会儿,在项目目下就会生成target文件夹,里面就是打好的war包。
原创文章,作者:Ferrycoln,如若转载,请注明出处:https://ms200.cn/archives/791