Jacoco覆盖率工具使用之maven篇
2016-05-25 11:05
405 查看
Jacoco覆盖率工具使用之maven篇
之前的文章已经介绍过如何使用apacheant 执行jacoco工具,下面开始介绍如何使用maven使用jacoco工具。
如图所示:
新建一个测试类helloworld,code 如图所示:
新建一个测试类helloworld test,code 如图所示:
编辑pom.xml文件,增加依赖包和jacoco配置,文件如下所示:
[java] view
plain copy
<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>com.test.jacoco</groupId>
<artifactId>testJacoco</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>JaCoCo Examples</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- Sonar -->
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
<!-- The destination file for the code coverage report has to be set to the same value
in the parent pom and in each module pom. Then JaCoCo will add up information in
the same report, so that, it will give the cross-module code coverage. -->
<sonar.jacoco.itReportPath>${project.basedir}/target/jacoco.exec</sonar.jacoco.itReportPath>
<sonar.language>java</sonar.language>
</properties>
<dependencies>
<!--
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.5.3.201107060350</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<includes>com.*</includes>
</configuration>
<executions>
<execution>
<id>pre-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>post-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
至此,基于maven的jacoco的使用讲解完了,整合jenkins 和 sonar 请参考“Jacoco覆盖率工具使用”;
转载:http://blog.csdn.net/wangmuming/article/details/28868833
说明
之前的文章已经介绍过如何使用apacheant 执行jacoco工具,下面开始介绍如何使用maven使用jacoco工具。
1.首先新建一个maven项目
如图所示:
2:HelloWorld
新建一个测试类helloworld,code 如图所示:
3:HelloWorldTest
新建一个测试类helloworld test,code 如图所示:
4:编辑pom.xml文件
编辑pom.xml文件,增加依赖包和jacoco配置,文件如下所示:[java] view
plain copy
<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>com.test.jacoco</groupId>
<artifactId>testJacoco</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>JaCoCo Examples</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- Sonar -->
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
<!-- The destination file for the code coverage report has to be set to the same value
in the parent pom and in each module pom. Then JaCoCo will add up information in
the same report, so that, it will give the cross-module code coverage. -->
<sonar.jacoco.itReportPath>${project.basedir}/target/jacoco.exec</sonar.jacoco.itReportPath>
<sonar.language>java</sonar.language>
</properties>
<dependencies>
<!--
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.5.3.201107060350</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<includes>com.*</includes>
</configuration>
<executions>
<execution>
<id>pre-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>post-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
5:打包测试
如图所示:6: 执行结果
执行结果:至此,基于maven的jacoco的使用讲解完了,整合jenkins 和 sonar 请参考“Jacoco覆盖率工具使用”;
转载:http://blog.csdn.net/wangmuming/article/details/28868833
相关文章推荐
- AWR 使用
- 2016 UESTC DP专题题解
- 学习进度13
- 推荐一些国内的Jquery CDN免费服务
- String、StringBuffer与StringBuilder之间区别
- 解决Eclipse建立Maven项目后无法建立src/main/java资源文件夹的办法
- MFC 控件关联变量
- 判断网络连接的是什么网络
- 一步一步在ubuntu上安装即时通讯服务器-Openfire
- DB2 注册 错误提示 LIC1450I The product licensed was not found in the DB2 copy
- 小白菜第一次接触ECSHOP模板感言
- linux 内核之旅
- Mac Item2 设置别名 永久生效
- 省前训练...Orz
- 《人月神话》阅读笔记01
- apache限制访问
- Spark中组件Mllib的学习32之朴素贝叶斯分类器(伯努利朴素贝叶斯)*
- android 从java到C层文件读取流程
- 音乐API
- Android创建桌面快捷方式