配置SpringBoot方便的切换jar和war的方法示例
作者:BBFBBF 时间:2023-11-22 11:57:54
网上关于如何切换,其实说的很明确,本文主要通过profile进行快速切换已实现在不同场合下,用不同的打包方式。
jar到war修改步骤
pom文件修改
packaging配置由jar改为war
排除tomcat等容器的依赖
配置web.xml或者无web.xml打包处理
入口类修改
添加ServletInitializer
特别注意:当改成war包的时候,application.properties配置的server.port和server.servlet.context-path就无效了,遵从war容器的安排。
配置pom
配置packaging
<packaging>${pom.package}</packaging>
修改build
<!-- 作用是打war包的时候,不带版本号 -->
<finalName>${pom.packageName}</finalName>
<!--加入plugin-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<!--如果想在没有web.xml文件的情况下构建WAR,请设置为false。-->
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
排除容器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
配置profile
<profiles>
<profile>
<!-- 开发环境 -->
<id>jar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<pom.package>jar</pom.package>
<pom.packageName>${project.artifactId}-${project.version}</pom.packageName>
<pom.profiles.active>dev</pom.profiles.active>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
</dependencies>
</profile>
<profile>
<id>war</id>
<properties>
<pom.package>war</pom.package>
<pom.packageName>${project.artifactId}</pom.packageName>
<pom.profiles.active>linux</pom.profiles.active>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
</profile>
</profiles>
修改入口类
入口类继承SpringBootServletInitializer
重写configure方法
使用@Profile注解,当启用war配置的时候,初始化Servlet。
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Profile(value = {"war"})
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
来源:https://segmentfault.com/a/1190000017926997
标签:SpringBoot,切换,jar,war
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
从java中调用matlab详细介绍
2023-08-01 14:04:17
![](https://img.aspxhome.com/file/2023/0/59870_0s.jpg)
android实现打地鼠游戏
2023-09-25 08:45:59
![](https://img.aspxhome.com/file/2023/1/138381_0s.jpg)
Java Spring Dubbo三种SPI机制的区别
2022-05-04 00:29:51
Struts2实现文件上传功能实例解析
2023-11-04 04:56:13
![](https://img.aspxhome.com/file/2023/6/58836_0s.png)
Java如何获取对象属性及对应值
2022-03-30 07:03:05
Android自定义View实现钟摆效果进度条PendulumView
2022-07-04 15:50:14
![](https://img.aspxhome.com/file/2023/1/126751_0s.gif)
C# memcached缓存使用实例代码
2022-01-15 02:17:11
c++ 虚继承,多继承相关总结
2022-11-19 12:09:35
![](https://img.aspxhome.com/file/2023/0/120810_0s.png)
C#串口通信模块使用方法示例
2023-06-19 12:13:57
![](https://img.aspxhome.com/file/2023/7/66577_0s.jpg)
Android编程判断网络是否可用及调用系统设置项的方法
2023-08-14 13:05:22
Spring Boot应用Docker化的步骤详解
2023-10-12 11:47:36
Spring MVC通过添加自定义注解格式化数据的方法
2023-11-06 09:05:32
C#中char和string的入门使用教程
2022-06-14 15:07:17
![](https://img.aspxhome.com/file/2023/5/96305_0s.png)
Java线程池Executor用法详解
2022-02-13 01:21:16
![](https://img.aspxhome.com/file/2023/2/62812_0s.jpg)
SpringBoot控制配置类加载顺序方式
2022-08-18 04:31:15
![](https://img.aspxhome.com/file/2023/8/129228_0s.png)
Java中List与Map初始化的一些写法分享
2021-11-23 13:13:46
如何使用JaCoCo分析java单元测试覆盖率
2023-01-13 11:38:39
![](https://img.aspxhome.com/file/2023/1/82031_0s.png)
Android中系统自带锁WalkLock与KeyguardLock用法实例详解
2023-11-26 01:50:49
Android多功能视频播放器GSYVideoPlayer开发流程
2021-07-21 07:35:05
SpringBoot自定义starter启动器的实现思路
2023-10-09 00:55:15
![](https://img.aspxhome.com/file/2023/0/119150_0s.jpg)