需求
每次修改一小部分代码或仅仅更新某个依赖jar包时,都需要重新进行整个项目的构建、打包、上传和部署 。
虽然传输jar包 比较大,但是安全性、稳定性比较高,不需要关注pom.xml 添加了新的依赖、更新了版本号等等影响版本功能的操作。
但是当你的项目趋于稳定,只有业务上的逻辑变更时,如果使用分离版本,可以加快迭代、更新的速度。
解决方案
为了考虑分离版本和整体版本可以同时切换,期望可以实现:
- 自主选择是否分离版本
- 切换某个版本,行为简单
方案一:
准备2个pom文件,通过打包时指定文件操作即可
- mvn install -f pom-oneJar.xml 即打包为整体版本
- mvn install -f pom-split.xml 即打包为分离版本
优点: 相互分离,配置之间无影响。
弊端: 需要配置IDE命令,修改依赖时需要同时修改2处。
方案二:
关注maven的特性, profiles即可满足, 根据不同的activation 状态,激活不同的build操作 。
- 切换build-for-oneJar的activeByDefault =true,build-for-split的activeByDefault =false 即打包为整体版本
- 切换build-for-oneJar的activeByDefault =false,build-for-split的activeByDefault =true 即打包为分离版本
优点: 相互分离,配置之间无影响 。
弊端:需要修改2处 。
<profiles>
<!-- 整体版本-->
<profile>
<id>build-for-oneJar</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
.....
</build>
</profile>
<!-- 分离版本-->
<profile>
<id>build-for-split</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
......
</build>
</profile>
</profiles>
方案二的pom.xml参考
注意:mainClass 运行类路径修改
<profiles>
<!-- 整体版本-->
<profile>
<id>build-for-oneJar</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<!-- 分离版本-->
<profile>
<id>build-for-split</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<!--不打入jar包的文件类型或者路径-->
<excludes>
<exclude>*.properties</exclude>
<exclude>*.yml</exclude>
<exclude>*.yaml</exclude>
</excludes>
<archive>
<manifest>
<!-- 执行的主程序路径 -->
<mainClass>cn.note.spring.thread.ThreadApplication</mainClass>
<!--是否要把第三方jar放到manifest的classpath中-->
<addClasspath>true</addClasspath>
<!--生成的manifest中classpath的前缀,因为要把第三方jar放到lib目录下,所以classpath的前缀是lib/-->
<classpathPrefix>lib/</classpathPrefix>
<!-- 打包时 MANIFEST.MF 文件不记录的时间戳版本 -->
<useUniqueVersions>false</useUniqueVersions>
</manifest>
<manifestEntries>
<!-- 在 Class-Path 下添加配置文件的路径 -->
<Class-Path>config/</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<resources>
<!--把配置文件打包到指定路径-->
<resource>
<directory>src/main/resources/</directory>
<includes>
<include>*.properties</include>
<include>*.yml</include>
<exclude>*.yaml</exclude>
</includes>
</resource>
</resources>
<outputDirectory>${project.build.directory}/config</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>