maven 中的dependencyManagement

記錄:
maven 中 dependencyManagement 的用法
在我們項目頂層的POM文件中,我們會看到dependencyManagement元素。
通過它元素來管理jar包的版本,讓子項目中引用一個依賴而不用顯示的列出版本號。
Maven會沿着父子層次向上走,直到找到一個擁有dependencyManagement元素的項目,然後它就會使用在這個dependencyManagement元素中指定的版本號。

<!-- 父pom -->
<dependencyManagement>
		<dependencies>
			<!-- SPRING begin -->
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-core</artifactId>
				<version>4.0.8.RELEASE</version>

				<exclusions>
					<exclusion>
						<groupId>commons-logging</groupId>
						<artifactId>commons-logging</artifactId>
					</exclusion>
				</exclusions>
			</dependency>
</dependencyManagement>
<!-- 子pom-->
				<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-core</artifactId>
				</dependency>
  • dependencies 即使在子項目中不寫該依賴項,那麼子項目仍然會從父項目中繼承該依賴項(全部繼承)

  • dependencyManagement 裏只是聲明依賴,並不實現引入,因此子項目需要顯示的聲明需要用的依賴。如果不在子項目中聲明依賴,是不會從父項目中繼承下來的;只有在子項目中寫了該依賴項,並且沒有指定具體版本,纔會從父項目中繼承該項,並且version和scope都讀取自父pom;另外如果子項目中指定了版本號,那麼會使用子項目中指定的jar版本。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章