gpt4 book ai didi

gradle - 雅 cocoa | Gradle |从聚合模块中排除

转载 作者:行者123 更新时间:2023-12-03 05:34:40 28 4
gpt4 key购买 nike

我有一个包含多个模块的 Java-Gradle 项目,这意味着多个 build.gradle 文件和多个测试文件夹。
我有一个主 build.gradle,我成功地将所有多 jacoco 报告聚合到一个主报告中,使用以下配置在我的主 bu​​ild.gradle 中。
但现在我想从聚合模块中排除一些包/类。
我怎么做?

你也可以在下面看到我的尝试

apply plugin: 'jacoco'
apply plugin: 'java'

def otherProjects = [':module1', ':module2']

otherProjects.each {
// ensure other projects evaluated first so sourceSets are configured
evaluationDependsOn it
}
jacoco {
toolVersion = "0.8.4"
reportsDir = file("$buildDir/jacoco")
}
jacocoTestReport {
FileTree sourceTree = files().asFileTree
FileTree classTree = files().asFileTree
otherProjects.each {
sourceTree += project(it).sourceSets.main.allJava
classTree += project(it).sourceSets.main.output.asFileTree
}
additionalSourceDirs = sourceTree
additionalClassDirs = classTree
reports {
html.enabled true
html.destination file("${buildDir}/jacocoHtml")
}

// try 1
afterEvaluate {
classDirectories = files(classDirectories.files.collect {
fileTree(dir: it, exclude: '/com/packege_from_module1/**')
})
}

// try 2
afterEvaluate {
additionalSourceDirs = files(additionalSourceDirs.files.collect {
sourceTree(dir: it, exclude: 'com/packege_from__module1/**')
})
}

// try 3
afterEvaluate {
additionalSourceDirs = files(additionalSourceDirs.files.collect {
classTree(dir: it, exclude: 'com/packege_from__module1/**')
})
}

}

最佳答案

给定
a/src/main/java/A.java

class A {
}
a/src/test/java/ATest.java
import org.junit.Test;

public class ATest {
@Test
public void a() {
}
}
b/src/main/java/B.java
class B {
}
b/src/test/java/BTest.java
import org.junit.Test;

public class BTest {
@Test
public void b() {
}
}
settings.gradle
rootProject.name = 'example'

include 'a'
include 'b'

build.gradle
allprojects {
apply plugin: 'java'
apply plugin: 'jacoco'

jacoco {
toolVersion = "0.8.3"
}

repositories {
mavenCentral()
}

dependencies {
testCompile 'junit:junit:4.12'
}
}

task jacocoAggregateReport(type: org.gradle.testing.jacoco.tasks.JacocoReport) {
dependsOn = subprojects.test
sourceDirectories = files(subprojects.sourceSets.main.allSource.srcDirs)
classDirectories = files(subprojects.sourceSets.main.output)
executionData = files(subprojects.jacocoTestReport.executionData)
}

使用 Gradle 4.10.3 或 5.4.1 执行 gradle jacocoAggregateReport将在目录 build/reports/jacoco/jacocoAggregateReport/html/index.html 中生成以下报告

before

现在让我们排除类文件 A.class通过改变报告
    classDirectories = files(subprojects.sourceSets.main.output)

build.gradle
    classDirectories = files(subprojects.sourceSets.main.output).asFileTree.matching {
exclude 'A.class'
}

并执行 gradle jacocoAggregateReport会产生

after

您的尝试没有成功,因为报告是为来自 classDirectories 的所有类文件构建的。和 additionalClassDirs - 不排除 classDirectoriesadditionalClassDirs在尝试 2 和 3 中,没有排除 additionalClassDirs在尝试 1。

关于gradle - 雅 cocoa | Gradle |从聚合模块中排除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57197492/

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