sonar 集成代码测试覆盖率插件jacoco
sonar安装与配置
sonar的安装非常简单,你只要下载sonar的压缩包,解压之后,切换到bin目录下,然后根据你的机器系统类型切换到指定的目录下去运行sonar就可以了,比如我的是在64位的centos中部署的,那我就切换到bin/linux-x86-64/这个目录下
然后在这个目录运行./sonar.sh start,就能启动sonar。最重要的就是配置要数据库连接信息,配置数据库的连接信息,是在conf文件夹下的sonar.properties文件中配置的
跟我们在平时的项目中配置一样,需要配置下面几个属性
sonar.jdbc.username=数据库用户名
sonar.jdbc.password =密码
sonar.jdbc.url =连接地址- 官方文档地址:https://docs.sonarqube.org/display/SONAR/Installing+the+Server
maven配置
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>prepare-unit-test-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>verify</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>prepare-it-test-agent</id>
<configuration>
<propertyName>jacoco.agent.argLine</propertyName>
<append>true</append>
</configuration>
<goals>
<goal>prepare-agent-integration</goal>
</goals>
</execution>
<execution>
<id>jacoco-site-it</id>
<phase>verify</phase>
<goals>
<goal>report-integration</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<argLine>${jacoco.agent.argLine}</argLine>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
</executions>
</plugin><pluginManagement>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.8</version>
</plugin>
</plugins>
</pluginManagement>prepare-unit-test-agent 这个是单元测试的配置
会生成一个jacoco.exec的文件在target目录下,并且会生成一份report在target/site/jacoco目录下。- prepare-it-test-agent 这个是集成测试的配置
会生成一个jacoco-it.exec的文件在target目录下,并且会生成一份report在target/site/jacoco-it目录下。 - 注意:exec文件是sonar在分析时会自动加载,并且会在分析结果中显示exec中的执行结果等信息。
备注:单元测试类好像必须要以Test结尾,集成测试类必须要以IT结尾。我测试好像是这样的,配置includes和excludes好像不生效,如果有对这个问题了解的朋友,请留言告知,谢谢。