2014-09-05 229 views
1

這是一個父POM文件的一部分:爲什麼在子POM中不需要版本號?

<properties> 
    <hibernate.annotations.version>3.3.0.ga</hibernate.annotations.version> 
    <hsqldb.version>1.8.0.7</hsqldb.version> 
</properties> 
<dependencyManagement> 
    <dependencies> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring</artifactId> 
      <version>2.0.7</version> 
     </dependency> 

      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-annotations</artifactId> 
      <version>${hibernate.annotations.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-commons-annotations</artifactId> 
      <version>${hibernate.annotations.version}</version> 
     </dependency> 

    </dependencies> 
</dependencyManagement> 

兒童POM,我有兩個問題: 1)爲什麼是沒有必要指定休眠的版本號? 2)另外,由於hibernate已經在父POM中指定爲依賴項,爲什麼有必要將它包含在子POM中?

感謝您的解釋。

<project xmlns="http://maven.apache.org/POM/4.0.0" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
          http://maven.apache.org/maven-v4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <parent> 
     <groupId>org.sonatype.mavenbook.optimize</groupId> 
     <artifactId>simple-parent</artifactId> 
     <version>1.0</version> 
    </parent> 
    <artifactId>simple-model</artifactId> 
    <packaging>jar</packaging> 

    <name>Chapter 8 Simple Object Model</name> 

    <dependencies> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-annotations</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate</artifactId> 
     </dependency> 

    </dependencies> 
</project> 
+2

這兩個問題都可以通過對'xxxManagement'進行一些研究來回答。這就是它的工作原理。父母管理有關依賴關係的詳細信息,爲孩子提供**選項**以使用該依賴關係。如果孩子不需要它,那麼它沒有在它的pom中指定它。如果確實需要它,那麼它指定它。細節由父母照顧。 – 2014-09-05 04:23:38

+0

查看[依賴機制介紹](http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html)。顯示一些信息用例 – 2014-09-05 05:04:24

回答

0

對此的簡單回答是導致類似於groupId,artifactId和版本的信息將被孩子繼承。 所以通常只需要覆蓋artifactId

第二個答案是簡單的原因,通過dependencyManagement你定義了什麼可以在你的孩子使用,但沒有真正使用它。如果你在依賴塊中取消一個依賴,它將被真正使用。

當然,我可以推薦閱讀documentation about dependencies我也可以推薦閱讀information about inheritance