使用maven构建项目(java项目或者web项目都可以)

jacoco插件的配置参考官方网址:http://www.eclemma.org/jacoco/trunk/doc/maven.html

(1)配置jacoco的依赖jar包

<dependency>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.8</version>
</dependency>

(2)配置jacoco的插件,以及相关的goal

 <plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.8</version>
    <!--这里的execution ,每一个执行的goal,对应的id必须是唯一的-->
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
        <!--这个check:对代码进行检测,控制项目构建成功还是失败-->
<execution>
<id>check</id>
<goals>
<goal>check</goal>
</goals>
</execution>
        <!--这个report:对代码进行检测,然后生成index.html在 target/site/index.html中可以查看检测的详细结果-->
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>

(3)配置代码检查的约束 rules

<!-- Configuration 里面写配置信息 -->
<configuration>
<!-- rules里面指定覆盖规则 -->
<rules>
<rule implementation="org.jacoco.maven.RuleConfiguration">
<element>BUNDLE</element>
<limits>
  <!-- 指定方法覆盖到80% -->
<limit implementation="org.jacoco.report.check.Limit">
<counter>METHOD</counter>
<value>COVEREDRATIO</value>
<minimum>0.80</minimum>
</limit>
<!-- 指定指令覆盖到80% -->
<limit implementation="org.jacoco.report.check.Limit">
<counter>INSTRUCTION</counter>
<value>COVEREDRATIO</value>
<minimum>0.80</minimum>
</limit>
<!-- 指定行覆盖到80% -->
<limit implementation="org.jacoco.report.check.Limit">
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.80</minimum>
</limit>
<!-- 指定类覆盖到100%,不能遗失任何类 -->
<limit implementation="org.jacoco.report.check.Limit">
<counter>CLASS</counter>
<value>MISSEDCOUNT</value>
<maximum>0</maximum>
</limit>
</limits>
</rule>
</rules>
</configuration>

(4)完整的pom.xml的配置如下

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>cn.demo</groupId>
<artifactId>answers</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>answers</name>
<url>http://maven.apache.org</url> <build>
<finalName>answers</finalName>
<plugins>
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${compiler.source}</source>
<target>${compiler.target}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>         <!--检查代码覆盖率的插件配置-->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.8</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>check</id>
<goals>
<goal>check</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions> <!-- Configuration 里面写配置信息 -->
<configuration>
<!-- rules里面指定覆盖规则 -->
<rules>
<rule implementation="org.jacoco.maven.RuleConfiguration">
<element>BUNDLE</element>
<limits>
<!-- 指定方法覆盖到80% -->
<limit implementation="org.jacoco.report.check.Limit">
<counter>METHOD</counter>
<value>COVEREDRATIO</value>
<minimum>0.80</minimum>
</limit>
<!-- 指定指令覆盖到80% -->
<limit implementation="org.jacoco.report.check.Limit">
<counter>INSTRUCTION</counter>
<value>COVEREDRATIO</value>
<minimum>0.80</minimum>
</limit>
<!-- 指定行覆盖到80% -->
<limit implementation="org.jacoco.report.check.Limit">
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.80</minimum>
</limit>
<!-- 指定类覆盖到100%,不能遗失任何类 -->
<limit implementation="org.jacoco.report.check.Limit">
<counter>CLASS</counter>
<value>MISSEDCOUNT</value>
<maximum>0</maximum>
</limit> </limits>
</rule>
</rules>
</configuration>
</plugin> </plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<compiler.source>1.7</compiler.source>
<compiler.target>1.7</compiler.target>
<junit.version>4.12</junit.version>
</properties> <dependencies>
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.8</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

maven项目使用jacoco插件检测代码覆盖率详细配置的更多相关文章

  1. jenkins使用jacoco插件检测代码覆盖率(八)

    代码覆盖率:类覆盖,方法覆盖,行覆盖,指令覆盖……(简而言之,就是判断有没有被执行) 覆盖率 = 已经执行的代码 / 总代码 (1)创建maven项目,配置pom.xml如下 pom.xml < ...

  2. 关于执行findbugs,checkstyle,jacoco插件检测代码,GitHook的脚本编写

    Git钩子的作用: (pre-commit ) 在用户执行 git commit -m "xxx" 命令之前,先执行pre-commit文件中的脚本命令 在pre-commit文件 ...

  3. Java Maven项目使用CXF插件生成WebService代理

    CXF生成代理类插件名称:cxf-codegen-plugin 实现功能: 指定代理类所在的包 生成soapheader 操作: eclipse中run as -> maven build -& ...

  4. ANT 发布项目中 build.xml 文件的详细配置

    xml 代码 <?xml version="1.0" encoding="UTF-8"?> <!-- name:对应工程名字 default: ...

  5. Intellij热部署插件JRebel的详细配置及图解

    参考博客地址:https://blog.csdn.net/nyotengu/article/details/80629631 参考博客地址:https://blog.csdn.net/weixin_4 ...

  6. IDEA热部署(二)---jetty插件启动maven项目

    jetty插件的配置 我们使用jetty插件来进行启动我们的maven项目,在pom.xml中进行配置: <plugins> <plugin> <groupId>o ...

  7. 使用maven的插件进行maven项目的打包

    1 maven项目打包的插件有3种 maven-jar-plugin maven-assembly-plugin maven-shade-plugin 2 maven-jar-plugin 现在要新增 ...

  8. Gradle 1.12用户指南翻译——第三十四章. JaCoCo 插件

    本文由CSDN博客万一博主翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...

  9. mybatis 学习一 建立maven项目

    一.直接建立Maven项目方法 1.建立Maven项目 接下来使用Eclipse的maven构建一个web项目,以构建SpringMVC项目为例: 1.1 选择建立Maven Project 选择Fi ...

随机推荐

  1. Exception: Unexpected End Of File(crontab)

    Exception: Unexpected End Of File [solphire@hadoop02 tools]$ crontab -l 1 * * * * source /etc/profil ...

  2. 【转载】MySQL事务以及SELECT ... FOR UPDATE的使用

    MySQL中的事务,默认是自动提交的,即autocommit = 1: 但是这样的话,在某些情形中就会出现问题:比如: 如果你想一次性插入了1000条数据,mysql会commit1000次的, 如果 ...

  3. 北斗时钟同步系统-GPS卫星授时设备-NTP网络校时服务器

    北斗时钟同步系统-GPS卫星授时设备-NTP网络校时服务器 北斗时钟同步系统-GPS卫星授时设备-NTP网络校时服务器 北斗时钟同步系统-GPS卫星授时设备-NTP网络校时服务器 论述当下网络时间同步 ...

  4. CentOS环境下tomcat启动超级慢的解决方案

    在本地开发环境,应用正常启动. 在CentOS测试环境,应用启动速度也是正常的. 但是在阿里云的生产环境,tomcat启动超级慢,并且在最终打印出来以下内容: org.apache.catalina. ...

  5. PHP错误与异常

    请一定要注意,没有特殊说明:本例 PHP Version < 7 说起PHP异常处理,大家首先会想到try-catch,那好,我们先看一段程序吧:有一个test.php文件,有一段简单的PHP程 ...

  6. spring的父子容器

    在创建ssm项目工程时,经常需要读取properties资源配置文件,传统的方法当然可以. 但是spring提供了更简便的方法,@value注解. 在page.properties文件中,配置分页信息 ...

  7. Es6主要特征详解

    一.简介 本文将对es6的最佳特性进行分享和讲解.es6也称ES6/ECMAScript2015,在2015年诞生,但是目前实际开发中还很多用的是ES5(2009年),原因就是很多的浏览器不支持新的语 ...

  8. Zabbix实战-简易教程(6)--Server端高可用

    3.4 server前端高可用    至此,单台Zabbix server环境已经搭建完成,为了达到高可用效果,我们需要通过2台服务器之间通过HA软件进行探测,一旦检测到主的server挂掉后,从的s ...

  9. JavaScript中的 true

    经常看到有人写 如下代码,有时候也是凭经验猜想到底是什么意思,本着认真学习 JavaScript 的精神,专门写一篇去讨论这个问题. if(name){ //do something. }else{ ...

  10. 学习 node.js 搭建web服务器

    开始 学习使用 node.js 首先完成搭建一个 web服务器.myweb.js var http = require('http'); var url = require('url'); var h ...