如何解决spring-boot-starter-parent与依赖中Flyway的包冲突问题
可行解决方案
方案1:通过重写Spring Boot版本属性统一管控(改造成本最低)
Spring Boot父POM中所有依赖版本都通过properties统一管理,Flyway对应的版本属性为flyway.version。你可以直接将公共库作为所有业务应用的中间父POM:
- 公共库POM先继承
spring-boot-starter-parent,在公共库的properties节点中新增<flyway.version>你需要的目标版本号</flyway.version>,覆盖父POM的默认版本 - 所有业务应用的POM不再直接继承Spring Boot父POM,改为继承你的公共库POM
该方案下业务应用无需做任何额外配置,Flyway版本完全由公共库统一维护。
方案2:通过BOM导入覆盖版本(不需要调整父POM继承关系)
如果业务应用不能修改父POM继承结构,可以用依赖导入的方式调整优先级:
- 在公共库的POM中新增
dependencyManagement节点,声明你需要的Flyway版本:
<dependencyManagement> <dependencies> <dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-core</artifactId> <version>目标Flyway版本号</version> </dependency> </dependencies> </dependencyManagement>
- 业务应用在自己POM的
dependencyManagement节点中,优先导入公共库的POM,优先级高于Spring Boot父POM的默认声明:
<dependencyManagement> <dependencies> <dependency> <groupId>公共库的groupId</groupId> <artifactId>公共库的artifactId</artifactId> <version>公共库版本号</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
Maven会优先使用先声明的dependencyManagement中的版本,直接覆盖Spring Boot父POM的低版本Flyway声明,后续你仅需更新公共库中的版本即可同步到所有应用。
方案3:公共库打包隔离Flyway(业务应用零改造)
如果不想让业务应用做任何POM调整,可以用maven-shade-plugin在公共库打包时把Flyway依赖打包进公共库,同时重命名Flyway的包路径,避免和应用的依赖冲突:
在公共库的POM中添加shade插件配置,指定对Flyway包进行重定向:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.4.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <relocations> <relocation> <pattern>org.flywaydb</pattern> <shadedPattern>你的内部包路径.shaded.org.flywaydb</shadedPattern> </relocation> </relocations> </configuration> </execution> </executions> </plugin>
该方案下公共库内部调用的是重命名后的隔离版Flyway,完全不受业务应用依赖的版本影响,业务应用不需要做任何改动。
内容的提问来源于stack exchange,提问作者Spen1210
相关产品推荐
相关产品推荐

