Ash*_*Kan 30 code-coverage maven jacoco gitlab gitlab-ci
我需要在Gitlab中看到java maven项目的代码覆盖率报告.根据这个,这个和其他一些来源:
jacoco到插件列表中pom.xml..gitlab-ci.yml文件中添加了页面作业.Total.*?([0-9]{1,3})%到项目设置中的代码覆盖解析.但是没有任何报道报道,或者至少我看不到它.没有覆盖百分比或覆盖率报告页面.
.gitlab-ci.yml文件内容:
image: maven:latest
variables:
MAVEN_CLI_OPTS: "--batch-mode --errors --fail-at-end --show-version -DinstallAtEnd=true -DdeployAtEnd=true"
MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN -Dorg.slf4j.simpleLogger.showDateTime=true -Djava.awt.headless=true"
cache:
paths:
- .m2/repository/
build:
stage: build
script:
- mvn $MAVEN_CLI_OPTS compile
test:
stage: test
script:
- mvn $MAVEN_CLI_OPTS test
artifacts:
paths:
- target/site/jacoco/
pages:
stage: deploy
dependencies:
- test
script:
- mkdir public
- mv target/site/jacoco/index.html public
artifacts:
paths:
- public
deploy:
stage: deploy
script:
- mvn $MAVEN_CLI_OPTS verify
only:
- master
Run Code Online (Sandbox Code Playgroud)
jacoco插件pom.xml:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.5.201505241946</version>
<executions>
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
我的项目是一个私人项目gitlab.com.
管道及其所有4个作业成功通过.
我怎样才能看到报道报道?
SKB*_*KBo 27
您似乎忘了cat在.gitlab-ci.yml文件中添加调用.
你应该有类似的东西:
script:
- mvn $MAVEN_CLI_OPTS test
- cat target/site/jacoco/index.html
Run Code Online (Sandbox Code Playgroud)
话虽这么说,我认为这不是最好的方法,因为你需要用原始HTML污染你的输出,以便检索所需的覆盖值.
我建议使用此拉取请求中描述的方法:https://github.com/jacoco/jacoco/pull/488
build.xml使用此awk指令打印正确的代码覆盖率总计:
awk -F"," '{ instructions += $4 + $5; covered += $5 } END { print covered, "/",
instructions, "instructions covered"; print 100*covered/instructions, "%
covered" }' target/site/jacoco/jacoco.csv
Run Code Online (Sandbox Code Playgroud)用指令返回的内容替换Gitlab CI regexp: \d+.\d+ \% covered
编辑:
从Gitlab 8.17开始,您可以直接在.gitlab-ci.yml文件中定义regexp ,如文档中所述.
这看起来似乎是多余的,但如果此正则表达式现在是存储库历史记录的一部分,则可以将其与用于计算它的其他工具一起更改.
小智 11
GitLab员工在这里.
如果您的管理员设置了GitLab页面,您可以通过(在您的项目上)到Settings- > 来查看工件部署到的URL Pages.
你应该看到:
恭喜!您的网页服务于: https://your-namespace.example.com/your-project
点击该链接,你应该好好去!我们还扩展了对HTML工件的支持.这个问题及其相关问题讨论了现有的和即将推出的功能,这些功能可能会扩展到您在此处构建的内容.
为了显示基本的总覆盖率,您需要做的是:
test:
stage: test
image: maven:3.6.3-jdk-11
- mvn $MAVEN_CLI_OPTS test
- cat target/site/jacoco/index.html | grep -o 'Total[^%]*%'
coverage: '/Total.*?([0-9]{1,3})%/'
artifacts:
paths:
- target/site/jacoco/jacoco.xml
expire_in: 1 mos
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
Run Code Online (Sandbox Code Playgroud)
如果您想启用代码覆盖可视化功能:
visualize_test_coverage:
stage: visualize_test_coverage
image: registry.gitlab.com/haynes/jacoco2cobertura:1.0.7
script:
- 'python /opt/cover2cover.py target/site/jacoco/jacoco.xml src/main/java > target/site/cobertura.xml'
- 'python /opt/source2filename.py target/site/cobertura.xml'
needs: [ "test" ]
dependencies:
- test
artifacts:
reports:
coverage_report:
coverage_format: cobertura
path: target/site/cobertura.xml
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请在此处查看官方 Gitlab 文档
我使用这个命令.gitlab-ci.yml
cat target/site/jacoco-merge/index.html | grep -o 'Total[^%]*%' | sed 's/<.*>/ /; s/Total/Jacoco Coverage Total:/'
Run Code Online (Sandbox Code Playgroud)
这将打印一个漂亮的字符串,没有混乱和 html 标签:
Jacoco Coverage Total: 39%
Run Code Online (Sandbox Code Playgroud)
然后你可以使用 gitlabs 文档中提到的正则表达式:
Total.*?([0-9]{1,3})%
Run Code Online (Sandbox Code Playgroud)
或者你可以使用:
Jacoco Coverage Total: ([0-9]{1,3})%
Run Code Online (Sandbox Code Playgroud)
除了@SKBo所说的以外,我还想做一点调整。
有
cat target / site / jacoco / index.html
会污染您的输出结果,使您难以阅读重要内容。
我建议它:
cat your/path/to/jacoco/report/index.html | grep -o '<tfoot>.*</tfoot>'
这样可以大大降低噪音
小智 5
我正在使用此代码:
图片:maven:最新
声纳检查:
脚本:
- mvn 验证声纳:sonar -Dsonar.host.url=$SONAR_HOST_URL -Dsonar.login=$SONAR_TOKEN -Dsonar.qualitygate.wait=true
- cat target/site/jacoco/index.html | grep -o '.*'
允许失败:假
覆盖范围:“/Total.*?([0-9]{1,3})%/”
| 归档时间: |
|
| 查看次数: |
20095 次 |
| 最近记录: |