maven pom類型,有import和沒有import的區別

問題

在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”?

發佈了339 篇原創文章 · 獲贊 370 · 訪問量 193萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章