一文带你了解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>

 

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页