老鬼的博客 来都来啦,那就随便看看吧~
springboot打war包
发布于: 2020-12-25 更新于: 2020-12-25 分类于:  阅读次数: 

一:介绍

1
本次主要介绍sprigboot如何打包war包部署在tomcat中。

二:调整步骤

2.1 修改pom.xml

2.2.1 修改packaging
  • 旧的
1
<packaging>jay</packaging>
  • 新的
1
<packaging>war</packaging>
2.2.2 移除内置tomcat
  • 旧的
1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
  • 新的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--打war的时候需要-->
<!--移除内置tomcat-->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--添加servlet-api的依赖,用来打war包 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>

2.2 新增WarConfig.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.tohours.iagent.config;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Configuration;

import com.tohours.iagent.IAgentApplication;

@Configuration
public class WarConfig extends SpringBootServletInitializer{
//注意,这里的IAgentApplication是启动类
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(IAgentApplication.class);
}
}

三:部署war包

3.1 打包

1
使用mvn clean package打包,将打好的war包放在tomcat/webapps目录下。

3.2 启动

1
2
3
4
5
6
7
8
9
10
11
12
默认的tomcat启动以后会报如下警告:
25-Dec-2020 14:31:07.564 警告 [main] org.apache.catalina.webresources.
Cache.getResource 无法将位于[/WEB-INF/classes/static/app/static/js/pag
es-client-clientDetail.dc7cdb47.js]的资源添加到Web应用程序[/iagent]的缓
存中,因为在清除过期缓存条目后可用空间仍不足 - 请考虑增加缓存的最大空间。

解决方案如下:

编辑tomcat/conf/context.xml加入如下代码:

<!-- 缓存最大值,大小默认是1024,单位是KB -->
<Resources cachingAllowed="true" cacheMaxSize="100000" />

四:相关截图

1.png
2.png
3.png

*************感谢您的阅读*************