2016-09-22 1392 views
6

我正嘗試使用maven中的jacoco插件生成代碼覆蓋率報告,用於我正在處理的多模塊項目。Maven Jacoco用於多模塊項目的配置

我在構建標記中的父pom.xml中添加了以下內容。

 <plugin> 
      <groupId>org.jacoco</groupId> 
      <artifactId>jacoco-maven-plugin</artifactId> 
      <version>0.7.8-SNAPSHOT</version> 
      <configuration> 
       <output>file</output> 
       <append>true</append> 
      </configuration> 
      <executions> 
       <execution> 
        <id>jacoco-initialize</id> 
        <goals> 
         <goal>prepare-agent</goal> 
        </goals> 
       </execution> 
       <execution> 
        <id>jacoco-site</id> 
        <phase>verify</phase> 
        <goals> 
         <goal>report</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 

     <plugin> 
      <artifactId>maven-surefire-plugin</artifactId> 
      <version>2.18.1</version> 
      <configuration> 
       <argLine>${argLine}</argLine> 
      </configuration> 
     </plugin> 

在運行MVN驗證,在每個模塊產生各自的jacoco報告「項目根\模塊\目標\網站\ jacoco \」

是否有可能產生在包含每個模塊的測試覆蓋細節的項目根目錄下整合jacoco報告?

請建議合併單個模塊報告的最佳方法。

回答

5

當然是!

花了一段時間和幾個來源來煮這種模式,但運作良好。

對於多模塊Maven項目:

ROOT 
|--LIB-1 
|--LIB-2 

LIB項目都有自己的單元測試。

ROOT pom.xml

<!- properties--> 
<jacoco.reportPath>${project.basedir}/../target/jacoco.exec</jacoco.reportPath> 

<!-- build/plugins (not build/pluginManagement/plugins!) --> 
<plugin> 
    <groupId>org.jacoco</groupId> 
    <artifactId>jacoco-maven-plugin</artifactId> 
    <version>0.7.6.201602180812</version> 
    <executions> 
     <execution> 
      <id>agent-for-ut</id> 
      <goals> 
       <goal>prepare-agent</goal> 
      </goals> 
      <configuration> 
       <append>true</append> 
       <destFile>${jacoco.reportPath}</destFile> 
      </configuration> 
     </execution> 
    </executions> 
</plugin> 

LIB項目pom.xml將繼承的JaCoCo插件執行,所以只需要接線了argline在神火插件。

<plugin> 
    <artifactId>maven-surefire-plugin</artifactId> 
    <version>2.18.1</version> 
    <configuration> 
     <argLine>${argLine}</argLine> 
    </configuration> 
</plugin> 

我必須通過聲納被報道捲起集成測試以及單元測試JaCoCo擴展的回答,您可以看到my detailed answer here

+0

謝謝....我按照你的詳細答案中提到的步驟..最後它工作:) – John

0

除了markdsievers詳細解答提示的步驟,我不得不設置sonarqube-5.3(支持JDK 7+)在本地主機:9000

Setup SonarQube

並使用MVN包產生jacoco .exec文件。 然後mvn聲納:聲納在聲納儀表板中生成報告。