一文带你了解maven中的dependencyManagement

作用

maven中dependencyManagement常用于解决子项目按需加载问题。

案列

1.test-parent项目,包含模块utils、common、auth,通常写法结构如下:

<project>
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.xxx.test</groupId>
	<artifactId>test-parent</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.3.RELEASE</version>
	</parent>
        <dependencies>
		<dependency>
			<groupId>com.xxx.test</groupId>
			<artifactId>test-utils</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		<dependency>
			<groupId>com.xxx.test</groupId>
			<artifactId>test-auth</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		<dependency>
			<groupId>com.xxx.test</groupId>
			<artifactId>test-common</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
	</dependencies>
</project>

2.假设b项目将test-parent作为父项目,b将加载test-parent中的utils、common、auth模块

<project>
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.xxx.test</groupId>
	<artifactId>test-b</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
        <!--父项目test-parent-->
	<parent>
		<groupId>com.xxx.test</groupId>
		<artifactId>test-parent</artifactId>
		<version>2.1.3.RELEASE</version>
	</parent>
        <dependencies>
		其它依赖
	</dependencies>
</project>

3.此时c项目将test-parent作为父项目,只需加载common模块,那么1这种写法显然不可行,因为我们只需要加载parent中的common模块,此时dependencyManagement的作用就体现出来了,改写写法1如下:

<project>
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.xxx.test</groupId>
	<artifactId>test-parent</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.3.RELEASE</version>
	</parent>
       <dependencyManagement>
            <dependencies>
		    <dependency>
			    <groupId>com.xxx.test</groupId>
			    <artifactId>test-utils</artifactId>
			    <version>0.0.1-SNAPSHOT</version>
		    </dependency>
		    <dependency>
			    <groupId>com.xxx.test</groupId>
			    <artifactId>test-auth</artifactId>
			    <version>0.0.1-SNAPSHOT</version>
		    </dependency>
		    <dependency>
			    <groupId>com.xxx.test</groupId>
			    <artifactId>test-common</artifactId>
			    <version>0.0.1-SNAPSHOT</version>
		    </dependency>
	    </dependencies>
       </dependencyManagement>
</project>

4.c项目中写法如下,只需要引入相应的模块,版本号会根据parent中指定的版本号加载。

<project>
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.xxx.test</groupId>
	<artifactId>test-b</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
        <!--父项目test-parent-->
	<parent>
		<groupId>com.xxx.test</groupId>
		<artifactId>test-parent</artifactId>
		<version>2.1.3.RELEASE</version>
	</parent>
    
        <dependency>
		<groupId>com.xxx.test</groupId>
		<artifactId>test-common</artifactId>
	</dependency>
</project>

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值