ぽらろいどの日記

新しい知見を得たり、得られた知見を記録したり共有したりする場を予定しています。

Gradle x Groovy x Jacoco @ローカル環境

以前ローカル環境でCodenarcを利用する例を調べたが、よくよく考えるとJacoco使える方が重要だった。ということで、色々調べつつ試して使えるところまで出来たので、その結果を保存。理解を後回しにしてしまったので、別途Gradle学び直す必要がある……。

plugins {
  id 'groovy'
  id 'jacoco'
}

// jarファイル探索先をローカルに設定
repositories {
  flatDir {
    dirs "/jar/library"
  }
}

// 利用するjarファイル指定
dependencies {
  implementation fileTree(dir: "/groovy/library", include: "*.jar")

  testImplementation "org.spockframework:spock-core:2.2-M1-groovy-4.0"
  testImplementation "net.bytebuddy:byte-buddy:1.12.9"
}

tasks.withType(Test).configureEach {
  useJUnitPlatform()
}

// testタスク実行後にjacocoのレポート作成
test {
  finalizedBy jacocoTestReport
}

jacoco {
  toolVersion = "0.8.8"
}

// jacocoのレポート作成設定
jacocoTestReport {
  // testタスクを要事前実行
  dependsOn test
  // jacocoのjarファイル指定
  jacocoClasspath = fileTree(dir: "/groovy/library", include: "*.jar")

  // レポートから除くファイルの指定
  afterEvaluate {
    classDirectories.setFrom(files(classDirectories.files.collect {
      fileTree(dir: it, exclude: '**/java/lang/**')
    }))
  }

  // 生成するレポートに関する指定
  reports {
    xml.required = false
    csv.required = true
  }
}