使用Grade,Jacoco和Sonarqube处理多项目构建

时间:2019-09-12 23:43:05

标签: gradle sonarqube jacoco

到目前为止,它一直有效(至少看起来像它可以正常工作),但是我不认为它是惯用的,甚至不会经受住时间的考验,甚至几个月。有什么办法可以“按书”做得更好或更完善吗?

我们在Gradle中构建了一些多项目,其中一个项目的测试可以触及另一个人的代码,因此,即使它不在同一项目中,但在同一多项目中,也必须查看覆盖率。我可能正在解决不存在的问题,但是在早期的SonarQube版本中,我不得不“ jacocoMerge”覆盖结果。

jacocoTestReport {
    reports {
        executionData (tasks.withType(Test).findAll { it.state.upToDate || it.state.executed })
        xml.enabled true
    }
}

if(!project.ext.has('jacocoXmlReportPathsForSonar')) {
    project.ext.set('jacocoXmlReportPathsForSonar', [] as Set<File>)
}

task setJacocoXmlReportPaths {
    dependsOn('jacocoTestReport')
    doLast {
        project.sonarqube {
            properties {
                property 'sonar.coverage.jacoco.xmlReportPaths', project.
                    ext.
                    jacocoXmlReportPathsForSonar.
                    findAll { d -> d.exists() }.
                    collect{ f -> f.path}.
                    join(',')
            }
        }
    }
}
project.rootProject.tasks.getByName('sonarqube').dependsOn(setJacocoXmlReportPaths)

sonarqube {
    properties {
        property "sonar.java.coveragePlugin", "jacoco"
        property "sonar.tests", []
        property "sonar.junit.reportPaths", []
    }
}

afterEvaluate { Project evaldProject ->
    JacocoReport jacocoTestReportTask = (JacocoReport) evaldProject.tasks.getByName('jacocoTestReport')

    evaldProject.ext.jacocoXmlReportPathsForSonar += jacocoTestReportTask.reports.xml.destination

    Set<Project> dependentProjects = [] as Set<Project>

    List<String> configsToCheck = [
        'Runtime',
        'RuntimeOnly',
        'RuntimeClasspath',
        'Compile',
        'CompileClasspath',
        'CompileOnly',
        'Implementation'
    ]

    evaldProject.tasks.withType(Test).findAll{ it.state.upToDate || it.state.executed }.each { Test task ->
        logger.debug "JACOCO ${evaldProject.path} test task: ${task.path}"

        sonarqube {
            properties {
                properties["sonar.junit.reportPaths"] += task.reports.junitXml.destination.path
                properties["sonar.tests"] += task.testClassesDirs.findAll { d -> d.exists() }
            }
        }

        configsToCheck.each { c ->
            try {
                Configuration cfg = evaldProject.configurations.getByName("${task.name}${c}")
                logger.debug "JACOCO ${evaldProject.path} process config: ${cfg.name}"

                def projectDependencies = cfg.getAllDependencies().withType(ProjectDependency.class)

                projectDependencies.each { projectDependency ->
                    Project depProj = projectDependency.dependencyProject
                    dependentProjects.add(depProj)
                }
            } catch (UnknownConfigurationException uc) {
                logger.debug("JACOCO ${evaldProject.path} unknown configuration: ${task.name}Runtime", uc)
            }
        }
    }

    dependentProjects.each { p ->
        p.plugins.withType(JacocoPlugin) {
            if (!p.ext.has('jacocoXmlReportPathsForSonar')) {
                p.ext.set('jacocoXmlReportPathsForSonar', [] as Set<File>)
            }
            p.ext.jacocoXmlReportPathsForSonar += jacocoTestReportTask.reports.xml.destination

            JacocoReport dependentJacocoTestReportTask = (JacocoReport) p.tasks.getByName('jacocoTestReport')
            dependentJacocoTestReportTask.dependsOn(jacocoTestReportTask)
            setJacocoXmlReportPaths.dependsOn(dependentJacocoTestReportTask)
        }
    }
}

0 个答案:

没有答案