Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save minazou67/58dff8a2f8f61ca25d057c2a205688ac to your computer and use it in GitHub Desktop.
Save minazou67/58dff8a2f8f61ca25d057c2a205688ac to your computer and use it in GitHub Desktop.
How to customize coverage targets of OpenClover

How to customize coverage targets of OpenClover

OpenClover のカバレッジ対象をカスタマイズする方法です。

Environment

  • Maven 3.3+
  • clover-maven-plugin 4.2.1

Introduction

日本語の解説エントリが少なかったのでメモ。
対象ファイル、テストコードの除外などは、基本的には公式サイトを参照すればOKです。
但し、解析対象から除外すると Code metrics (LOC等) に影響するため注意が必要です。

How to

Build SettingsmethodContextsstatementContexts を定義し、reporting でフィルタリングします。

  <build>
    <plugins>
      <plugin>
        <groupId>org.openclover</groupId>
        <artifactId>clover-maven-plugin</artifactId>
        <version>${clover.version}</version>
        <configuration>
          <methodContexts>
            <toString>(.* )?public String toString\(\).*</toString>
          </methodContexts>
          <statementContexts>
            <log>LOGGER\.(fine|finer|finest)\(.*\);</log>
          </statementContexts>
        </configuration>
        <executions>
          <execution>
            <id>main</id>
            <phase>verify</phase>
            <goals>
              <goal>instrument</goal>
              <goal>aggregate</goal>
            </goals>
          </execution>
          <execution>
            <id>site</id>
            <phase>pre-site</phase>
            <goals>
              <goal>instrument</goal>
              <goal>aggregate</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  <reporting>
    <plugins>
      <plugin>
        <groupId>org.openclover</groupId>
        <artifactId>clover-maven-plugin</artifactId>
        <version>${clover.version}</version>
        <configuration>
          <contextFilters>static,toString,log</contextFilters>
        </configuration>
      </plugin>
    </plugins>
  </reporting>

Tips

Block Contexts

下記のようなブロックコンテキスト (Java言語におけるブロック構文) は、15種類以上が事前定義されています。

  • static
  • constructor
  • try

Method Contexts

下記のメソッドコンテキストは、事前定義されています。

  • private
  • property

その他のメソッドは、methodContext で正規表現を使用して自前で定義することが可能です。

Statement Contexts

ステートメントコンテキストは、事前定義されていません。
statementContexts で正規表現を使用して自前で定義します。
ログ出力ステートメントなどをフィルタリングするのに使用します。

Reference information

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment