2010-09-03 67 views
4

我有一個父項目,其中有5個子項也具有彼此之間的依賴關係。我在子節點pom.xml中使用了<parent>元素,並在父節點中使用了與<module>元素的聚合。當使用Maven構建子項目時缺少工件2

我父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> 
<groupId>com.domain</groupId> 
<artifactId>Parent</artifactId> 
<packaging>pom</packaging> 
<version>RELEASE</version> 
<name>Parent</name> 

<modules> 
    <module>../Child1</module> 
    <module>../Child2</module> 
    <module>../Child3</module> 
    <module>../Child4</module> 
    <module>../Child5</module> 
</modules> 

<dependencyManagement> 
    <dependencies> 
     <dependency> 
      <groupId>com.domain</groupId> 
      <artifactId>Child1</artifactId> 
      <version>RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>com.domain</groupId> 
      <artifactId>Child2</artifactId> 
      <version>RELEASE</version> 
     </dependency> 
    </dependencies> 
</dependencyManagement> 
</project> 

Child3 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> 
<groupId>com.domain</groupId> 
<artifactId>Child3</artifactId> 
<name>Child3</name> 
<packaging>war</packaging> 

<parent> 
    <artifactId>Parent</artifactId> 
    <groupId>com.domain</groupId> 
    <version>RELEASE</version> 
</parent> 

<dependencies> 
    <dependency> 
     <groupId>com.domain</groupId> 
     <artifactId>Child1</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>com.domain</groupId> 
     <artifactId>Child2</artifactId> 
    </dependency> 
</dependencies> 
</project> 

一切工作正常,當我在家長或Child1運行mvn install。但是,當我在Child3運行它,我得到以下錯誤:

[INFO] Failed to resolve artifact. 
Missing: 
---------- 
1) com.domain:Child1:jar:RELEASE 
... 
2) com.domain:Child2:jar:RELEASE 

這有什麼錯我的建立?

回答

6

我不會真正嘗試解決您當前的方法問題,而是會涵蓋我認爲是最佳實踐的情況。你可以隨意採用與否:)

首先,請注意RELEASE(和LATEST)是一個特殊的關鍵字(不知道你是意識到了這一點),並RELEASE以某種方式在這裏被誤用(家長用定義爲RELEASE的版本沒有意義)。無論如何,這些特殊的關鍵字是一個壞主意,爲了構建可重複性,我們不推薦使用這些關鍵字(我不確定它們是否在Maven3中受支持),只是爲了避免使用它們。

因此,使用快照版本,如果該項目正在積極開發中,而不是即修改這樣的家長:

<project> 

    <modelVersion>4.0.0</modelVersion> 

    <groupId>com.domain</groupId> 
    <artifactId>Parent</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    <packaging>pom</packaging> 

    <name>Parent</name> 

    <modules> 
    <module>../Child1</module> 
    <module>../Child2</module> 
    <module>../Child3</module> 
    <module>../Child4</module> 
    <module>../Child5</module> 
    </modules> 

</project> 

注意,我刪除了dependencyManagement元素,我不認爲它提供了很多增值一個多模塊構建的內部依賴和推薦使用的${project.groupId}${project.version}內置屬性聲明,而不是當他們:

<project> 
    <modelVersion>4.0.0</modelVersion> 

    <parent> 
    <artifactId>Parent</artifactId> 
    <groupId>com.domain</groupId> 
    <version>1.0-SNAPSHOT</version><!-- must hard code this --> 
    </parent> 

    <!--groupId>com.domain</groupId--><!-- you can skip this, you inherit it --> 
    <artifactId>Child3</artifactId> 
    <packaging>war</packaging> 

    <name>Child3</name> 

    <dependencies> 
    <dependency> 
     <groupId>${project.groupId}</groupId> 
     <artifactId>Child1</artifactId> 
     <version>${project.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>${project.groupId}</groupId> 
     <artifactId>Child2</artifactId> 
     <version>${project.version}</version> 
    </dependency> 
    </dependencies> 
</project> 

正如我寫的,我不認爲使用dependencyManagement內部依賴關係非常有用,這就是我設置我的項目的方式。但你可以,如果你想。只需使用屬性即可不重複信息。

+0

問題確實來自使用RELEASE作爲版本號。 Merci Pascal。 – Damien 2010-09-07 15:25:36