简体   繁体   English

如何使用JaCoCo或任何其他工具在Java项目中生成涵盖Maven依赖关系的报告?

[英]How to generate reports covering Maven dependencies in a Java project using JaCoCo or any other tool?

My project uses two other libraries that are added to it as Maven dependencies. 我的项目使用了另外两个作为Maven依赖项添加到它的库。 When I generate a coverage report for the project using the command prompt, it generates the jacoco.exec file and I can use it to see the coverage report in the IDE. 当我使用命令提示符生成项目的覆盖率报告时,它会生成jacoco.exec文件,并且可以使用它在IDE中查看覆盖率报告。

But it doesn't give me coverage over the dependencies used. 但这并不能覆盖所使用的依赖项。

I have seen in the Eclipse plugin for JaCoCo where it gives the opportunity to configure coverage and select specific jars being used in the project to be included in the coverage report. 我在JaCoCo的Eclipse插件中看到过,它提供了配置覆盖率并选择项目中使用的特定jar的机会,以将其包含在覆盖率报告中。

How can I to the same in the case of command line based execution? 在基于命令行的执行情况下,我该怎么办?

My project use two other libraries which are added to it as a maven dependency. 我的项目使用了另外两个作为maven依赖项添加到它的库。

If those libraries are other modules of the same Maven build, then please have a look at report-aggregate goal of JaCoCo Maven Plugin. 如果这些库是同一Maven构建的其他模块,则请查看JaCoCo Maven插件的report-aggregate目标

I have seen in eclipse plugin for JaCoCo where it gives opportunity to configure coverage and select specific jars being used in the project to be included in coverage report. 我在JaCoCo的eclipse插件中看到过,它提供了配置覆盖率并选择项目中使用的特定jar的机会,以将其包含在覆盖率报告中。 But how to do same in the case of command line based execution? 但是在基于命令行的执行情况下该如何做呢?

In JaCoCo 0.7.9 there are JaCoCo Ant Tasks , and in 0.8.0 there will be JaCoCo Command Line Interface - both provide more freedom than JaCoCo Maven Plugin in specification of what should be included into report, in particular can be used for arbitrary (eg third-party) JAR files. 在JaCoCo 0.7.9中,存在JaCoCo Ant任务 ;在0.8.0中,将具有JaCoCo命令行界面 -两者都比JaCoCo Maven插件在应包括在报告中的规范方面提供了更大的自由度,尤其是可用于任意(例如第三方)JAR文件。 Ant Tasks can be executed from within Maven build using maven-antrun-plugin . 可以使用maven-antrun-plugin从Maven内部执行Ant Tasks。

And there is JaCoCo APIs with examples (in particular ReportGenerator.java ) that can be used to build you own report generators, other integrations. 还有带有示例的 JaCoCo API (特别是ReportGenerator.java ),可用于构建您自己的报告生成器以及其他集成。 Mentioned by you Eclipse plugin that is based on JaCoCo - is Eclipse EclEmma . 您提到的基于JaCoCo的Eclipse插件是Eclipse EclEmma And it uses exactly these APIs for integration into Eclipse IDE. 而且它完全使用这些API集成到Eclipse IDE中。

Use Codehaus plugin for better result which generates class wise coverage report. 使用Codehaus插件可获得更好的结果,可生成按类的覆盖率报告。 you can generate html as well as xml report. 您可以生成html以及xml报告。

<project>
  ...
  <reporting>
    <plugins>
      ...
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>cobertura-maven-plugin</artifactId>
        <configuration>
          <formats>
            <format>html</format>
            <format>xml</format>
          </formats>
        </configuration>
      </plugin>
    </plugins>
  </reporting>
</project>
<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>0.5.5.201112152213</version>
  <configuration>
     <destFile>${basedir}/target/coverage-reports/jacoco-unit.exec</destFile>
     <dataFile>${basedir}/target/coverage-reports/jacoco-unit.exec</dataFile>
  </configuration>
  <executions>
     <execution>
         <excludes>
            <exclude>**/*Config.*</exclude>
            <exclude>**/*Dev.*</exclude>
     </excludes>
  </execution>
  <execution>
        <id>jacoco-site</id>
        <phase>package</phase>
        <goals>
           <goal>report</goal>
       </goals>
     </execution>
   </executions>
</plugin>

You can also provide in tag to exclude the classes for code coverage and similarly for tag for including the classes 您还可以提供in标签以排除代码覆盖的类,并类似地为tag提供包含类的标签

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM