maven pom类型,有<scope>import</scope>和没有<scope>import</scope>的区别

本文深入解析了Maven中import与普通pom依赖的不同之处。import仅用于dependencyManagement,将指定POM的dependencyManagement下所有依赖引入当前工程,避免依赖传递;而普通pom依赖则会直接加入dependencies,存在依赖传递。文章详细解释了两种方式的工作原理,帮助读者更好地理解并运用。

问题

在maven中,pom类型,有import与无import有什么不同?

有import

在使用springboot时,通常工程有自己的父模块,而不能继承spring-boot-starter-parent时,文档推荐配置

<dependencyManagement>
	<dependencies>
		<dependency>
			<!-- Import dependency management from Spring Boot -->
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-dependencies</artifactId>
			<version>2.1.12.RELEASE</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>

我们看到,这里多了一个<scope>import</scope>,它的意思是将spring-boot-dependenciesdependencyManagementdependencies,全部引入到当前工程的dependencyManagement中。根据maven官方文档Dependency Scope

import
This scope is only supported on a dependency of type pom in the <dependencyManagement> section. It indicates the dependency to be replaced with the effective list of dependencies in the specified POM’s
<dependencyManagement> section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.

import只能用在dependencyManagement块中,它将spring-boot-dependenciesdependencyManagement下的dependencies插入到当前工程的dependencyManagement中,所以不存在依赖传递。


无import

当没有<scope>import</scope>时,意思是将spring-boot-dependenciesdependencies全部插入到当前工程的dependencies中,并且会依赖传递。


加深理解

如果懂了上述所说,那么再看Stack Overflow的What is the difference between “pom” type dependency with scope “import” and without “import”?也就懂了。

You can only import managed dependencies. This means you can only
import other POMs into the dependencyManagement section of your
project’s POM. i.e.

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>other.pom.group.id</groupId>
            <artifactId>other-pom-artifact-id</artifactId>
            <version>SNAPSHOT</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>   
    </dependencies>
</dependencyManagement>

What then happens is that all the dependencies defined in the
dependencyManagement section of the other-pom-artifact-id are included
in your POM’s dependencyManagement section. You can then reference
these dependencies in the dependency section of your POM (and all of
its child POMs) without having to include a version etc.

However if in your POM you simply define a normal dependency to
other-pom-artifact-id then all dependencies from the dependency
section of the other-pom-artifact-id are included transitively in your
project - however the dependencies defined in the dependencyManagement
section of the other-pom-artifact-id are not included at all.

So basically the two different mechanisms are used for
importing/including the two different types of dependencies (managed
dependencies and normal dependencies).

There is a good page on the maven website, which can explain this far
better than I can, Dependency Management in Maven and it also contains
specific information on importing dependencies.

参考

import scope
What is the difference between “pom” type dependency with scope “import” and without “import”?

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值