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