環境准備:
1.Jenkins集成環境(安裝 sonarqube插件)
2.安裝sonarqube服務(下載sonarqube安裝包並解壓,目錄到"sonarqube-5.4/bin/linux-x86-64"下運行命令"./sonar.sh start"啟動服務)
啟動服務后jps看到如下結果
33878 Jps 15191 SearchServer 15134 WrapperSimpleApp 15455 WebServer
Jenkins配置:
項目配置:
項目pom.xml添加如下配置(添加及配置jacoco插件):
<build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <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> <version>3.1</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>8.1.16.v20140903</version> <configuration> <stopPort>9966</stopPort> <stopKey>foo</stopKey> <scanIntervalSeconds>10</scanIntervalSeconds> <webApp> <contextPath>/p_test</contextPath> </webApp> </configuration> </plugin> </plugins> <pluginManagement> <plugins> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.7.5.201505241946</version> </plugin> </plugins> </pluginManagement> </build> <!--覆蓋率 --> <profiles> <profile> <id>coverage-per-test</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.9</version> <configuration> <!--忽略某個目錄的測試代碼 --> <includes> <include>**/*Test.java</include> </includes> <properties> <property> <name>listener</name> <value>org.sonar.java.jacoco.JUnitListener</value> </property> </properties> <systemPropertyVariables> <jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile> </systemPropertyVariables> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.sonarsource.java</groupId> <artifactId>sonar-jacoco-listeners</artifactId> <version>3.8</version> <scope>test</scope> </dependency> <dependency> <groupId>org.jacoco</groupId> <artifactId>org.jacoco.agent</artifactId> <version>0.7.5.201505241946</version> <scope>test</scope> </dependency> </dependencies> </profile> </profiles> </project>
項目運行無誤就可以提交Jenkins了,找到Jenkins的該項目配置,在add post-build steps 中選擇"Invoke Standalone SonarQube Analysis"
在Analysis properties中添加如下內容:
# must be unique in a given SonarQube instance sonar.projectKey=p_test # this is the name displayed in the SonarQube UI sonar.projectName=p_test sonar.projectVersion=1.0 # Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows. # Since SonarQube 4.2, this property is optional if sonar.modules is set. # If not set, SonarQube starts looking for source code from the directory containing # the sonar-project.properties file. # Encoding of the source code. Default is default system encoding #sonar.sourceEncoding=UTF-8 sonar.svn.username=*** sonar.svn.password.secured=*** sonar.java.coveragePlugin=jacoco sonar.sources=src/main/java sonar.tests=src/test/java/ sonar.binaries=target/classes sonar.junit.reportsPath=target/surefire-reports sonar.surefire.reportsPath=target/surefire-reports
注意填寫訪問代碼源的賬號和密碼,其他如果需要可以更改項目名,收集報告的目錄等:
編譯項目,點擊sonarqube按鈕可以進入查看頁面:
點擊測試可以繼續看到測試的方法以及通過情況.