2016-11-16 78 views
0

我已經多模塊結構Maven項目如下:如何聲明全局依賴與Maven的所有子模塊和eclipse

    • 服務器
    • 共享
    • 客戶

對我來說,顯然我會爲所有這些項目編寫單元測試。因此,我認爲,將足夠多的增加依賴於母公司的pom.xml如下:

<dependency> 
    <groupId>junit</groupId> 
    <artifactId>junit</artifactId> 
    <version>4.12</version> 
    <scope>test</scope> 
</dependency> 

,然後在每個子模塊使用它,FE:

import static org.junit.Assert.assertEquals; 
import static org.junit.Assert.assertFalse; 
import static org.junit.Assert.assertNotNull; 
import static org.junit.Assert.assertTrue; 

但我得到的錯誤在Eclipse中像:The import org.junit cannot be resolved

當我執行Maven-> Update項目或mvn clean install時,根本沒有錯誤。

我指的是父模塊中的孩子一個如下:

<parent> 
    <groupId>pl.daniel.erp</groupId> 
    <artifactId>erp</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
</parent> 

請幫

+0

你聲明的孩子勁歌父? – Compass

+0

我已更新後檢查它。 thx – masterdany88

+0

嗯。我會通過:http:// stackoverflow。com/questions/3211643/maven2-sharing-dependencies-across-parent-and-children-without-redeclaring-depe如果這不是問題,請嘗試從Eclipse完全清理構建。可能是一個愚蠢的配置問題或Eclipse的行爲,需要一個完整的清潔。 – Compass

回答

1

AJNeufeld:

「但你仍必須指定的子模塊的依賴關係」

只有在依賴關係處於<dependencyManagement>時,纔是正確的。試着在父代<依賴關係>中設置<依賴關係>,並且不要在子代中設置相同的依賴關係。

父POM:

<project> 
... 
<dependencies> 
    ... 
    <dependency> 
    <groupId>junit</groupId> 
    <artifactId>junit</artifactId> 
    <version>4.12</version> 
    <scope>test</scope> 
    </dependency> 
    ... 
<dependencies> 
... 
</project> 

兒童POM:

<project> 
    ... 
    <!-- no junit dependency defined at all --> 
    ... 
</project> 
+0

那麼''和''標籤之間有什麼區別?爲什麼都存在? – masterdany88

+0

是一種「抽象」聲明(與抽象類相同)任何地方在父項目樹中,它可以聲明通用設置(即版本,範圍等),但只有當定義了依賴時纔會添加到類路徑中在<依賴關係>中。當它處於dependencyManagement中時,實際的依賴關係只需要具有groupId/artifactId。因此,有很多分層設置的空間... 中的也是相同的。而插件則更有用,因爲它們可能有很大的特定配置。 – Vadim

+0

所以......「grand-parent」可以有常用設置,那麼它的孩子可能有實際的具有特定的設置,所有他們的「孩子」根本不需要定義插件......類似的東西。實際上我有超過100個這樣的項目。而所有的依賴版本和插件只在一個地方定義。版本升級很容易管理。 – Vadim

0

您可以在父配置的依存關係,但仍然必須指定的子模塊的依賴關係。 這允許您在父POM中指定版本一次,並讓所有子模塊使用該版本。 但是,每個子模塊仍然必須列出其依賴關係。

父POM:

<project> 
    ... 
    <dependencyManagement> 
     <dependencies> 
      <dependency> 
       <groupId>junit</groupId> 
       <artifactId>junit</artifactId> 
       <version>4.12</version> 
       <scope>test</scope> 
      </dependency> 
     ... 
     <dependencies> 
    </dependencyManagement> 
    ... 
</project> 

兒童的POM:

<project> 
    ... 
    <dependencies> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
     </dependency> 
     ... 
    </dependencies> 
    ... 
</project>