博客
关于我
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/

你可能感兴趣的文章
mysql索引创建和使用注意事项
查看>>
MySQL索引原理以及查询优化
查看>>
Mysql索引合并(index merge)导致的死锁问题
查看>>
mysql索引底层数据结构和算法
查看>>
Mysql索引底层结构的分析
查看>>
MySQL索引底层:B+树详解
查看>>
Mysql索引总结
查看>>
mysql索引最左匹配原则理解以及常见的sql使用的索引情况的实测
查看>>
Mysql索引类型
查看>>
MySQL索引背后的数据结构及算法原理
查看>>
mysql索引能重复吗_mysql “索引”能重复吗?“唯一索引”与“索引”区别是什么?...
查看>>
Mysql索引(2):索引结构
查看>>
Mysql索引(3):索引分类
查看>>
Mysql索引(4):索引语法
查看>>
mysql级联删除_Mysql笔记系列,DQL基础复习,Mysql的约束与范式
查看>>
mysql经常使用命令
查看>>
MySQL经常使用技巧
查看>>
mysql给账号授权相关功能 | 表、视图等
查看>>
MySQL缓存使用率超过80%的解决方法
查看>>
Mysql缓存调优的基本知识(附Demo)
查看>>