Gradle로 JaCoCo 커버리지 보고서 필터링
문제:
jacoco를 사용 하는 프로젝트가 있으며 특정 클래스 및 / 또는 패키지를 필터링 할 수 있기를 원합니다.
관련 문서 :
다음 문서를 읽었습니다.
공식 jacoco 사이트 : http://www.eclemma.org/jacoco/index.html
공식 jacoco에 대한 문서 Gradle을 : https://gradle.org/docs/current/userguide/jacoco_plugin.html
공식 jacoco Github
문제, 적용 범위 작업 : https://github.com/jacoco/jacoco/wiki/FilteringOptions https://github.com/jacoco/jacoco/issues/14
관련 StackOverflow 링크 :
JaCoCo 및 Gradle-필터링 옵션 (답변 없음)
Sonarrunner 및 Gradle을 사용하여 Jacoco 보고서에서 패키지 제외 ( 소나를 사용하지 않음 )
JaCoCo-보고서에서 JSP 제외 ( Maven 에서 작동하는 것 같습니다. gradle을 사용 하고 있습니다 )
메이븐 Jacoco 구성 - 클래스 / 보고서에서 패키지가 작동하지 제외 (그것은을 위해 작동하는 것 같다 받는다는 , 내가 사용하고 Gradle을을 )
JaCoCo gradle 플러그인 제외 (이 작업을 수행 할 수 없음)
Gradle Jacoco-커버리지 보고서에는 구성에서 제외 된 클래스가 포함되어 있습니다 (매우 가깝게 보였고 사용 doFirst
했으며 나를 위해 작동하지 않았습니다)
내가 시도한 것의 예 :
apply plugin: 'java'
apply plugin: 'jacoco'
buildscript {
repositories {
mavenCentral()
jcenter()
}
}
repositories {
jcenter()
}
jacocoTestReport {
reports {
xml {
enabled true // coveralls plugin depends on xml format report
}
html {
enabled true
}
}
test {
jacoco {
destinationFile = file("$buildDir/jacoco/jacocoTest.exec")
classDumpFile = file("$buildDir/jacoco/classpathdumps")
excludes = ["projecteuler/**"] // <-- does not work
// excludes = ["projecteuler"]
}
}
}
질문:
How can I exclude certain packages and classes when generating the jacoco coverage reports?
Thanks to, Yannick Welsch
:
After searching Google, reading the Gradle docs and going through older StackOverflow posts, I found this answer on the Official gradle forums!
jacocoTestReport {
afterEvaluate {
classDirectories = files(classDirectories.files.collect {
fileTree(dir: it, exclude: 'com/blah/**')
})
}
}
Source: https://issues.gradle.org/browse/GRADLE-2955
Solution to my build.gradle
for Java/Groovy projects:
apply plugin: 'java'
apply plugin: 'jacoco'
jacocoTestReport {
reports {
xml {
enabled true // coveralls plugin depends on xml format report
}
html {
enabled true
}
}
afterEvaluate {
classDirectories = files(classDirectories.files.collect {
fileTree(dir: it,
exclude: ['codeeval/**',
'crackingthecode/part3knowledgebased/**',
'**/Chapter7ObjectOrientedDesign**',
'**/Chapter11Testing**',
'**/Chapter12SystemDesignAndMemoryLimits**',
'projecteuler/**'])
})
}
}
As you can see, I was successfully able to add more to exclude:
in order to filter a few packages.
Source: https://github.com/jaredsburrows/CS-Interview-Questions/blob/master/build.gradle
Custom tasks for other projects such as Android:
apply plugin: 'jacoco'
task jacocoReport(type: JacocoReport) {
reports {
xml {
enabled true // coveralls plugin depends on xml format report
}
html {
enabled true
}
}
afterEvaluate {
classDirectories = files(classDirectories.files.collect {
fileTree(dir: it,
exclude: ['codeeval/**',
'crackingthecode/part3knowledgebased/**',
'**/Chapter7ObjectOrientedDesign**',
'**/Chapter11Testing**',
'**/Chapter12SystemDesignAndMemoryLimits**',
'projecteuler/**'])
})
}
}
For Gradle version 5.0 and later, the classDirectories = files(...)
gives a deprecation warning. This is the non deprecated way of excluding classes:
jacocoTestReport {
afterEvaluate {
classDirectories.setFrom(files(classDirectories.files.collect {
fileTree(dir: it, exclude: 'com/exclude/**')
}))
}
}
for me, it's fine working with
test {
jacoco {
excludes += ['codeeval/**',
'crackingthecode/part3knowledgebased/**',
'**/Chapter7ObjectOrientedDesign**',
'**/Chapter11Testing**',
'**/Chapter12SystemDesignAndMemoryLimits**',
'projecteuler/**']
}
}
as stated out in documentation https://docs.gradle.org/current/userguide/jacoco_plugin.html#N16E62 and initally asked so the answer is:
so if you ask me: it's not a question of
excludes = ["projecteuler/**"]
or
excludes += ["projecteuler/**"]
but
excludes = ["**/projecteuler/**"]
to exclude a package *.projecteuler.*
and test {}
on project level, not nested in jacocoTestReport
Here is a solution for this problem in ANT. This can be adapted to gradle by adding the following under the jacocoTestReport
task. Although this isn't really documented by jacoco, it seems like the only way to filter the test results for now.
afterEvaluate {
classDirectories = files(classDirectories.files.collect {
fileTree(dir: it, exclude: 'excluded/files/**')
})
}
This has been out for a while, but I just ran across this. I was struggling with all the exclusions needed. I found it was something much more simple for me. If you follow the Maven project layout style /src/main/java and /src/test/java, you simply need to put buildDir/classes/main in your classDirectories config like so:
afterEvaluate {
jacocoTestReport {
def coverageSourceDirs = ['src/main/java']
reports {
xml.enabled false
csv.enabled false
html.destination "${buildDir}/reports/jacocoHtml"
}
sourceDirectories = files(coverageSourceDirs)
classDirectories = fileTree(
dir: "${project.buildDir}/classes/main",
excludes: [
//whatever here like JavaConfig etc. in /src/main/java
]
)
}
}
The code below excludes classes from coverage verification as well:
jacocoTestCoverageVerification {
afterEvaluate {
classDirectories = files(classDirectories.files.collect {
fileTree(dir: "${project.buildDir}/classes/main",
exclude: ['**/packagename/**'])
})
}
}
add below config in gradle.properties file
coverageExcludeClasses=["com.example.package.elasticsearch.*", "com.example.package2.*",]
참고 URL : https://stackoverflow.com/questions/29887805/filter-jacoco-coverage-reports-with-gradle
'developer tip' 카테고리의 다른 글
Java Eclipse : JAR로 내보내기와 실행 가능한 JAR로 내보내기의 차이점 (0) | 2020.09.22 |
---|---|
HTTP 요청에 여러 쿠키 헤더가 허용됩니까? (0) | 2020.09.22 |
Java의 ByteBuffer에서 바이트 배열을 가져옵니다. (0) | 2020.09.22 |
JavaScript 또는 jQuery에서 HTML을 정규화하는 방법은 무엇입니까? (0) | 2020.09.22 |
Rails 4에서 컨트롤러 또는 액션에 대한 X-Frame-Options를 재정의하는 방법 (0) | 2020.09.22 |