plugins {
id 'jacoco'
}
subprojects {
apply plugin: 'java'
apply plugin: 'jacoco'
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.10.0'
}
test {
useJUnitPlatform()
finalizedBy jacocoTestReport
}
jacoco {
toolVersion = "0.8.11"
}
jacocoTestReport {
dependsOn test
reports {
html.required = true
xml.required = true
csv.required = false
html.outputLocation = file("${rootProject.buildDir}/reports/jacoco/${project.name}")
xml.outputLocation = file("${rootProject.buildDir}/reports/jacoco/${project.name}/report.xml")
}
}
jacocoTestCoverageVerification {
violationRules {
rule {
limit {
minimum = 0.8
}
}
}
}
check.dependsOn jacocoTestCoverageVerification
}
実行
./gradlew test jacocoTestReport
サブプロジェクトをまたいで共通部品が使用されている場合
今回の場合はhoge,fugaがpiyoを呼び出している
task jacocoMergeAll(type: JacocoMerge) {
executionData fileTree(dir: '.', includes: [
'hoge/build/jacoco/*.exec',
'fuga/build/jacoco/*.exec'
])
destinationFile = file("$buildDir/jacoco/merged.exec")
}
task jacocoPiyoAggregatedReport(type: JacocoReport, dependsOn: jacocoMergeAll) {
executionData file("$buildDir/jacoco/merged.exec")
classDirectories.from = fileTree(dir: "piyo/build/classes/java/main") {
include '**/*.class'
}
sourceDirectories.from = files("piyo/src/main/java")
reports {
html.required = true
xml.required = true
html.outputLocation = file("$buildDir/reports/jacoco/piyo-aggregated")
xml.outputLocation = file("$buildDir/reports/jacoco/piyo-aggregated/report.xml")
}
}
実行
./gradlew :hoge:test :fuga:test
./gradlew jacocoPiyoAggregatedReport
※JacocoMergeをなぜか認識してくれなかったので...
task jacocoPiyoAggregatedReport(type: JacocoReport) {
dependsOn ':hoge:test', ':fuga:test'
// ✅ Hoge・Fugaの .exec ファイルを複数読み込む
executionData.from = files(
fileTree(dir: 'hoge/build/jacoco', includes: ['*.exec']),
fileTree(dir: 'fuga/build/jacoco', includes: ['*.exec'])
).filter { it.exists() && it.length() > 0 }
// ✅ Piyo のクラスファイル
classDirectories.from = files("piyo/build/classes/java/main")
.asFileTree
.matching {
include '**/*.class'
}
// ✅ Piyo のソースファイル
sourceDirectories.from = files("piyo/src/main/java")
// ✅ 出力先をルート build 配下に集約
reports {
html.required = true
xml.required = true
html.outputLocation = file("$buildDir/reports/jacoco/piyo-aggregated")
xml.outputLocation = file("$buildDir/reports/jacoco/piyo-aggregated/report.xml")
}
}