博客
关于我
SpringBoot在IDEA中以war打包
阅读量:383 次
发布时间:2019-03-05

本文共 2401 字,大约阅读时间需要 8 分钟。

第一步

在pom.xml中将默认的jar方式改为war:

...	
homeworkme
0.0.1-SNAPSHOT
homeworkme
Demo project for Spring Boot
war
1.8
...
...

第二步

排除内置的Tomcat容器(两种方式):

1.排除spring-boot-starter-web中的Tomcat:

org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-tomcat

2.添加依赖:

org.springframework.boot
spring-boot-starter-tomcat
provided

第三步

在springBoot启动类的同级目录添加一个继承SpringBootServletInitializer 的类或者启动类继承该类,实现configure方法。

SpringBootServletInitializer源码注释:

Note that a WebApplicationInitializer is only needed if you are building a war file and deploying it.
If you prefer to run an embedded web server then you won’t need this at all.
注意,如果您正在构建WAR文件并部署它,则需要WebApplicationInitializer。
如果你喜欢运行一个嵌入式Web服务器,那么你根本不需要这个。

package com.example.homeworkme;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;public class ServletInitializer extends SpringBootServletInitializer {       public ServletInitializer() {           System.out.println("初始化ServletInitializer");    }    @Override    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {           return application.sources(HomeworkmeApplication.class);//启动类名    }}

第四步

为了防止应用上下文所导致的项目访问资源加载不到的问题,建议pom.xml文件中<build></build>标签下添加<finalName></finalName>标签,并设置maven打包时跳过单元测试:

war包名称
org.apache.maven.plugins
maven-surefire-plugin
2.20.1
true
org.springframework.boot
spring-boot-maven-plugin

第五步

通过maven命令进行打包或者在IDEA顶部菜单栏目中点击Build,再选择Build Artifacts,最后点击build,在项目的target目录下就会生成对应的war包。

在这里插入图片描述在这里插入图片描述

最后一步

将生成的war包放到tomcat的webapps目录下,通过ip/项目名访问即可。

注意

当以war包形式打包部署时,使用的是外部Tomcat,这个时候application.yml配置的端口将失效。

转载地址:http://otmg.baihongyu.com/

你可能感兴趣的文章
nginx connect 模块安装以及配置
查看>>
nginx css,js合并插件,淘宝nginx合并js,css插件
查看>>
Nginx gateway集群和动态网关
查看>>
nginx http配置说明,逐渐完善。
查看>>
Nginx keepalived一主一从高可用,手把手带你一步一步配置!
查看>>
Nginx Location配置总结
查看>>
Nginx log文件写入失败?log文件权限设置问题
查看>>
Nginx Lua install
查看>>
nginx net::ERR_ABORTED 403 (Forbidden)
查看>>
vue中处理过内存泄露处理方法
查看>>
Nginx RTMP 模块使用指南
查看>>
Nginx SSL 性能调优
查看>>
nginx ssl域名配置
查看>>
Nginx SSL私有证书自签,且反代80端口
查看>>
Nginx upstream性能优化
查看>>
Nginx 中解决跨域问题
查看>>
nginx 代理解决跨域
查看>>
Nginx 做负载均衡的几种轮询策略分析
查看>>
Nginx 入门,一篇搞定!
查看>>
Nginx 利用代理转发请求示例
查看>>