cobertura-maven-plugin用法

翟嘉年
2023-12-01

基本配置

<project>
  ...  
  <reporting>
    <plugins>
      ...
      <strong><plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>cobertura-maven-plugin</artifactId>
        <version>2.7</version>
      </plugin></strong>
    </plugins>
  </reporting>
</project>

配置Instrumentation

Instrumentation用于定制Cobertura的功能

<project>
  ...
  <build>
    ...
    <plugins>
      ...
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>cobertura-maven-plugin</artifactId>
        <version>2.7</version>
        <configuration>
         <strong> <instrumentation>
            <ignores>
              <ignore>com.example.boringcode.*</ignore>
            </ignores>
            <excludes>
              <exclude>com/example/dullcode/**/*.class</exclude>
              <exclude>com/example/**/*Test.class</exclude>
            </excludes>
          </instrumentation></strong>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>clean</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

配置Check

插件可以检查测试后的覆盖率,以确保一个有效的测试覆盖。

<project>
  ...
  <build>
    ...
    <plugins>
      ...
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>cobertura-maven-plugin</artifactId>
        <version>2.7</version>
        <configuration>
          <check>
            <branchRate>85</branchRate>
            <lineRate>85</lineRate>
            <haltOnFailure>true</haltOnFailure>
            <totalBranchRate>85</totalBranchRate>
            <totalLineRate>85</totalLineRate>
            <packageLineRate>85</packageLineRate>
            <packageBranchRate>85</packageBranchRate>
            <regexes>
              <regex>
                <pattern>com.example.reallyimportant.*</pattern>
                <branchRate>90</branchRate>
                <lineRate>80</lineRate>
              </regex>
              <regex>
                <pattern>com.example.boringcode.*</pattern>
                <branchRate>40</branchRate>
                <lineRate>30</lineRate>
              </regex>
            </regexes>
          </check>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>clean</goal>
              <goal>check</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

使用不同的reports

2.6版本之前只有一种report可用:cobertura,用于生成单元测试的覆盖率。因为只有一种,所以不需要进行配置。

从2.7版本开始,新增了一种新的report:cobertura-integration-test,用于生成集成测试的报告。

注意:Maven 2无法在同一次编译中运行两种reports,因此需要在用Maven 2时需要配置使用哪一种。

两种reports默认都是开启的。如果需要像旧的那样只生成单元测试的覆盖率报告,在使用2.7版本时需要像下面这样配置:

<project>
  <reporting>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>cobertura-maven-plugin</artifactId>
        <version>2.7</version>
        <reportSets>
          <reportSet>
            <reports>
              <report>cobertura</report>
            </reports>
          </reportSet>
        </reportSets>
      </plugin>
    </plugins>
  </reporting>
</project>



cobertura:cobertura

Instrument the compiled classes, run the unit tests and generate a Cobertura report.

Note:This goal should be used as a Maven report.

Full name:

org.codehaus.mojo:cobertura-maven-plugin:2.7:cobertura

Description:

Instrument the compiled classes, run the unit tests and generate aCobertura report.

Attributes:

  • Requires a Maven project to be executed.

  • Invokes the execution of the lifecycle phasetest prior to executing itself.

  • Executes in its own lifecycle: cobertura.

Required Parameters

NameTypeSinceDescription

outputDirectory

File

-

The output directory for the report.

Default value is: ${project.reporting.outputDirectory}/cobertura.


Optional Parameters

NameTypeSinceDescription

aggregate

boolean

2.5

Generate aggregate reports in multi-module projects.

Default value is: false.

User property is: cobertura.aggregate.

encoding

String

2.4

The encoding for the java source code files.

Default value is: UTF-8.

User property is: project.build.sourceEncoding.

format

String

-

The format of the report. Supports 'html' or 'xml'. Defaults to'html'.

User property is: cobertura.report.format.

formats

String[]

-

The formats of the report. Can be 'html' and/or 'xml'. Defaults to'html'.

maxmem

String

-

Maximum memory to pass to the JVM for Cobertura processes.

User property is: cobertura.maxmem.

omitGplFiles

boolean

2.5

Whether to remove GPL licensed files from the generated report.This is required to distribute the report as part of adistribution, which is licensed under the ASL, or a similarlicense, which is incompatible with the GPL.

Default value is: false.

User property is: cobertura.omitGplFiles.

quiet

boolean

2.1

Only output Cobertura errors, avoid info messages.

Default value is: false.

User property is: quiet.



http://www.mojohaus.org/cobertura-maven-plugin/

 类似资料: