越來越懶得寫總結了 好多都是幾句話搞定orz...
sonar負責檢測代碼質量與查看代碼覆蓋率。這個工具先搭建起來可以正常使用, 但頁面上各個功能都是做什麼的還沒有研究。
sonarqube需要用到的東西 jdk 、 mysql、 中文插件(放在SONARQUBE_HOME/extensions/plugins http://docs.codehaus.org/display/SONAR/Chinese+Pack)。
The onlyprerequisite for running SonarQube is to have Java (Oracle JRE 8 onwards orOpenJDK 8 onwards) installed on your machine.(需要jdk1.8具體信息看官網https://docs.sonarqube.org/display/SONAR/Requirements)
安裝mysql
爲sonarqube server創建一個數據庫
CREATE DATABASE sonar CHARACTER SET utf8 COLLATEutf8_general_ci;
創建訪問數據庫的用戶:
CREATE USER 'sonar' IDENTIFIED BY 'sonar';
配置sonar用戶的權限(tips: 'sonar'@'%'可配置用戶遠程連接數據庫 , 可參考 http://www.cnblogs.com/davidgu/p/3706663.html)
GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY'sonar'; GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar';flush privileges;
安裝sonarqube
在/conf/sonar.properties裏面添加數據庫信息:
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
sonar.jdbc.username: sonar
sonar.jdbc.password: sonar
# Optional properties
sonar.jdbc.driverClassName: com.mysql.jdbc.Driver
配置好後保存 到bin目錄下啓動StartSonar.bat
SonarQube默認的端口號是9000
網頁查看http://192.168.100.194:9000
安裝sonarscanner
設置SONAR_RUNNER_HOME環境變量 並在PATH中加入%SONAR_RUNNER_HOME%\bin;
如果只是在Jenkins所在服務器上 使用Jenkins系統管理的默認安裝就好
我scanner和qube裝在了同一臺服務器上,scanner下載後安裝位置隨意。
在scanner的bin目錄下sonar-scanner.properties里加上qube的地址(如果不在同一臺服務器 localhost改成qube所在ip地址)
sonar.host.url=http://localhost:9000
配置Jenkins
系統設置中配置sonarqube所在服務器的jdk
下載插件SonarQubeScanner for Jenkins
登錄sonarqube頁面,在我的賬號中找安裝項,生成token。
在Jenkins的系統管理中配置sonarqube信息,填入獲取的token。
這樣以後構建完就可以在Jenkins上鍊接到qube
Jenkins2中在Global Tool Configuration裏配置scanner信息。(經過試驗 發現不配置也可以)
構建項目
在maven命令行中寫上
clean org.jacoco:jacoco-maven-plugin:prepare-agent install -Dmaven.test.failure.ignore=true
-Dmaven.test.failure.ignore=true作用:當測試失敗時maven會自動停止構建此參數可以告訴maven忽略失敗的測試繼續構建
該參數的作用與在pom文件中maven-surefire-plugin插件的testFailureIgnore 這個配置屬性設置爲 true是一樣的
<plugin><groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
使用sonarqube:
sonar.projectKey=dd_Sonar
sonar.projectName=dd_Sonar
sonar.projectVersion=1.0
sonar.language=java
sonar.sourceEncoding=UTF-8
sonar.sources=./src (被測代碼所在目錄)
sonar.java.binaries=./target
sonar.java.coveragePlugin=jacoco
sonar.jacoco.reportPaths=./target/coverage-reports/jacoco-unit.exec
sonar.dynamicAnalysis=reuseReports
坑:
1、maven項目父pom有跳過測試的代碼 去掉<skipTest>就可以執行測試步驟了
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
2、有的人說jacoco只是分析了單元測試的代碼覆蓋率 並不能執行測試 需要在pom文件中添加junit插件纔可以 但是我實驗的結果是 只要加入了jacoco的插件就ok。而且單元測試的結果會直接生成xml 根本不需要此插件。
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>compile</scope>
</dependency>
jacoco插件
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.7.201606060606</version>
<configuration>
<!--指定生成.exec文件的存放位置-->
<destFile>target/coverage-reports/jacoco-unit.exec</destFile>
<!--Jacoco是根據.exec文件生成最終的報告,所以需指定.exec的存放路徑-->
<dataFile>target/coverage-reports/jacoco-unit.exec</dataFile>
</configuration>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
有人會遇到sonar中顯示了單元測試數量卻沒有覆蓋率的情況但是我發現只要加了jacoco插件後使用clean install 就會成功的