问题
在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-dependencies 中dependencyManagement的dependencies,全部引入到当前工程的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-dependencies 中dependencyManagement下的dependencies插入到当前工程的dependencyManagement中,所以不存在依赖传递。
无import
当没有<scope>import</scope>时,意思是将spring-boot-dependencies 的dependencies全部插入到当前工程的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”?
本文深入解析了Maven中import与普通pom依赖的不同之处。import仅用于dependencyManagement,将指定POM的dependencyManagement下所有依赖引入当前工程,避免依赖传递;而普通pom依赖则会直接加入dependencies,存在依赖传递。文章详细解释了两种方式的工作原理,帮助读者更好地理解并运用。
3658





