父项目管理jar包版本的情况下,子项目如何使用自己的jar版本

背景

项目使用的是boot 2.2.0 版本,但是子模块A用于加密的jasypt只支持2.1.0。如何在不改变整体boot版本的情况下,让A单独使用2.1.0版本?

尝试子项目中直接修改boot版本

<dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

spring-boot-dependencies 中管理了boot很多相关依赖的版本号,即使如下图般修改了子模块的boot 版本号,还可以看到boot下面的依赖版本依旧是2.2.0:

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
			<version>2.1.0.RELEASE</version>
		</dependency>

在这里插入图片描述
这就是告诉我们子项目的boot 下面的jar 包仍然受父项目的管理,我们这样改只能改变boot自身的版本。这也是我有些不明白的地方,按理说应该层次分明,管理分明。子项目管理自己的,父项目管理子项目。

使用dependencyManagement

经过我不懈地百度,看到一段话:

“使用dependencyManagement是为了统一管理版本信息
在子工程中使用时,还是需要引入坐标的,但是不需要给出version
在我们项目顶层的POM文件中,dependencyManagement元素。
通过它元素来管理jar包的版本,
让子项目中引用一个依赖而不用显示的列出版本号。
Maven会沿着父子层次向上找,
直到找到一个拥有dependencyManagement元素的项目,
然后它就会使用在这个dependencyManagement元素中指定的版本号。”

猜测:那么,如果我在子项目中建立一个dependencyManagement,指定boot 版本为2.1 ,这样子项目中其它的jar包就也会是2.1了。

实践:

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.1.0.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

在这里插入图片描述
看到了吗?子项目中的都变成了2.1的版本。大功告成!

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Spring Cloud 项目中,可以通过 Maven 的 Dependency Management 和 Spring 的 Environment 来实现在不同现场中使用不同版本依赖。 1. Maven 的 Dependency Management 在 pom.xml 中,可以使用 Maven 的 Dependency Management 来统一管理依赖版本。例如,定义一个项目,在项目中定义 Dependency Management,然后在子项目中引入项目,如下所示: ``` <!-- 项目 pom.xml --> <dependencyManagement> <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>example-dependencies</artifactId> <version>1.0.0</version> <scope>import</scope> <type>pom</type> </dependency> </dependencies> </dependencyManagement> <!-- 子项目 pom.xml --> <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>example-dependency</artifactId> <version>${example.version}</version> </dependency> </dependencies> ``` 在不同现场中,可以通过修改项目中的 Dependency Management 来指定不同的依赖版本。例如,在现场A中使用版本为 1.0.0 的依赖,在现场B中使用版本为 2.0.0 的依赖。 2. Spring 的 Environment 在 Spring Boot 项目中,可以使用 Spring 的 Environment 来指定不同的依赖版本。例如,在 application.yml 文件中,可以定义不同的 Profile,然后在不同的 Profile 中指定不同的依赖版本,如下所示: ``` spring: profiles: active: prod --- spring: profiles: prod properties: example.version: 1.0.0 --- spring: profiles: test properties: example.version: 2.0.0 ``` 在代码中,可以使用 `@Value` 注解来获取指定的依赖版本,如下所示: ``` @Service public class ExampleServiceImpl implements ExampleService { @Value("${example.version}") private String exampleVersion; // ... } ``` 这样,在不同的环境中启动应用程序时,Spring 会根据当前的 Profile 来选择合适的依赖版本

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值