William

不管走了多远都不要忘记为什么出发

0%

sonarqube安装

版本

如果需要使用mysql作为数据库,sonarqueb版本的不能高于7.9及以上。

安装

1 JAVA与Mysql

使用sonarqube6.7.5版本,故安装jdk1.8即可

2 sonarqube安装

下载直接放到/usr/local/

环境变量和数据库配置

在.bash_profile文件中增加

1
2
export OCLINT_HOME=/usr/local/oclint-0.13
export PATH=$OCLINT_HOME/bin:$PATH

数据库设置:

1
2
3
4
5
CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci; 
CREATE USER 'sonar' IDENTIFIED BY 'sonar';
GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar';
GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar';
FLUSH PRIVILEGES;

配置sonarqube

配置sonar.properties
打开路径/usr/local/sonarqube-6.7.5/conf/sonar.properties
修改参数

1
2
3
4
5
6
7
8
9
10
11
12
sonar.jdbc.username=你的数据库账号(我的是root)
sonar.jdbc.password=你的数据库密码 (123)

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

完成后在目录/usr/local/sonarqube-6.7.5/bin/macosx-universal-64/sonar.sh start
完成启动后,打开浏览器访问http://localhost:9000
如果访问成功即完成安装,如果不成功查看log修正,log位置/usr/local/sonarqube-6.7.5/logs

如何扫描iOS代码库

在代码库的根目录增加sonar-project.propertiesrun-sonar.sh两个文件。内容示例如下:
sonar-project.properties

1
2
3
4
5
6
7
sonar.projectKey=wanpaniOS //sonar中显示的显示
sonar.projectName=netdiskiOS //同上即可
sonar.projectVersion=10.0.73 //sonar中显示的版本号
sonar.sources=/Users/william/netdisk/ios //项目的代码目录
sonar.objectivec.project=/Users/william/netdisk/ios/netdisk/netdisk_iphone/netdisk_iPhone.xcodeproj //xcodeproj的位置和名称
sonar.objectivec.workspace=netdisk_iPhone.xcworkspace //xcworkspace的位置和名称
sonar.objectivec.appScheme=netdisk_iPhone // scheme的名称

run-sonar.sh:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/bin/bash
## INSTALLATION: script to copy in your Xcode project in the same directory as the .xcodeproj file
## USAGE: ./run-sonar.sh
## DEBUG: ./run-sonar.sh -v
## WARNING: edit your project parameters in sonar-project.properties rather than modifying this script
#

trap "echo 'Script interrupted by Ctrl+C'; stopProgress; exit 1" SIGHUP SIGINT SIGTERM

function testIsInstalled() {

hash $1 2>/dev/null
if [ $? -eq 1 ]; then
echo >&2 "ERROR - $1 is not installed or not in your PATH"; exit 1;
fi
}

function readParameter() {

variable=$1
shift
parameter=$1
shift

eval $variable="\"$(sed '/^\#/d' sonar-project.properties | grep $parameter | tail -n 1 | cut -d '=' -f2- | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')\""
}

# Run a set of commands with logging and error handling
function runCommand() {

# 1st arg: redirect stdout
# 2nd arg: command to run
# 3rd..nth arg: args
redirect=$1
shift

command=$1
shift

if [ "$nflag" = "on" ]; then
# don't execute command, just echo it
echo
if [ "$redirect" = "/dev/stdout" ]; then
if [ "$vflag" = "on" ]; then
echo "+" $command "$@"
else
echo "+" $command "$@" "> /dev/null"
fi
elif [ "$redirect" != "no" ]; then
echo "+" $command "$@" "> $redirect"
else
echo "+" $command "$@"
fi

elif [ "$vflag" = "on" ]; then
echo

if [ "$redirect" = "/dev/stdout" ]; then
set -x #echo on
$command "$@"
returnValue=$?
set +x #echo off
elif [ "$redirect" != "no" ]; then
set -x #echo on
$command "$@" > $redirect
returnValue=$?
set +x #echo off
else
set -x #echo on
$command "$@"
returnValue=$?
set +x #echo off
fi

if [[ $returnValue != 0 && $returnValue != 5 ]] ; then
stopProgress
echo "ERROR - Command '$command $@' failed with error code: $returnValue"
exit $returnValue
fi
else
echo "--------------------------------"
echo $command
echo "$@"
if [ "$redirect" = "/dev/stdout" ]; then
$command "$@" > /dev/null
elif [ "$redirect" != "no" ]; then
$command "$@" > $redirect
else
$command "$@"
fi

returnValue=$?
if [[ $returnValue != 0 && $returnValue != 5 ]] ; then
stopProgress
echo "ERROR - Command '$command $@' failed with error code: $returnValue"
exit $?
fi


echo
fi
}

## COMMAND LINE OPTIONS
vflag=""
nflag=""
oclint="on"
while [ $# -gt 0 ]
do
case "$1" in
-v) vflag=on;;
-n) nflag=on;;
-nooclint) oclint="";;
--) shift; break;;
-*)
echo >&2 "Usage: $0 [-v]"
exit 1;;
*) break;; # terminate while loop
esac
shift
done

# Usage OK
echo "Running run-sonar.sh..."

# 检查依赖是否已经安装 xcpretty and oclint
echo "check xcodebuild, oclint installed is installed"
testIsInstalled xcodebuild
testIsInstalled xcpretty
testIsInstalled oclint

# 检查有没有 sonar-project.properties 文件
if [ ! -f sonar-project.properties ]; then
echo >&2 "ERROR - No sonar-project.properties in current directory"; exit 1;
fi

# 从 sonar-project.properties 读出参数

# .xcworkspacefilename
workspaceFile=''; readParameter workspaceFile 'sonar.objectivec.workspace'
# 源文件
srcDirs=''; readParameter srcDirs 'sonar.sources'
# Scheme
appScheme=''; readParameter appScheme 'sonar.objectivec.appScheme'


if [ "$vflag" = "on" ]; then
echo "Xcode workspace file is: $workspaceFile"
echo "Xcode application scheme is: $appScheme"
echo "Xcode sources is: $srcDirs"
fi

# 检查必须参数
if [ ! "$workspaceFile" != " " ]; then
echo >&2 "ERROR - sonar.objectivec.project parameter is missing in sonar-project.properties. You must specify which projects (comma-separated list) are application code within the workspace $workspaceFile."
exit 1
fi

if [ -z "$srcDirs" -o "$srcDirs" = " " ]; then
echo >&2 "ERROR - sonar.sources parameter is missing in sonar-project.properties. You must specify which directories contain your .h/.m source files (comma-separated list)."
exit 1
fi

if [ -z "$appScheme" -o "$appScheme" = " " ]; then
echo >&2 "ERROR - sonar.objectivec.appScheme parameter is missing in sonar-project.properties. You must specify which scheme is used to build your application."
exit 1
fi

## SCRIPT

# Create sonar-reports/ for reports output

if [[ ! (-d "sonar-reports") && ("$nflag" != "on") ]]; then
if [ "$vflag" = "on" ]; then
echo 'Creating directory sonar-reports/'
fi
mkdir sonar-reports
if [[ $? != 0 ]] ; then
stopProgress
exit $?
fi
fi


export LC_ALL="en_US.UTF-8"
if [[ "$workspaceFile" != "" ]] ; then
echo "xcodebuild clean"
xcodebuild clean -workspace "${workspaceFile}" -scheme "${appScheme}"
echo "xcodebuild analyze"
xcodebuild -workspace "${workspaceFile}" -scheme "${appScheme}" -configuration Debug analyze COMPILER_INDEX_STORE_ENABLE=NO | tee xcodebuild.log | xcpretty -r json-compilation-database
else
echo "no workspaceFile"
exit 1
fi

echo "mv compilation_db.json compile_commands.json"
mv ./build/reports/compilation_db.json ./compile_commands.json

echo "check folder existence"
if [ ! -d "build/sonar-reports" ]; then
mkdir -p build/sonar-reports
fi

echo "oclint-json-compilation-database"
oclint-json-compilation-database \
-v \
-- \
-report-type pmd -o build/sonar-reports/oclint.xml \
-max-priority-1=99999 -max-priority-2=99999 -max-priority-3=99999 \
-rc LONG_METHOD=300 \
-rc LONG_VARIABLE_NAME=50 \
-rc LONG_CLASS=3000 \
-rc NCSS_METHOD=300 \
-rc NESTED_BLOCK_DEPTH=8 \

echo "upload generated oclint report to sonar qube server"
sonar-scanner -X

echo "clean up"
rm -rf .scannerwork
rm -rf xcodebuild.log
rm -rf compile_commands.json
rm -rf build/sonar-reports/oclint.xml

exit 0

然后在项目的根目录中,直接运行run-sonar.sh

报错

1
2
ERROR: Error during SonarQube Scanner execution
ERROR: Failed to upload report - 500: An error has occurred. Please contact your administrator

查看web.log

1
Caused by: com.mysql.jdbc.PacketTooBigException: Packet for query is too large (54581962 > 20971520). You can change this value on the server by setting the max_allowed_packet' variable.

原因是mysql收到包超过了限制。
如下处理
修改/usr/local/etc/my.cnf中:增加max_allowed_packet = 1024M

1
2
[mysqld]
max_allowed_packet = 1024M

以及/usr/local/mysql-5.7.17-macos10.12-x86_64/support-files/my-default.cnf做上述相同修改。
重新启动mysql和sonarqube。
方法2:
进入数据库
mysql -u root -p
查看show VARIABLES like '%max_allowed_packet%';
图片
修改max_allowed_packet:
set global max_allowed_packet = 2048 * 1024 * 1024;
然后退出mysql,再进入。
重启sonarqube。
此种方案不能重启电脑,重启上述修改会失效。

如果对你有帮助,欢迎赞赏。