前提: 1,SonarQube已經安裝好且已經安裝了sonar-php-plugin,而且在測試服務器上也安裝並配置好了sonar-scanner 2,安裝了Maven 這個很是簡單,直接從官網上下載Maven的zip包,解開而後配置下面這個配置文件便可php
export MAVEN_HOME=/usr/local/maven333 export PATH=$MAVEN_HOME/bin:$PATH
一,Maven簡單項目java
配置pom.xml,使之產生Junit單元測試報告linux
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.19.1</version> <configuration> <skip>false</skip> <!--<reportsDirectory>${project.basedir}/target/junit</reportsDirectory> --> </configuration> </plugin> </plugins> </build>
默認就是產生在target目錄下面的surefire-reports目錄裏,因此不須要配置reportsDirectory。spring
配置pom.xml ,使之產生代碼測試覆蓋率報告,在這裏使用Jacoco工具來產生。apache
<build> <plugins> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.7.2.201409121644</version> <executions> <execution> <id>default-prepare-agent</id> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>default-report</id> <phase>prepare-package</phase> <goals> <goal>report</goal> </goals> </execution> <execution> <id>default-check</id> <goals> <goal>check</goal> </goals> <configuration> <rules> <!-- implementation is needed only for Maven 2 --> <rule implementation ="org.jacoco.maven.RuleConfiguration" > <element>BUNDLE</element> <limits> <!-- implementation is needed only for Maven 2 --> <limit implementation ="org.jacoco.report.check.Limit" > <counter>COMPLEXITY</counter> <value>COVEREDRATIO</value> <minimum>0.60</minimum> </limit> </limits> </rule> </rules> </configuration> </execution> </executions> </plugin> </plugins> </build>
默認就產生在target目錄下,覆蓋率報告文件名爲jacoco.execapi
在項目的根目錄下建立一個配置文件sonar-project.properties服務器
sonar.projectKey=org.sonarqube:parent sonar.projectName=Java :: JaCoco Multi Modules :: Parent sonar.projectVersion=1.0-SNAPSHOT sonar.sources=src/main/java sonar.tests=src/test/java sonar.binaries=target/classes sonar.language=java #Tells SonarQube where the unit tests execution reports are sonar.junit.reportsPath=target/surefire-reports #Tells SonarQube where the integration tests code coverage report is #sonar.jacoco.itReportPath=target/jacoco-it.exec sonar.jacoco.reportPath=target/jacoco.exec # Encoding of the source files sonar.sourceEncoding=UTF-8
注意,必定要配置這個sonar.binaries,不然會報: INFO: No JaCoCo analysis of project coverage can be done since there is no class files.maven
另外,配置sonar.jacoco.itReportPath或sonar.jacoco.reportPath視產生的覆蓋率報告文件而定工具
二,Maven多模塊項目單元測試
配置pom.xml,使之產生Junit單元測試報告,同「Maven簡單項目 」的配置方法。
配置pom.xml ,使之產生代碼測試覆蓋率報告 ,同「Maven簡單項目 」的配置方法。
在項目的根目錄下建立一個配置文件sonar-project.properties
sonar.projectKey=com.dangdang:elastic-job sonar.projectName=elastic-job sonar.projectVersion=1.1.0 # Set modules IDs sonar.modules=elastic-job-api-core,elastic-job-console,elastic-job-core,elastic-job-spring # Modules inherit properties set at parent level sonar.sources=src/main/java sonar.tests=src/test/java sonar.java.binaries=target/classes sonar.language=java #Tells SonarQube where the unit tests execution reports are sonar.junit.reportsPath=target/surefire-reports #Tells SonarQube where the integration tests code coverage report is sonar.jacoco.reportPath=target/jacoco.exec # Encoding of the source files sonar.sourceEncoding=UTF-8
elastic-job-api-core.sonar.projectBaseDir=elastic-job-api/elastic-job-api-core
這裏,須要注意的就是配置sonar.modules,這樣下面的sonar.sources,sonar.tests等配置項就是指各個模塊下面的相對目錄。
指定代碼的目錄的目的是爲了把代碼文件上傳到Sonar服務器上。
模塊必須是非父模塊,也就是再也不包含子模塊了,只有這樣,統計代碼質量纔有意義。
在這裏,elastic-job-api-core這個子項目的目錄不是在當前目錄下面,而是又深刻了一級目錄,就能夠使用elastic-job-api-core.sonar.projectBaseDir來指定這個完整的相對路徑。
三,測試步驟都同樣:
1,在頂級目錄下執行
mvn clean test
這時候就會在頂級目錄下獲得一個文件夾reports,裏面有兩個測試報文文件,分別是測試報告和測試覆蓋率報告
2,在頂級目錄下執行如下命令,就會將兩個測試報告文件和src和tests目錄下的文件都提交給SonarQube服務器(前提是Sonar已經都配置好了) (D:\sonar-scanner-2.6.1\bin\sonar-scanner.bat這個路徑或者在linux就放入PATH環境變量中)
sonar-scanner