在Gradle Multiproject中应用Jacoco

时间:2015-07-02 02:36:45

标签: gradle jacoco

我在一些Gradle实现中使用Jacoco,并决定改进其在多项目中的使用。

我创建了一个类似于

的结构
parent-project
  - model-project
  - service-project

在我的build.gradle父项目中,我定义了:

allprojects {
  apply plugin: 'jacoco'
  //apply other plugins

  repositories {...}

  test.finalizedBy(project.tasks.jacocoTestReport)

  jacocoTestReport {

    reports {

      csv.enabled false

      html{
        enabled true
        destination "${buildDir}/reports/jacoco"
      }

      xml.enabled false
    }

    additionalSourceDirs = files(sourceSets.main.allJava.srcDirs)
  }

  //other configurations

  buildscript {

    repositories {

      jcenter()
      mavenCentral()
      mavenLocal()
    }

    dependencies {
      classpath 'org.ajoberstar:gradle-jacoco:0.2.0'
    }
  }
}

我收到的所有时间都执行了这段代码:

jacocoTestReport SKIPPED

所以我发现this discussion我意识到了

  

如果某些子项目根本没有任何测试,jacocoRootReport不起作用,因为这会导致JacocoReport的onlyIf为假

我尝试插入以下代码,但仍然无法正常工作

task jacocoRootReport(type: org.gradle.testing.jacoco.tasks.JacocoReport) {
  dependsOn = subprojects.test
  additionalSourceDirs = files(subprojects.sourceSets.main.allSource.srcDirs)
  sourceDirectories = files(subprojects.sourceSets.main.allSource.srcDirs)
  classDirectories = files(subprojects.sourceSets.main.output)
  executionData = files(subprojects.jacocoTestReport.executionData)
  reports {
    html.enabled = true
    xml.enabled = true
    csv.enabled = false
  }
  onlyIf = {
    true
  }
  doFirst {
    executionData = files(executionData.findAll {
        it.exists()
    })
  }
}

我也试过在服务项目中创建一个测试...仍然没有工作,跳过的消息仍然存在

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

这是一个不正确的配置。至于我使用TestNG而不是JUnit,我需要在build.gradle中指定它

所以我的父配置改为:

subprojects {

  apply plugin: 'jacoco'
  //apply other plugins

  def coverageSourceDirs = [
    'src/main/java'
  ]

  repositories {...}

  jacoco {
    toolVersion = '0.7.5+'
  }

  jacocoTestReport {

    additionalSourceDirs = files(sourceSets.main.allSource.srcDirs)
    classDirectories =  files(sourceSets.main.output)
    sourceDirectories = files(sourceSets.main.allSource.srcDirs)

    reports {

      csv.enabled false

      html{
        enabled true
        destination "${buildDir}/reports/jacoco"
      }

      xml.enabled false
    }
  }

  test {
    useTestNG()
  }

  test.finalizedBy(project.tasks.jacocoTestReport)

  dependencies {

    testCompile "org.testng:testng:$testNGVersion"
    testCompile "org.hamcrest:hamcrest-all:$hamcrestVersion"
    testCompile "org.springframework:spring-test:$springVersion"
  }

  task jacocoRootReport(type: org.gradle.testing.jacoco.tasks.JacocoReport) {

    additionalSourceDirs = files(subprojects.sourceSets.main.allSource.srcDirs)
    classDirectories = files(subprojects.sourceSets.main.output)
    dependsOn = subprojects.test
    executionData = files(subprojects.jacocoTestReport.executionData)
    sourceDirectories = files(subprojects.sourceSets.main.allSource.srcDirs)

    reports {

      csv.enabled false

      html{
        enabled true
        destination "${buildDir}/reports/jacoco"
      }

      xml.enabled false
    }

    onlyIf = {
      true
    }

    doFirst {
      executionData = files(executionData.findAll {
        it.exists()
      })
    }
  }
}