sonar代码检测插件

SonarQube®是一种自动代码审查工具,用于检测代码中的错误,漏洞和代码异味。它可以与您现有的工作流程集成,以便在项目分支和拉取请求之间进行连续的代码检查。

参考文章

基于Sonar的iOS代码质量检测系统
mac搭建SonarQube
iOS mac sonar安装指南
Objective C接入Sonar代码扫描

安装步骤

环境

演示版本:
1
2
3
4
5
JDK:1.8.0_221
MySQL:5.7.27
Sonarqube:7.1
Sonar-runner:2.4
Sonar-scanner:3.3.0.1492-macosx
Java环境与MySQL安装
  • 进入jdk1.8安装界面,下载macos版本
  • 进入MySQL下载界面,下载之后安装,安装完成后,进入系统偏好设置,可看到MySql选项,代表已安装成功
需要的插件

最新方法: sudo gem install xcpretty -n/usr/local/bin
旧方法:

1
2
3
4
5
git clone https://github.com/Backelite/xcpretty.git
cd xcpretty
git checkout fix/duration_of_failed_tests_workaround
gem build xcpretty.gemspec
sudo gem install --both xcpretty-0.2.2.gem -n/usr/local/bin

  • OCLint
1
2
brew tap oclint/formulae    
brew install oclint

检查路径:/usr/local/Cellar/oclint/0.13

  • slather
1
gem install slather
  • lizard
    • sudo pip install lizard
    • 若没有安装pip 下载文件 get-pip.py
    • 并执行sudo python get-pip.py
  • sonar-objective-c-plugin-0.5.0-SNAPSHOT.jar
    插件下载地址:https://github.com/ChenTF/iOS-sonarShell
    解压放在:sonarqube-7.1/extensions/plugins/
最后环境目录组成 /usr/local

sonar目录

创建MySQL数据库

1
2
3
4
5
6
7
8
cd /usr/local/mysql-5.7.27-macos10.14-x86_64/bin

mysql -u root -p

mysql> CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
mysql> CREATE USER 'sonar' IDENTIFIED BY 'sonar';
mysql> GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar';
mysql> GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar';
mysql> FLUSH PRIVILEGES;

修改配置文件

  • 修改confsonar.properties
1
2
3
4
5
6
vim /usr/local/sonarqube-7.1/confsonar.properties

sonar.host.url=http://localhost:9000/sonarqube
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
  • 修改wrapper.conf
1
2
vim /usr/local/sonarqube-7.1/wrapper.conf

wrapper.java.command=/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java
  • 修改sonar.properties
1
2
3
4
5
6
7
8
9
10
11
vim /usr/local/sonarqube-7.1/conf/sonar.properties

sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
sonar.jdbc.maxActive=60
sonar.jdbc.maxIdle=5
sonar.jdbc.minIdle=2
sonar.jdbc.maxWait=5000
sonar.jdbc.minEvictableIdleTimeMillis=600000
sonar.jdbc.timeBetweenEvictionRunsMillis=30000
  • 修改sonar-runner.properties
1
2
3
4
5
6
7
8
9
vim /usr/local/sonar-runner-2.4/conf/sonar-runner.properties

sonar.host.url=http://localhost:9000
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
sonar.sourceEncoding=UTF-8
sonar.login=admin
sonar.password=admin
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
  • 修改 sonar-scanner.properties
1
2
3
4
5
#----- Default SonarQube server
sonar.host.url=http://localhost:9000/sonarqube

#----- Default source code encoding
sonar.sourceEncoding=UTF-8

配置环境变量

1
2
3
4
5
6
7
8
9
10
11
vim ~/.bash_profile

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH:.
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:.
export OCLINT_HOME=/usr/local/Cellar/oclint/0.13
export PATH=$OCLINT_HOME/bin:$PATH

退出 :wq
保存 source ~/.bash_profile
验证 sonar-runner -version

注:检查JAVA_HOME 和 OCLINT_HOME 的路径是否正确 可能有不同

修改完成保存后,终端执行
cd /usr/local/sonarqube-7.1/bin/macosx-universal-64./sonar.sh start
打开浏览器访问http://localhost:9000 若正常进入则配置成功

安装中文插件

项目内需要的两个文件(根目录)

run-sonar.sh
sonar-project.properties(可修改对应的项目名称、版本号)

启动sonar
cd /usr/local/sonarqube-7.1/bin/macosx-universal-64./sonar.sh start
最后在项目根目录执行
./run-sonar.sh

安装执行过程中的命令

MySQL

查看数据库
SHOW DATABASES;
删除数据库
DROP DATABASE sonar;
查看账户
select user,host from user;
删除账户
delete from mysql.user where user='sonar'; 
启动
sudo /usr/local/mysql/support-files/mysql.server start
停止
sudo /usr/local/mysql/support-files/mysql.server stop
重启
sudo /usr/local/mysql/support-files/mysql.server restart

注: 完全卸载MAC上的mysql

sonarqube

cd /usr/local/sonarqube-7.1/bin/macosx-universal-64
启动
./sonar.sh start
停止
./sonar.sh stop
重启
./sonar.sh restart

安装执行过程中的问题


1
2
main ERROR Null object returned for RollingFile in Appenders.
main ERROR Unable to locate appender "file_es" for logger config "root"

解决:需要将配置文件log4j2.properties  中logger.deprecation.level=warn  改为 ERROR   ( 文件位置/user/local/sonarqube-7.1/temp)  若没有该设置 直接添加


1
WrapperSimpleApp: Encountered an error running main: java.nio.file.AccessDeniedException:

解决:删除 /user/local/sonarqube-7.1/temp 文件下所有内容即可


mac下安装mysql,连接出现Access denied for user ‘root‘@’localhost’ (using password: YES)
解决:https://blog.csdn.net/lyl123_456/article/details/82426212


1
ERROR: Re-run SonarQube Scanner using the -X switch to enable full debug logging.ERROR: SonarQube scanner exited with non-zero code: 1Finished: FAILURE

解决:问题解决


1
Another active Homebrew update process is already in progress. Please wait for it to finish or terminate it to continue.

解决: 执行 rm -rf /usr/local/var/homebrew/locks


扫描成功后最后查看网站,代码重复率有数字,其他代码质量为0
解决:查看project:sonar-project.properties文件,配置这2句 可以尝试。。

1
2
sonar.objectivec.oclint.report=oclint.xml(这个是报告的名字)
sonar.objectivec.oclint.reportPath=sonar-reports/oclint.xml(这个是报告的路径)

1
ERROR 1046 (3D000): No database selected

解决: mysql> use mysql


1
ERROR 1396 (HY000): Operation CREATE USER failed for 'sonar'@'%'

解决:换个名称CREATE USER 'sonarqube' IDENTIFIED BY 'sonarqube';


1
java.io.FileNotFoundException: /usr/local/sonarqube-7.1/extensions/plugins/sonar-objective-c-plugin-0.5.0-SNAPSHOT.jar (No such file or directory)

解决:冲突 去掉对应的文件 重新运行sonar


1
java.lang.RuntimeException: can not run elasticsearch as root

解决:运行命令将sudo /usr/local//sonarqube-7.1/bin/macosx-universal-64/sonar.sh start 修改为 cd /usr/local//sonarqube-7.1/bin/macosx-universal-64  和  ./sonar.sh start


1
若项目根目录 执行 ./run-sonar.sh 出错 -bash: ./run-sonar.sh: Permission denied  

解决:chmod u=rxw run-sonar.sh 修改权限


1
error: one compiler command contains multiple jobs:  

解决:
• 将 Project 和 Targets 中 Building Settings 下的 COMPILER_INDEX_STORE_ENABLE 设置为 NO
• 在 podfile 中 target ‘xx’ do 前面添加下面的脚本

1
2
3
4
5
6
7
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['COMPILER_INDEX_STORE_ENABLE'] = "NO"
end
end
end