博客
关于我
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 导入 sql 文件时 ERROR 1046 (3D000) no database selected 错误的解决
查看>>
mysql 导入导出大文件
查看>>
MySQL 导出数据
查看>>
mysql 将null转代为0
查看>>
mysql 常用
查看>>
MySQL 常用列类型
查看>>
mysql 常用命令
查看>>
Mysql 常见ALTER TABLE操作
查看>>
MySQL 常见的 9 种优化方法
查看>>
MySQL 常见的开放性问题
查看>>
Mysql 常见错误
查看>>
mysql 常见问题
查看>>
MYSQL 幻读(Phantom Problem)不可重复读
查看>>
mysql 往字段后面加字符串
查看>>
mysql 快照读 幻读_innodb当前读 与 快照读 and rr级别是否真正避免了幻读
查看>>
MySQL 快速创建千万级测试数据
查看>>
mysql 快速自增假数据, 新增假数据,mysql自增假数据
查看>>
MySql 手动执行主从备份
查看>>
Mysql 批量修改四种方式效率对比(一)
查看>>