0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

マルチプロジェクト構成のGradleプロジェクトで各サブプロジェクトのカバレッジを取得する

Last updated at Posted at 2025-04-30
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")
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?