2017-05-30 55 views
0

在我的多模塊maven項目中,所有模塊都依賴於我自己的「common」模塊,例如,模塊「A」具有此依賴性「common 」。而「通用」模塊具有spring-boot-starter-test依賴關係。但是,當我在此「A」模塊中編寫單元測試時,它顯示未導入測試依賴項。然後我檢查依賴關係,發現沒有導入spring-boot-starter-test。我想問爲什麼?在我的意義上,模塊「A」參考「常見」,「常見」參考spring-boot-starter-test,所以模塊「A」應參考spring-boot-starter-test,但事實並非如此。順便說一句,儘管這個spring-boot-starter-test其他依賴通過hirachy很好地工作。有誰知道它爲什麼?先謝謝你。依賴項spring-boot-starter-test未在多個模塊中繼承maven項目

+0

爲什麼因爲範圍是'test',所以只包含編譯時間和非可選依賴項。 –

+0

哦......謝謝你的信息 – Yun

回答

2

最有可能在模塊「A」依賴spring-boot-starter-test有範圍test。這種範圍的相關性不是傳遞性的。請參閱依賴關係範圍部分https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html。 最好的解決方案是依賴管理。見依賴管理

簡單地說,你需要創建parrent模塊,並聲明依賴同治sectoin:

<dependencyManagement> 
    <dependencies> 
     <dependency> 
      <groupId>com.example</groupId> 
      <artifactId>A</artifactId> 
      <version>1.0</version> 
      <scope>test</scope> 
     </dependency> 
    </dependencies> 
</dependencyManagement> 

然後繼承父的模塊和剛剛宣佈的依賴,而不versionscope

<dependencies> 
     <dependency> 
      <groupId>com.example</groupId> 
      <artifactId>A</artifactId> 
     </dependency> 
    </dependencies> 
+0

謝謝。這似乎是我沒有足夠的maven知識 – Yun

相關問題