sonar.jdbc.username=root sonar.jdbc.password=123456 sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
3、啟動SonarQube
進入到bin目錄下,執行: ./sonar.sh start
4、訪問sonarqube:localhost:9000
新建項目,如圖點擊加號,然后點擊創建項目:
輸入項目唯一標識,任意字符,只要能標識唯一項目即可,然后點擊設置按鈕,如圖:
創建令牌,用於執行掃描的鑒權使用,如圖:
選擇要分析的語言,及編譯方式,如圖:最終會把需要執行檢查的命令顯示出來,執行在項目根目錄下執行即可
5、下載sonar-scanner:https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/
6、配置mysql:conf目錄下的sonar-scanner.properties文件
#----- Default SonarQube server #sonar.host.url=http://localhost:9000 #----- Default source code encoding #sonar.sourceEncoding=UTF-8 sonar.jdbc.username=root sonar.jdbc.password=123456 sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
7、在需要進行檢查的項目根目錄下:
- 新增文件:sonar-project.properties
- 新增內容
# must be unique in a given SonarQube instance sonar.projectKey=testproject # this is the name displayed in the SonarQube UI sonar.projectName=testproject 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. sonar.sources=src/main/java sonar.java.binaries=./target/classes # Encoding of the source code. Default is default system encoding #sonar.sourceEncoding=UTF-8
備注:projectName是項目名字,sources是源文件所在的目錄,sonar.java.binaries是項目編譯后的class文件的目錄。
8、執行sonar檢查:
在項目根目錄下執行:sonar-scanner
9、sonarqube漢化
1、git地址下載對應版本的Jar:https://github.com/SonarQubeCommunity/sonar-l10n-zh/releases 2、下載之后放到sonar\sonarqube-6.7.6\extensions\plugins目錄下面 3、重啟:執行./sonar.sh restart
10、sonar指定分支掃描
插件與sonar版本對應關系:
1、https://github.com/mc1arke/sonarqube-community-branch-plugin 2、對應不同版本的插件下載地址:https://github.com/mc1arke/sonarqube-community-branch-plugin/releases 3、項目下載地址:git clone git@github.com:mc1arke/sonarqube-community-branch-plugin.git ,自行編譯打包 ,在項目內執行./gradlew clean build
。在libs目錄下生成sonarqube-community-branch-plugin*.jar
4、下載插件放到${SONAR_HOME}/extensions/plugins目錄下,重啟 Sonar。 5、掃描時,增加-Dsonar.branch.name=${GIT_BRANCH}即可。
指定分支掃描:
1、mvn clean verify sonar:sonar -Dmaven.test.skip=true -Dsonar.branch=master 2、mvn clean verify sonar:sonar -Dmaven.test.skip=true -Dsonar.projectName=${JOB_NAME} -Dsonar.projectKey=${JOB_NAME}
11、sonar規則開發
官方提供示例:
https://github.com/SonarSource/sonar-custom-plugin-example 項目下載地址:git@github.com:SonarSource/sonar-custom-plugin-example.git
12、單元測試覆蓋率的生成
方式1:在pom.xml中配置,只需在pom.xml文件中加上Jacoco插件,以后每次執行mvn install即可生成代碼覆蓋率數據:
<plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.7.8</version> <executions> <execution> <goals> <goal>prepare-agent</goal> <goal>report</goal> </goals> </execution> </executions> </plugin>
13、同步單測覆蓋率結果至sonarqube
<profile> <id>sonar</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <sonar.jdbc.url> jdbc:mysql://127.0.0.1:3306/sonar?useUnicode=true&characterEncoding=utf8 </sonar.jdbc.url> <sonar.jdbc.driver>com.mysql.jdbc.Driver</sonar.jdbc.driver> <sonar.host.url>http://127.0.0.1:9000</sonar.host.url> <sonar.login>admin</sonar.login> <sonar.password>admin</sonar.password> </properties> </profile>
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>sonar-maven-plugin</artifactId> <version>2.6</version> </plugin>
該命令會先執行靜態掃描,並將掃描的結果和代碼覆蓋率數據(即jacoco.exec)一起上傳至SonarQube平台。
14、不增加jacoco依賴配置的方式
也可以執行如下兩個步驟:
1、mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install -Dmaven.test.failure.ignore=true 2、mvn sonar:sonar
說明:
方式1:sonar-scanner,會使用項目根目錄下的sonar-project.properties配置文件。 方式2:mvn sonar:sonar -Dsonar.projectKey=jacoco -Dsonar.host.url=http://localhost:9000 ,后面可以接參數指定,沒有則走settings.xml配置文件
備注:命令后面接參數的時候使用-D, 例如:-Dsonar.projectKey