博客
关于我
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锁(2):表级锁
查看>>
MySQL锁,锁的到底是什么?
查看>>
MySQL错误-this is incompatible with sql_mode=only_full_group_by完美解决方案
查看>>
Mysql错误2003 -Can't connect toMySQL server on 'localhost'(10061)解决办法
查看>>
MySQL错误提示mysql Statement violates GTID consistency
查看>>
mysql错误:This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its de
查看>>
mysql长事务
查看>>
mysql问题记录
查看>>
MySQL集群解决方案(1):MySQL数据库的集群方案
查看>>
MySQL集群解决方案(4):负载均衡
查看>>
MySQL集群解决方案(5):PXC集群
查看>>
MySQL面试宝典
查看>>
WAP短信:融合传统短信和互联网的新型通信方式
查看>>
mysql面试题学校三表查询_mysql三表查询分组后取每组最大值,mysql面试题。
查看>>
Mysql面试题精选
查看>>
MySQL面试题集锦
查看>>
mysql面试题,存储引擎InnoDB和MyISAM
查看>>
mysql面试题:为什么MySQL单表不能超过2000W条数据?
查看>>
mysql面试题:创建索引时会不会锁表?
查看>>
mysql面试题:高度为3的B+树可以存放多少数据?
查看>>