gpt4 book ai didi

gradle - 如何将模块中的排除项聚合到根排除项中?

转载 作者:行者123 更新时间:2023-12-03 03:55:23 24 4
gpt4 key购买 nike

我们有一个多模块 Gradle 项目。所有模块都是根项目目录的直接子目录。

在模块中,我们运行 JUnit 并使用 Jacoco 生成覆盖范围。在根中,我们聚合了覆盖范围。这一切都有效,但目前我们必须输入两次排除项:

MODULE1:

apply plugin: "jacoco"
jacoco {
toolVersion = "0.8.5"
}

def static analysisExcludes() {
return [
"com/ourcompany/module1/*Config*",
"com/ourcompany/module1/endpoint/exception/**",
"com/ourcompany/module1/v2/**",
"com/ourcompany/module1/v3/**",
"src/generated/**",
"src/*test*/**",
"wrapper/dists/**"
]
}

def execData() {
return files(fileTree(buildDir).include("jacoco/*.exec"))
}

jacocoTestReport {
getExecutionData().setFrom(execData())
afterEvaluate {
classDirectories.setFrom(files(classDirectories.files.collect {
fileTree(dir: it, exclude: analysisExcludes())
}))
}
}

jacocoTestCoverageVerification {
getExecutionData().setFrom(execData());
afterEvaluate {
classDirectories.setFrom(files(classDirectories.files.collect {
fileTree(dir: it, exclude: analysisExcludes())
}))
}
violationRules {
rule {
limit {
minimum = 0.93
}
}
}
}

MODULE2:

apply plugin: "jacoco"
jacoco {
toolVersion = "0.8.5"
}

def static analysisExcludes() {
return [
"com/ourcompany/module2/*IgnoreMe*"
"src/generated/**",
"src/*test*/**",
"wrapper/dists/**"
]
}

def execData() {
return files(fileTree(buildDir).include("jacoco/*.exec"))
}

jacocoTestReport {
getExecutionData().setFrom(execData())
afterEvaluate {
classDirectories.setFrom(files(classDirectories.files.collect {
fileTree(dir: it, exclude: analysisExcludes())
}))
}
}

jacocoTestCoverageVerification {
getExecutionData().setFrom(execData());
afterEvaluate {
classDirectories.setFrom(files(classDirectories.files.collect {
fileTree(dir: it, exclude: analysisExcludes())
}))
}
violationRules {
rule {
limit {
minimum = 0.87
}
}
}
}

ROOT:

apply plugin: "jacoco"
jacoco {
toolVersion = "0.8.5"
}

// TODO: Eliminate double-entry bookkeeping by getting the excludes from the modules.
def static aggregatedAnalysisExcludes() {
return [
"**/com/ourcompany/module1/*Config*",
"**/com/ourcompany/module1/endpoint/exception/**",
"**/com/ourcompany/module1/v2/**",
"**/com/ourcompany/module1/v3/**",
"**/com/ourcompany/module2/*IgnoreMe*"
"**/src/generated/**",
"**/src/*test*/**",
"**/wrapper/dists/**"
]
}

def execData() {
return files(fileTree(rootDir).include("**/build/jacoco/*.exec"))
}

def includedClasses() {
return files(fileTree(dir: rootDir, include: "**/build/classes/**", exclude: aggregatedAnalysisExcludes()))
}

task jacocoAggregateReport(type: JacocoReport) {
getExecutionData().setFrom(execData());
afterEvaluate {
classDirectories.setFrom(includedClasses())
}
}

task jacocoAggregateTestCoverageVerification(type: JacocoCoverageVerification) {
getExecutionData().setFrom(execData());
afterEvaluate {
classDirectories.setFrom(includedClasses())
}
violationRules {
rule {
limit {
minimum = 0.91
}
}
}
}

task mergeJacocoExecData(type: JacocoMerge) {
setExecutionData(execData());
setDestinationFile(new File("build/jacoco/all.exec"))
}

apply plugin: "org.sonarqube"

def junitResultsDirs() {
def dirs = []
rootDir.eachDirRecurse { dir ->
if (dir.absolutePath.matches("^.*/build/test-results/(test|(component|integration)Test)\$")) {
dirs << dir
}
}
return dirs;
}

sonarqube {
properties {
property "sonar.projectName", "Multimodule Repo"
property "sonar.projectKey", "multimodule-repo"
property "sonar.java.coveragePlugin", "jacoco"
property "sonar.jacoco.reportPath", "build/jacoco/all.exec"
property "sonar.junit.reportsPath", junitResultsDirs()
property "sonar.issuesReport.html.enable", true
property "sonar.issuesReport.console.enable", true
property 'sonar.exclusions', aggregatedAnalysisExcludes()
}
}

我们想以某种方式迭代模块以获取它们的排除项,并将它们组合成聚合的排除项,添加“**/”前缀,使路径相对于根。

在伪代码中:
def aggregatedAnalysisExcludes() {
return rootDir.modules.excludes.withPrefixes("**/")
}

或者:
def aggregatedAnalysisExcludes() {
def excludes = []
for (Module m : rootDir.modules) {
excludes << m.excludes.withPrefixes("**/")
}
return excludes
}


我们如何真正编写代码?

注意:假设有一种方法可以做我们想做的事,结果列表将包含 src/generated、src/test 和 wrapper/dists 的重复项。我们尝试创建具有重复项的列表以查看这是否重要,但事实并非如此。即使有重复项,排除项也能正常工作。但是,如果有一种干净的声明方式来删除重复项,那将是对解决方案的一个很好的补充。

最佳答案

我了解您希望确保 Jacoco 排除在子项目级别上生成的内容自动传播到聚合级别。

这可以通过确保不会过早评估文件树来实现,当然还可以向 JacocoReport 任务询问它们是如何配置的。我找到了this Gist使用该解决方案,并编写了一个创建汇总报告的小插件,称为 gradle-jacoco-log (查看内部以找到 source code which explicitly performs the aggregation ,包括排除项)。

您基本上添加到您的 build.gradle:

plugins {
id 'org.barfuin.gradle.jacocolog' version '1.1.0'
}

然后运行
gradle jacocoAggregatedReport

这是 full example project其中包含自动汇总的 JacocoReport 排除项的子项目。

关于gradle - 如何将模块中的排除项聚合到根排除项中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60728028/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com