Sonar+Maven+Idea+Jenkins 持续测试
安装部署
server (sonarqube) +cli(oldversion:runner newversion:scanner)
本地使用(本机同时作为server&client)
- 下载解压sonarQube7.3,sonarscanner2.8 (可适配mysql5.7.29+jdk1.8)
- 创建数据库:sonar
- add env vriables :
SONAR_HOME=your sonar install dir ;
SONAR_SCANNER_HOME=your sonar_scanner install dir ; - sonar.properties
sonar.jdbc.username=XXXXXXXXXXXXXXXX
sonar.jdbc.password=XXXXXXXXXXXXXXXX
sonar.jdbc.url=jdbc:mysql//XXXXXXXXXXXXXXXX
sonar.web.port=9000
- sonar-scanner.properties
sonar.host.url=https://localhost:9000
sonar.sourceEncoding=UTF-8
sonar.jdbc.username=XXXXXXXXXXXXX
sonar.jdbc.password=XXXXXXXXXXXXX
sonar.jdbc.url=jdbc:mysql://XXXXXXXXXXXXX
- run D:\sonarqube-7.3\bin\your ops sys\StartSonar.bat
- visit http://localhost:9000
- sonar-project.properties(create property file yourself in your project(to be analyzed) root dir)
# must be unique in a given SonarQube instance
sonar.projectKey=XXXXXXX
sonar.projectName=XXXXXXX
sonar.projectVersion=1.8.0.0
sonar.login=admin
sonar.password=admin
sonar.sources=src/main/java
sonar.java.binaries=./target/classes
sonar.tests=src/test
sonar.sourceEncoding=UTF-8
sonar.dynamicAnalysis=reuseReports
sonar.jacoco.reportPath=jacoco.exec
sonar.surefire.reportsPath=tests/test-reports
intergrate Jenkins
- add pom dependencies :
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.8</version>
</dependency>
<dependency>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.2</version>
</dependency>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>false</skip>
</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>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>**/services/**</include>
</includes>
<excludes>
<exclude>**/model/**</exclude>
<exclude>**/constant/**</exclude>
<exclude>**/util/**</exclude>
</excludes>
<rules>
<rule implementation="org.jacoco.maven.RuleConfiguration">
<element>BUNDLE</element>
<limits>
<limit implementation="org.jacoco.report.check.Limit">
<counter>METHOD</counter>
<value>COVEREDRATIO</value>
<minimum>0.80</minimum>
</limit>
<limit implementation="org.jacoco.report.check.Limit">
<counter>INSTRUCTION</counter>
<value>COVEREDRATIO</value>
<minimum>0.80</minimum>
</limit>
<limit implementation="org.jacoco.report.check.Limit">
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.80</minimum>
</limit>
<limit implementation="org.jacoco.report.check.Limit">
<counter>CLASS</counter>
<value>MISSEDCOUNT</value>
<maximum>0</maximum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</plugin>
- push code to git repo
- ‘build now’ on jenkins : (auto pull code ---- run tests — execute sonar analysis — push report to sonar server )
have to set sonar server in jenkins project;
have to make sure project key same as project on sonar server
issue solving
issue desc: surefire didnt stop even if tests done and build finished. so here runs many java process on jenkins server , and caused the resource problems.
reason : unknown.
action : add post step on jenkins to stop surefire processes manually.
#!/bin/bash
echo "Stopping surefire after sonar done... ..."
pid=`ps -ef | grep surefire | grep -v grep | grep -v jenkins.war | awk '{print $2}'`
if [ -n "$pid" ]
then
kill -9 $pid
fi
echo "Surefire Stopped."