簡體   English   中英

為什么“mvn編譯”需要“test-jar”依賴

[英]why is “test-jar” dependency required for “mvn compile”

我在多模塊項目中使用test-jar依賴項時遇到問題。 例如,當我聲明cleartk-syntax模塊依賴於cleartk-token模塊的test-jar (完整代碼在這里 ):

<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我會收到以下錯誤:

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

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

如果我使用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 需要在范圍內依賴解析工件:test 我已經創建了一個刪除范圍的功能請求:測試依賴項

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

<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

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

我有同樣的問題,並測試了Maven 3.0.1和3.0.2。 驗證不會失敗,只有編譯步驟失敗。 使用Maven 3 mvn compile中斷,但mvn test-compile有效。

似乎編譯階段正在尋找反應堆中的test-jar工件,然后是repo,但它不應該因為依賴項在測試范圍內。 測試范圍工件應該在測試編譯期間解決,而不是編譯。

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

我把它添加到我的pom中,緊挨着在上游pom中添加test-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>
    </executions>
  </plugin>

但這不會起作用,因為編譯和測試編譯之間的五個階段都沒有運行並設置像測試類路徑這樣的東西。

我想在修復此錯誤之前的真正解決方法是使用test-compile代替compile

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

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

我在這里為exec:java插件提交了一個錯誤報告,雖然我無法確定錯誤是否在exec:javatest-jarmvn compile所以如果/當有人認為錯誤時,錯誤可能會移動到其他地方出:

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

更新:這不是一個真正的錯誤,maven-exec-plugin被記錄為需要測試依賴項:

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

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

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

因此,maven只是盡力為后來的執行獲取所有必需的依賴項。

這對我來說是個問題: -DskipTests

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM