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

你可能感兴趣的文章
Mysql5.7版本单机版my.cnf配置文件
查看>>
mysql5.7的安装和Navicat的安装
查看>>
mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
查看>>
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>
MYSQL8.0以上忘记root密码
查看>>
Mysql8.0以上重置初始密码的方法
查看>>
mysql8.0新特性-自增变量的持久化
查看>>
Mysql8.0注意url变更写法
查看>>
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>
Mysql8在Centos上安装后忘记root密码如何重新设置
查看>>
Mysql8在Windows上离线安装时忘记root密码
查看>>
MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
查看>>
mysql8的安装与卸载
查看>>
MySQL8,体验不一样的安装方式!
查看>>
MySQL: Host '127.0.0.1' is not allowed to connect to this MySQL server
查看>>
Mysql: 对换(替换)两条记录的同一个字段值
查看>>