持续集成-jenkins+maven+sonar+testng+jacoco

1、基于maven的编译、代码检查、打包、测试、覆盖率检测

clean -》 compile 》 sonar -》 test -》 jacoco

clean compile

2、jenkins简单model

java -jar jenkins.war 自带jetty服务 默认端口8080

启动成功:INFO: Jenkins is fully up and running

根目录:/root/.jenkins/ 

jenkins登陆:默认用户admin admin

可以去掉user验证

设置LDAP认证登录后,发现设置错误导致无法登陆Jenkins。为了去除认证登录的设置,可以采取如下操作:

1、停止Jenkins
2、修改$JENKINS_HOME下的config.xml
①将<useSecurity>true</useSecurity>元素中的true改为false
②将<authorizationStrategy>和<securityRealm>元素的内容删掉


3、sonar集成

maven的setting.xml文件中添加sonar插件,调用搭建的sonar平台(url+数据库等)

pom.xml添加sonar插件

jenkins中添加sonar:sonar增加执行sonar节点

4、testng集成

添加jenkins插件:TestNG Results Plugin的testng报告插件

在pom中添加testng插件:maven-surefire-plugin

在testng中传递参数:

jenkins中添加"参数化构建过程-》构建命令中需要输入参数(可以设置默认值)"

-》在pom.xml文件中接受jenkins传递过来的参数("<SystemPropertyVariables>属性,根据参数名进行传递")

-》在test.xml中添加<parameter name="" value="${testEnv}">标签

-》在测试代码中使用@Parameters({"",""})

测试代码需要再test包中,否则找不到test类

5、jacoco集成

jenkins添加jaocco报告插件:jacoco plugin

pom中添加jacoco插件:jacoco-maven-plugin

1、在testng中添加jvm参数,在启动testng时加载jacoco的prepare-agent包,jacoco包记录调用过程:<argLine>${surefireArgLine}</argLine>

被测试类不能命名为test,且test类在test目录下,注意jacoco的版本

2、添加jacoco的prepare-agent包(在maven的initialize阶段,将Jacoco的runtime agent作为VM的一个参数 传给被测程序,用于监控JVM中的调用)

3、testng执行完成之后,生成记录调用过程的jacoco文件jacoco.exec文件

4、根据jacoco.exec文件和jenkins中配置的java源文件+classes文件生成最后的jacoco覆盖率报告

jacoco生成调用记录文件只依赖于test工程的jvm,所以支持源工程与test工程分开的分布式代码统计

<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.7.201606060606</version>
<executions>
<execution>
<!-- 在maven的initialize阶段,将Jacoco的runtime agent作为VM的一个参数 传给被测程序,用于监控JVM中的调用。 -->
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>
<!-- ${project.build.directory}/coverage-reports/jacoco.exec -->
${project.build.directory}/jacoco.exec
</destFile>
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<!-- 在程序的verify阶段,执行report测试的程序。 文件的输入为perpare-agent阶段中设置或者默认的jacoco.exec. 
参数 includes和excludes可用来选定report中过滤的类。 -->
<execution>
<id>default-report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/jacoco.exec</dataFile>
<outputDirectory>${project.reporting.outputDirectory}/jacoco</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>




猜你喜欢

转载自blog.csdn.net/weilan100/article/details/52423484