Jenkins+sonar+jcoco+ivy做代码覆盖率
首先,研发写的代码里要有单元测试相关的桩内容。
1.需要下载可用的jacoco.jar包配置到build.xml中
2.jenkins配置:
sonar.projectKey=${JOB_NAME}
sonar.projectName=$JOB_NAME
sonar.projectVersion=$SVN_REVISION
sonar.sources=.
sonar.language=java
sonar.sourceEncoding=UTF-8
sonar.dynamicAnalysis=reuseReports
sonar.core.codeCoveragePlugin=jacoco
sonar.surefire.reportsPath=${WORKSPACE}/billtrunk/target/junit
sonar.java.binaries=${WORKSPACE}/billtrunk/bin/classes
sonar.jacoco.reportPath=${WORKSPACE}/billtrunk/target/sonar/jacoco.exec
sonar.junit.reportPaths=${WORKSPACE}/billtrunk/target/junit
sonar.jacoco.itReportPath=${WORKSPACE}/billtrunk/target/ITCoverageReport/jacocoIT.exec
3.build.xml样本
<?xml version="1.0" encoding="UTF-8"?>
<project name="Simple Java Project analyzed with the Sonar Ant Task" default="all" basedir="." xmlns:jacoco="antlib:org.jacoco.ant" xmlns:sonar="antlib:org.sonar.ant" xmlns:ivy="antlib:org.apache.ivy.ant">
<!-- ========= Define the main properties of this project ========= -->
<property name="src.dir" value="src" />
<property name="build.dir" value="bin" />
<property name="classes.dir" value="${build.dir}/classes" />
<property name="lib.dir" value="${build.dir}/lib/"/>
<property name="reports.junit.xml.dir" value="${basedir}/target/junit"/>
<property name="sonar.coverage.lib" value="/opt/lib_coverage"/>
<property name="ivysettings.dir" value="/opt/ivysettings"/>
<property name="jacoco.dir" value="${basedir}/target/sonar"/>
<property name="reports.ITCoverageReport.exec" value="${basedir}/target/ITCoverageReport"/>
<property name="reports.ITCoverageReport.dir" value="${reports.ITCoverageReport.exec}/report"/>
<path id="project.classpath">
<fileset dir="${lib.dir}">
<include name="*.jar"/>
</fileset>
<pathelement path="${classes.dir}"/>
</path>
<ivy:settings file="${ivysettings.dir}/ivysettings.xml" />
<target name="resolve" description="--> parse ivy.xml">
<ivy:resolve file="ivy.xml" conf="*" />
<ivy:retrieve pattern="${lib.dir}/[artifact].[ext]"/>
</target>
<target name="clean">
<delete dir="${build.dir}" />
<delete dir="${jacoco.dir}"/>
</target>
<target name="init">
<mkdir dir="${build.dir}" />
<mkdir dir="${classes.dir}" />
<mkdir dir="${reports.junit.xml.dir}" />
<mkdir dir="${jacoco.dir}" />
</target>
<target name="compile" depends="init">
<javac srcdir="${src.dir}" destdir="${classes.dir}" source="1.7" target="1.7" debug="true"
deprecation="false" optimize="false" failonerror="true" >
<compilerarg line="-encoding UTF-8"/>
<classpath refid="project.classpath"></classpath>
</javac>
<copy todir="${classes.dir}" overwrite="true">
<fileset dir="${src.dir}/main/java/">
<exclude name="**/*.java"/>
</fileset>
<fileset dir="${src.dir}/test/java/">
<exclude name="**/*.java"/>
</fileset>
<fileset dir="${src.dir}/main/resources/">
<exclude name="**/*.java"/>
</fileset>
<fileset dir="${src.dir}/test/resources/">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target name="test" depends="compile">
<taskdef name="junit" classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask">
<classpath>
<path refid="project.classpath"/>
</classpath>
</taskdef>
<!-- Import the JaCoCo Ant Task -->
<taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
<!-- Update the following line, or put the "jacocoant.jar" file in your "$HOME/.ant/lib" folder -->
<classpath path="${sonar.coverage.lib}/jacocoant0.7.9.jar" />
</taskdef>
<!-- Run your unit tests, adding the JaCoCo agent -->
<jacoco:coverage destfile="${jacoco.dir}/jacoco.exec">
<junit fork="on" forkmode="once" printsummary="on" showoutput="true">
<formatter type="xml" usefile="true"/>
<classpath>
<path refid="project.classpath"/>
</classpath>
<batchtest todir="${reports.junit.xml.dir}">
<fileset dir="${classes.dir}">
<include name="test/**/*Test.class" />
</fileset>
</batchtest>
</junit>
</jacoco:coverage>
<junitreport todir="${reports.junit.xml.dir}">
<fileset dir="${reports.junit.xml.dir}">
<include name="TEST-*.xml" />
</fileset>
<report format="frames" todir="${reports.junit.xml.dir}" />
</junitreport>
<jacoco:report>
<executiondata>
<file file="${jacoco.dir}/jacoco.exec" />
</executiondata>
<structure name="ma project coverage report">
<classfiles>
<fileset dir="${classes.dir}" />
</classfiles>
<sourcefiles encoding="UTF-8">
<fileset dir="${src.dir}" />
</sourcefiles>
</structure>
<html destdir="${basedir}/target/report" />
<csv destfile="${basedir}/target/report/report.csv" />
<xml destfile="${basedir}/target/report/report.xml" />
</jacoco:report>
</target>
<target name="it coverage" depends="test">
<jacoco:dump address="192.168.126.7" reset="true" destfile="${reports.ITCoverageReport.exec}/jacocoIT.exec" port="8080" append="true">
</jacoco:dump>
</target>
<target name="it coverage report" depends="it coverage">
<delete dir="${reports.ITCoverageReport.dir}" />
<mkdir dir="${reports.ITCoverageReport.dir}" />
<jacoco:report>
<executiondata>
<file file="${reports.ITCoverageReport.exec}/jacocoIT.exec" />
</executiondata>
<structure name="integration test coverage rate report">
<group name="Check Order Related">
<classfiles>
<fileset dir="${classes.dir}" />
</classfiles>
<sourcefiles encoding="UTF-8">
<fileset dir="${basedir}/src" />
</sourcefiles>
</group>
</structure>
<html footer="baby-console it coverage report" destdir="${reports.ITCoverageReport.dir}" />
<csv destfile="${reports.ITCoverageReport.dir}/coverage-report.csv" />
<xml destfile="${reports.ITCoverageReport.dir}/coverage-report.xml" />
</jacoco:report>
</target>
<!-- ========= The main target "all" ========= -->
<target name="all" depends="clean,resolve,compile,it coverage report" />
</project>
4.单元测试要显示在sonarqube中,需要在web端配置


5.可以在设计,链接中加入详情
可以tomcat的conf/server.xml中配置链接 <host>标签内加上访问url
<Context path="/coverage" docBase="/opt/jenkins/workspace/UNIT_TEST_srpbase-common/billtrunk/target/report" debug="0" privileged="true">
</Context>
<Context path="/junit" docBase="/opt/jenkins/workspace/UNIT_TEST_srpbase-common/billtrunk/target/junit" debug="0" privileged="true">
</Context>




---------------------
原文:https://blog.csdn.net/aaf02002/article/details/81507410
版权声明:本文为博主原创文章,转载请附上博文链接!
Jenkins+sonar+jcoco+ivy做代码覆盖率的更多相关文章
- Jenkins+Sonar 项目构建前代码审查
一.sonar简介 1.概述 Sonar (SonarQube)是一个开源平台,用于持续检查代码质量,不只是一个质量数据报告工具,更是代码质量管理平台. 支持Java, C#, C/C++, PL/S ...
- 使用Cobertura做代码覆盖率测试
经验总结:首先要把cobertura.jar包含ant的classpath路径中,其次要求它包含在测试用例的classpath中: 使用cobertura做代码覆盖率测试中出现的问题:覆盖率始终为0, ...
- Jenkins+Sonar质量门禁【实践篇-maven版】
Jenkins+Sonar质量门禁[实践篇-maven版] 配置文档百度挺多的,就不展开来了 首先很遗憾的告诉大家,maven版做不了质量门禁!只能扫描!!! 就我们公司项目里,jenkins ...
- jenkins + sonar 安装配置
最近把snoar 添加上了 [root@snoar data]# wget https://sonarsource.bintray.com/Distribution/sonarqube/sonar ...
- Jenkins+Sonar搭建持续集成和代码质量检查环境
Jenkins+Sonar搭建 一.相关环境及下载地址 系统:Ubuntu JDK:1.8 MySQL:5.7 软件包: jenkins_2.121.3_all.deb sonarqube-7.3.z ...
- jenkins+sonar发送结果邮件的状态问题修复
在我的这篇博文中:使用jenkins+sonar进行代码扫描,并发送自定义邮件 邮件的配置为默认的$PROJECT_DEFAULT_SUBJECT 所以发送的邮件标题中的状态是jenkins构建的状态 ...
- Jenkins+Sonar集成对代码进行持续检测
介绍 SonarQube(曾用名Sonar(声纳)[1])是一个开源的代码质量管理系统. 特征 支持超过25种编程语言[2]:Java.C/C++.C#.PHP.Flex.Groovy.JavaScr ...
- Jenkins Sonar
sonar简介 SonarQube是 一个开源的代码质量分析平台,便于管理代码的质量,可检查出项目代码的漏洞和潜在的逻辑问题.同时,它提供了丰富的插件,支持多种语言的检测, 如 Java.Python ...
- jenkins使用jacoco插件检测代码覆盖率(八)
代码覆盖率:类覆盖,方法覆盖,行覆盖,指令覆盖……(简而言之,就是判断有没有被执行) 覆盖率 = 已经执行的代码 / 总代码 (1)创建maven项目,配置pom.xml如下 pom.xml < ...
随机推荐
- LG4556 [Vani有约会]雨天的尾巴 动态开点线段树+线段树合并
问题描述 LG4556 题解 对于每一个结点,建立一棵动态开点线段树. 然后自低向上合并线段树. 同时维护整个值域的最大值和最大值位置. \(\mathrm{Code}\) #include<b ...
- LG1337 [JSOI2004]平衡点 / 吊打XXX 模拟退火
问题描述 LG1337 题解 模拟退火模板 记住概率公式: \(exp(\frac{dealt}{T}) \times rand \ge R_A^ND^M_AX\) zzk太欧了,我交了一版没过他来了 ...
- luoguP3181 [HAOI2016]找相同字符
题意 考虑将\(s1\)和\(s2\)接在一起求出相同子串个数,再求出\(s1\)自己匹配的相同子串个数和\(s2\)自己匹配的相同子串个数减去即可. 如何求相同子串个数: 我们知道子串的集合即所有后 ...
- 第十 构建Web内容的技术
第十章 构建Web内容的技术 一.HTML HTML(HyperText Markup Language,超文本标记语言)是为了发送Web 上的超文本(Hypertext)而开发的标记语言.超文本是一 ...
- CentOS单机安装FastDFS&整合Nginx
单机安装 一 准备工作 准备linux服务器或虚拟机,这里是虚拟机,操作系统CentOS 6.4 Tracker 和 Storage 安装在一台机器上 FastDFS 5.08版本 1,准备软件 软件 ...
- vue 使用watch监听实现类似百度搜索功能
watch监听方法,watch可以监听多个变量,具体使用方法看代码: HTML: <!doctype html> <html lang="en"> < ...
- spring boot 开启https
1.生成证书 keytool -genkey -alias tomcat -keyalg RSA -keystore E:/https.keystore 将生成好的证书放在项目根目录即可 2 修改配置 ...
- python-4-格式化输出
前言 有些小伙伴在打印中乱码或者编码不对,在这里讲格式化输出前,先讲下编码.我们都知道目前主流使用就是utf-8编码. 一.编码简介 编码用来让计算机识别,当然我们都知道计算机只能识别01010101 ...
- LeetCode 234:回文链表 Palindrome Linked List
请判断一个链表是否为回文链表. Given a singly linked list, determine if it is a palindrome. 示例 1: 输入: 1->2 输出: ...
- VSFTP日志文件详解
开启FTP服务器记录上传下载的情况,如果启用该选项,系统将会维护记录服务器上传和下载情况的日志文件.默认情况下,该日志文件为 /var/log/vsftpd.log # This depends on ...