2011-01-24 51 views
13

我在多模塊項目中使用test-jar依賴關係時遇到問題。例如,當我宣佈cleartk-syntax模塊依賴於cleartk-token模塊的test-jar像這樣(完整代碼here):爲什麼「mvn compile」需要「test-jar」依賴關係

[INFO] ------------------------------------------------------------------------ 
[ERROR] BUILD ERROR 
[INFO] ------------------------------------------------------------------------ 
[INFO] Failed to resolve artifact. 

Missing: 
---------- 
1) org.cleartk:cleartk-token:test-jar:tests:0.7.0-SNAPSHOT 

<modelVersion>4.0.0</modelVersion> 
<groupId>org.cleartk</groupId> 
<artifactId>cleartk-syntax</artifactId> 
<version>0.5.0-SNAPSHOT</version> 
<name>cleartk-syntax</name> 
... 
<dependencies> 
    ... 
    <dependency> 
     <groupId>org.cleartk</groupId> 
     <artifactId>cleartk-token</artifactId> 
     <version>0.7.0-SNAPSHOT</version> 
     <type>test-jar</type> 
     <scope>test</scope> 
    </dependency> 

我碰到下面的錯誤,如果我使用maven 2運行mvn compile

如果我用maven 3我得到的錯誤:

[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD FAILURE 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 4.654s 
[INFO] Finished at: Mon Jan 24 21:19:17 CET 2011 
[INFO] Final Memory: 16M/81M 
[INFO] ------------------------------------------------------------------------ 
[ERROR] Failed to execute goal on project cleartk-syntax: Could not resolve 
dependencies for project org.cleartk:cleartk-syntax:jar:0.5.0-SNAPSHOT: Could 
not find artifact org.cleartk:cleartk-token:jar:tests:0.7.0-SNAPSHOT 

在後一種情況下,我看齊特別困惑,因爲我原以爲它應該尋找test-jar類型的工件,而不是jar類型。

使用maven 2或maven 3,我可以通過運行mvn compile package -DskipTests來編譯它。使用maven 3,我也可以通過運行mvn compile test-compile來編譯它。

但爲什麼maven 2或maven 3在compile階段尋找test-jar依賴關係?它不應該等到test-compile階段才能尋找這樣的依賴關係嗎?

更新:答案是,我的編譯階段使用的maven-exec-plugin,requires dependency resolution of artifacts in scope:test。我創建了a feature request to remove the scope:test dependency

回答

8

這看起來像是一個明確的bug。

我有同樣的問題,並測試了Maven 3.0.1和3.0.2。驗證不會失敗,只有編譯步驟失敗。隨着Maven 3 mvn compile休息但mvn test-compile的作品。

似乎編譯階段正在查找reactor中的測試jar工件,然後回購,但它不應該,因爲依賴項在測試範圍內。測試範圍工件應在測試編譯期間解決,而不是編譯。

因此,我認爲這可以通過將maven-compiler-plugin的testCompile目標映射到編譯階段而不是默認的測試編譯階段來解決。

我說這個我POM,旁邊,增加了上游POM測試-JAR創作的部分:

<!-- there is a bug in maven causing it to resolve test-jar types 
     at compile time rather than test-compile. Move the compilation 
     of the test classes earlier in the build cycle --> 
    <plugin> 
    <artifactId>maven-compiler-plugin</artifactId> 
    <executions> 
     <execution> 
     <id>default-testCompile</id> 
     <phase>compile</phase> 
     <goals> 
      <goal>testCompile</goal> 
     </goals> 
     </execution> 
    </execution> 
    </plugin> 

但是,這不會工作,要麼是因爲編譯和測試之間的五個階段-compile沒有運行並設置類似測試類路徑的東西。

我猜真正的解決方法,直到這個bug被修復是使用test-compile代替compile

0

我正在使用maven2。我想答案是在Maven生命週期管理中。 默認生命週期的第一步是驗證,它'驗證項目是否正確,並且所有必要的信息都可用。 (見http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html)。

因此,maven只是盡力爲後面的執行獲取所有需要的依賴關係。

+0

但是,如果是這樣的話,爲什麼`mvn compile package`工作? – Steve 2011-01-24 22:54:25

+0

你已經提到它只在你提供-DskipTests = true變量時纔有效。這可以是內部優化maven的東西嗎? – WeMakeSoftware 2011-01-25 08:36:10

+0

它沒有使用-DskipTests。我只是補充說,因爲我只是試圖編譯,而不是測試。 – Steve 2011-01-25 14:44:58

1

所以我做了一些認真的調試,發現問題似乎是exec:java插件,test-jar依賴和mvn compile之間的交互。

總之,如果您將exec:java附加到執行階段,mvn compile開始在編譯時查找test-jar依賴關係。如果您從exec:java插件聲明中刪除<executions>元素,則mvn compile將再次正常工作。

我提交了一份bug報告爲exec:java插件在這裏,雖然我真的不能告訴的bug是否exec:javatest-jarmvn compile所以或許bug會在其他地方如果/移動,當有人數字了這一點:

http://jira.codehaus.org/browse/MEXEC-91

更新:這不是一個真正的錯誤,行家-EXEC-插件記錄爲需要測試的依賴性在這裏:

http://mojo.codehaus.org/exec-maven-plugin/java-mojo.html

這並不意味着它不會成爲一個很棒的功能。 ;-)

7

在我的情況下,根本原因是應該用作test作用域的類型爲test-jar的依賴項的模塊未包含所需的maven-jar-plugin配置。如果沒有下面的代碼片段,當您在相應的模塊上調用mvn deploy時,將不會部署測試代碼。

<build> 
<plugins> 
    <plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-jar-plugin</artifactId> 
    <executions> 
     <execution> 
     <goals> 
      <goal>test-jar</goal> 
     </goals> 
     </execution> 
    </executions> 
    </plugin> 
</plugins> 

詳情請參閱https://maven.apache.org/guides/mini/guide-attached-tests.html

相關問題