對於有多個module的項目來說,父module的依賴引用在使用時需要注意一些地方。
dependencies-即使在子module中不寫該依賴項,那么子module仍然會從父module中繼承該依賴項(全部繼承)。
dependencyManagement里只是聲明依賴,並不實現引入,因此子module需要顯式地聲明需要用的依賴。如果不在子module中聲明依賴,是不會從父module中繼承下來的;
只有在子module中寫了該依賴項,並且沒有指定具體版本,才會從父module中繼承該項,並且version和scope都讀取自父pom,
另外,如果子module中指定了版本號,那么會使用子module中指定的jar版本。
1 父module
父module的dependencyManagement標簽里面的內容只是用來管理版本號的,只是聲明,不會直接實現引用。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</dependencyManagement>
但是,<dependencies>標簽里的依賴是可以直接被子module繼承的,會直接引入到子module中。
<dependencies>
xxxx
</dependencies>
2 子module
子module是可以直接繼承父module中的dependencies依賴的。無法直接繼承dependencyManagement,但是可通過顯式引用某個依賴將其引入。
比如
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version> 若指定了version,則使用。否則,使用父module中的。
</dependency>