OpenClover のカバレッジ対象をカスタマイズする方法です。
- Maven 3.3+
- clover-maven-plugin 4.2.1
日本語の解説エントリが少なかったのでメモ。
対象ファイル、テストコードの除外などは、基本的には公式サイトを参照すればOKです。
但し、解析対象から除外すると Code metrics (LOC等) に影響するため注意が必要です。
Build Settings
で methodContexts
や statementContexts
を定義し、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>
下記のようなブロックコンテキスト (Java言語におけるブロック構文) は、15種類以上が事前定義されています。
- static
- constructor
- try
下記のメソッドコンテキストは、事前定義されています。
- private
- property
その他のメソッドは、methodContext
で正規表現を使用して自前で定義することが可能です。
ステートメントコンテキストは、事前定義されていません。
statementContexts
で正規表現を使用して自前で定義します。
ログ出力ステートメントなどをフィルタリングするのに使用します。