sonarqube linux 安装以及简单使用

sonarqube是代码质量审查工具,具体就不介绍了

安装

准备工作
1.jdk安装
下载

http://www.oracle.com/technetwork/cn/java/archive-139210-zhs.html

下载后解压

-rw-r--r-- 1 root root 185515842 May 25 18:45 jdk-8u144-linux-x64 (2).tar.gz
tar -zxvf jdk-8u144-linux-x64\ \(2\).tar.gz

设置JAVA_HOME

[root@i-vzdytl5t ~]# mkdir /opt/java
[root@i-vzdytl5t ~]# cp -r jdk1.8.0_144 /opt/java/

export JAVA_HOME=/opt/java/jdk1.8.0_144
export CLASSPATH=.:${JAVA_HOME}/jre/lib/rt.jar:${JAVA_HOME}/lib/dt.jar:${JAVA_HOME}/lib/tools.jar
export PATH=$PATH:${JAVA_HOME}/bin

2.下载sonarqube以及解压

wget https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-7.2.zip
unzip sonarqube-7.2.zip 
[root@i-vzdytl5t sonarqube-7.2]# ls -l
total 44
drwxr-xr-x 8 root root 4096 Jun 18 09:31 bin
drwxr-xr-x 2 root root 4096 Jun 18 09:31 conf
-rw-r--r-- 1 root root 7651 Jun 18 09:31 COPYING
drwxr-xr-x 2 root root 4096 Jun 18 09:31 data
drwxr-xr-x 7 root root 4096 Jun 18 09:31 elasticsearch
drwxr-xr-x 4 root root 4096 Jun 18 09:31 extensions
drwxr-xr-x 6 root root 4096 Jun 18 09:35 lib
drwxr-xr-x 2 root root 4096 Jun 18 09:31 logs
drwxr-xr-x 2 root root 4096 Jun 18 09:31 temp
drwxr-xr-x 7 root root 4096 Jun 18 09:35 web
[root@i-vzdytl5t sonarqube-7.2]# pwd
/root/sonarqube/sonarqube-7.2

启动sonarqube

[root@i-vzdytl5t linux-x86-64]# ./sonar.sh console
Running SonarQube...
wrapper  | --> Wrapper Started as Console
wrapper  | Launching a JVM...
jvm 1    | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
jvm 1    |   Copyright 1999-2006 Tanuki Software, Inc.  All Rights Reserved.
jvm 1    | 
jvm 1    | 2018.06.23 23:41:55 INFO  app[][o.s.a.AppFileSystem] Cleaning or creating temp directory /root/sonarqube/sonarqube-7.2/temp
jvm 1    | 2018.06.23 23:41:55 INFO  app[][o.s.a.es.EsSettings] Elasticsearch listening on /127.0.0.1:9001
jvm 1    | 2018.06.23 23:41:55 INFO  app[][o.s.a.p.ProcessLauncherImpl] Launch process[[key='es', ipcIndex=1, logFilenamePrefix=es]] from [/root/sonarqube/sonarqube-7.2/elasticsearch]: /root/sonarqube/sonarqube-7.2/elasticsearch/bin/elasticsearch -Epath.conf=/root/sonarqube/sonarqube-7.2/temp/conf/es
jvm 1    | 2018.06.23 23:41:55 INFO  app[][o.s.a.SchedulerImpl] Waiting for Elasticsearch to be up and running
jvm 1    | 2018.06.23 23:41:55 INFO  app[][o.e.p.PluginsService] no modules loaded
jvm 1    | 2018.06.23 23:41:55 INFO  app[][o.e.p.PluginsService] loaded plugin [org.elasticsearch.transport.Netty4Plugin]
jvm 1    | 2018.06.23 23:41:56 WARN  app[][o.s.a.p.AbstractProcessMonitor] Process exited with exit value [es]: 1
jvm 1    | 2018.06.23 23:41:56 INFO  app[][o.s.a.SchedulerImpl] Process [es] is stopped
jvm 1    | 2018.06.23 23:41:56 INFO  app[][o.s.a.SchedulerImpl] SonarQube is stopped

原因是不能用root用户启动es
创建sonarUser用户并赋权

[root@i-vzdytl5t linux-x86-64]# adduser sonarUser  
[root@i-vzdytl5t linux-x86-64]# passwd sonarUser
Changing password for user sonarUser.
New password: 
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@i-vzdytl5t sonarqube]# chown -R sonarUser:sonarUser sonarqube-7.2
[root@i-vzdytl5t linux-x86-64]# mkdir /opt/snoar

[root@i-vzdytl5t sonarqube]# mv sonarqube-7.2 /opt/snoar/
[root@i-vzdytl5t sonarqube]# cd /opt/snoar/
[root@i-vzdytl5t snoar]# ls -l
total 4
drwxr-xr-x 11 sonarUser sonarUser 4096 Jun 18 09:35 sonarqube-7.2
[root@i-vzdytl5t snoar]# cd sonarqube-7.2/
[root@i-vzdytl5t sonarqube-7.2]# ls -l
total 44
drwxr-xr-x 8 sonarUser sonarUser 4096 Jun 18 09:31 bin
drwxr-xr-x 2 sonarUser sonarUser 4096 Jun 18 09:31 conf
-rw-r--r-- 1 sonarUser sonarUser 7651 Jun 18 09:31 COPYING
drwxr-xr-x 2 sonarUser sonarUser 4096 Jun 18 09:31 data
drwxr-xr-x 7 sonarUser sonarUser 4096 Jun 18 09:31 elasticsearch
drwxr-xr-x 4 sonarUser sonarUser 4096 Jun 18 09:31 extensions
drwxr-xr-x 6 sonarUser sonarUser 4096 Jun 18 09:35 lib
drwxr-xr-x 2 sonarUser sonarUser 4096 Jun 23 23:24 logs
drwxr-xr-x 3 sonarUser sonarUser 4096 Jun 23 23:41 temp
drwxr-xr-x 7 sonarUser sonarUser 4096 Jun 18 09:35 web

再次启动sonarqube

[root@i-vzdytl5t java]# chown -R sonarUser:sonarUser jdk1.8.0_144
export JAVA_HOME=/opt/java/jdk1.8.0_144
export CLASSPATH=.:${JAVA_HOME}/jre/lib/rt.jar:${JAVA_HOME}/lib/dt.jar:${JAVA_HOME}/lib/tools.jar
export PATH=$PATH:${JAVA_HOME}/bin

[root@i-vzdytl5t linux-x86-64]# su sonarUser
[sonarUser@i-vzdytl5t linux-x86-64]$ ./sonar.sh console
Running SonarQube...
wrapper  | --> Wrapper Started as Console
wrapper  | Launching a JVM...
jvm 1    | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
jvm 1    |   Copyright 1999-2006 Tanuki Software, Inc.  All Rights Reserved.
jvm 1    | 
jvm 1    | 2018.06.23 23:55:31 INFO  app[][o.s.a.AppFileSystem] Cleaning or creating temp directory /opt/snoar/sonarqube-7.2/temp
jvm 1    | 2018.06.23 23:55:31 INFO  app[][o.s.a.es.EsSettings] Elasticsearch listening on /127.0.0.1:9001
jvm 1    | 2018.06.23 23:55:31 INFO  app[][o.s.a.p.ProcessLauncherImpl] Launch process[[key='es', ipcIndex=1, logFilenamePrefix=es]] from [/opt/snoar/sonarqube-7.2/elasticsearch]: /opt/snoar/sonarqube-7.2/elasticsearch/bin/elasticsearch -Epath.conf=/opt/snoar/sonarqube-7.2/temp/conf/es
jvm 1    | 2018.06.23 23:55:31 INFO  app[][o.s.a.SchedulerImpl] Waiting for Elasticsearch to be up and running
jvm 1    | 2018.06.23 23:55:31 INFO  app[][o.e.p.PluginsService] no modules loaded
jvm 1    | 2018.06.23 23:55:31 INFO  app[][o.e.p.PluginsService] loaded plugin [org.elasticsearch.transport.Netty4Plugin]
jvm 1    | 2018.06.23 23:55:39 INFO  app[][o.s.a.SchedulerImpl] Process[es] is up
jvm 1    | 2018.06.23 23:55:39 INFO  app[][o.s.a.p.ProcessLauncherImpl] Launch process[[key='web', ipcIndex=2, logFilenamePrefix=web]] from [/opt/snoar/sonarqube-7.2]: /opt/java/jdk1.8.0_144/jre/bin/java -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djava.io.tmpdir=/opt/snoar/sonarqube-7.2/temp -Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError -cp ./lib/common/*:/opt/snoar/sonarqube-7.2/lib/jdbc/h2/h2-1.3.176.jar org.sonar.server.app.WebServer /opt/snoar/sonarqube-7.2/temp/sq-process2095846763000389244properties

//后台启动
[sonarUser@i-vzdytl5t linux-x86-64]$ ./sonar.sh start
Starting SonarQube...
Started SonarQube.

[sonarUser@i-vzdytl5t linux-x86-64]$ netstat -tlnp 
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:32000         0.0.0.0:*               LISTEN      14232/java          
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -                   
tcp6       0      0 127.0.0.1:43168         :::*                    LISTEN      -                   
tcp6       0      0 127.0.0.1:9092          :::*                    LISTEN      14393/java          
tcp6       0      0 127.0.0.1:36198         :::*                    LISTEN      -                   
tcp6       0      0 :::9000                 :::*                    LISTEN      14393/java          
tcp6       0      0 127.0.0.1:9001          :::*                    LISTEN      14257/java          
tcp6       0      0 :::6123                 :::*                    LISTEN      -                   
tcp6       0      0 :::39884                :::*                    LISTEN      -                   
tcp6       0      0 :::8080                 :::*                    LISTEN      -                   
tcp6       0      0 :::8081                 :::*                    LISTEN      -                   
tcp6       0      0 :::22                   :::*                    LISTEN      -                   
tcp6       0      0 :::3000                 :::*                    LISTEN      -    

启动成功,访问9000端口

sonarqube linux 安装以及简单使用_第1张图片

配置MySql数据库

1.安装mysql数据库 请参考https://www.cnblogs.com/bigbrotherer/p/7241845.html
2.打开sonarqube安装目录下的/opt/snoar/sonarqube-7.2/confsonar.properties文件

# User credentials.
# Permissions to create tables, indices and triggers must be granted to JDBC user.
# The schema must be created first.
sonar.jdbc.username=root
sonar.jdbc.password=qinzhao

#----- Embedded Database (default)
# H2 embedded database server listening port, defaults to 9092
#sonar.embeddedDatabase.port=9092

#----- DEPRECATED
#----- MySQL >=5.6 && <8.0
# Support of MySQL is dropped in Data Center Editions and deprecated in all other editions
# Only InnoDB storage engine is supported (not myISAM).
# Only the bundled driver is supported. It can not be changed.
sonar.jdbc.url=jdbc:mysql://127.0.0.1:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
[root@i-vzdytl5t linux-x86-64]# mysql -u root -pqinzhao
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.22 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create create database sonar;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'create database sonar' at line 1
mysql> create database sonar;
Query OK, 1 row affected (0.00 sec)

数据库初始化成功后,登录;默认用户名和密码是admin;admin
登录成功页面

设置token

qinzhao: bd0aca6726892046521d2ba458df99f3f47c305d

汉化
sonarqube linux 安装以及简单使用_第2张图片
重启
sonarqube linux 安装以及简单使用_第3张图片

汉化成功
sonarqube linux 安装以及简单使用_第4张图片

linux安装SonarQube+Scanner

1.下载,我下载的是:https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/sonar-scanner-2.8.zip

2.解压到目录(可解压到任意目录)。修改系统路 径 path,如下:
2.1.切换到root用户
2.2.通过修改profile文件:

[root@i-vzdytl5t sonar-scanner-2.8]# pwd
/opt/snoar/sonarqube-7.2/bin/linux-x86-64/sonar-scanner-2.8
vi /etc/profile

#sonar-scanner profile   
export  
 SONAR_SCANNER_HOME=/opt/snoar/sonarqube-7.2/bin/linux-x86-64/sonar-scanner-2.8  
export  
 PATH=${SONAR_SCANNER_HOME}/bin:${PATH}
source /etc/profile

验证安装是否成功

[root@i-vzdytl5t sonar-scanner-2.8]# sonar-runner -v 
WARN: sonar-runner script is deprecated. Please use sonar-scanner instead.
INFO: Scanner configuration file: /opt/snoar/sonarqube-7.2/bin/linux-x86-64/sonar-scanner-2.8/conf/sonar-scanner.properties
INFO: Project root configuration file: NONE
INFO: SonarQube Scanner 2.8
INFO: Java 1.8.0_144 Oracle Corporation (64-bit)
INFO: Linux 3.10.0-862.3.2.el7.x86_64 amd64

使用

参考https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner

下载代码到本地

[root@i-vzdytl5t opt]# git clone https://source.enncloud.cn/qinzhao/spring-boot-demo.git
Cloning into 'spring-boot-demo'...
remote: Counting objects: 39, done.
remote: Compressing objects: 100% (24/24), done.
remote: Total 39 (delta 3), reused 0 (delta 0)
Unpacking objects: 100% (39/39), done.
[root@i-vzdytl5t opt]# ls -l
total 28
drwxrwxr-x 10      1005      1006 4096 Aug  3  2017 flink-1.3.2
drwxr-xr-x  3 root      root      4096 Jun 23 23:49 java
drwxr-xr-x  3 root      root      4096 Jun 23 11:10 jenkins
drwxr-xr-x  3 root      root      4096 Jun 23 17:21 jenkinsci
drwxr-xr-x 19 root      root      4096 Jun 23 20:07 jenkins-data
drwxr-xr-x  3 sonarUser sonarUser 4096 Jun 23 23:47 snoar
drwxr-xr-x  4 root      root      4096 Jun 24 08:48 spring-boot-demo

[root@i-vzdytl5t opt]# cd spring-boot-demo/
[root@i-vzdytl5t spring-boot-demo]# pwd
/opt/spring-boot-demo

1.打开要进行代码分析的项目根目录,新建sonar-project.properties文件

[root@i-vzdytl5t spring-boot-demo]# cat sonar-project.properties 
# must be unique in a given SonarQube instance
sonar.projectKey=spring-boot-demo
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=spring-boot-demo
sonar.projectVersion=master

# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# This property is optional if sonar.modules is set. 
sonar.sources=./
sonar.language=java

# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8
# Additional parameters  
sonar.my.property=value  
sonar.java.binaries=/opt/spring-boot-demo/target/classes #编译的完成的,参考https://docs.sonarqube.org/display/PLUG/Java+Plugin+and+Bytecode

2.执行分析sonar-runner
执行sonar-runner命令时 如果出现一下错误,

ERROR: Error during SonarQube Scanner execution 
org.sonar.squidbridge.api.AnalysisException: Please provide compiled classes of your project with sonar.java.binaries property 

请参考相关博客解决新版sonar-java插件需要配置sonar.java.binaries参数的问题
解决办法

[root@i-vzdytl5t extensions]# cd plugins/
[root@i-vzdytl5t plugins]# pwd
/opt/snoar/sonarqube-7.2/extensions/plugins
[root@i-vzdytl5t plugins]# wget https://sonarsource.bintray.com/Distribution/sonar-java-plugin/sonar-java-plugin-4.10.0.10260.jar

[root@i-vzdytl5t plugins]# ls -l
total 40644
-rw-r--r-- 1 sonarUser sonarUser     224 Jun 18 09:31 README.txt
-rw-r--r-- 1 sonarUser sonarUser 1563976 Jun 14 17:29 sonar-csharp-plugin-7.2.0.5463.jar
-rw-r--r-- 1 sonarUser sonarUser 1551459 Jun 14 16:26 sonar-flex-plugin-2.4.0.1222.jar
-rw-r--r-- 1 sonarUser sonarUser 3904078 Jun 15 14:50 sonar-go-plugin-1.1.0.1612.jar
-rw-r--r-- 1 root      root      4779909 May 29  2017 sonar-java-plugin-4.10.0.10260.jar
-rw-r--r-- 1 sonarUser sonarUser 8181910 Jun 14 17:29 sonar-java-plugin-5.4.0.14284.jar
-rw-r--r-- 1 sonarUser sonarUser 3405969 Jun 14 16:26 sonar-javascript-plugin-4.1.0.6085.jar
-rw-r--r-- 1 sonarUser sonarUser   40663 Jun 24 00:50 sonar-l10n-zh-plugin-1.21.jar
-rw-r--r-- 1 sonarUser sonarUser  300503 Jun 14 17:29 sonar-ldap-plugin-2.2.0.608.jar
-rw-r--r-- 1 sonarUser sonarUser 2550393 Jun 14 16:26 sonar-php-plugin-2.13.0.3107.jar
-rw-r--r-- 1 sonarUser sonarUser 1562193 Jun 14 16:26 sonar-python-plugin-1.10.0.2131.jar
-rw-r--r-- 1 sonarUser sonarUser 2650234 Jun 14 17:29 sonar-scm-git-plugin-1.4.1.1128.jar
-rw-r--r-- 1 sonarUser sonarUser 7213239 Jun 14 16:26 sonar-scm-svn-plugin-1.7.0.1017.jar
-rw-r--r-- 1 sonarUser sonarUser 2098783 Jun 15 14:50 sonar-typescript-plugin-1.7.0.2893.jar
-rw-r--r-- 1 sonarUser sonarUser 1783028 Jun 14 17:29 sonar-xml-plugin-1.5.1.1452.jar
[root@i-vzdytl5t plugins]# mv sonar-java-plugin-5.4.0.14284.jar sonar-java-plugin-5.4.0.14284.jar.bak
[root@i-vzdytl5t plugins]# ls -l
total 40644
-rw-r--r-- 1 sonarUser sonarUser     224 Jun 18 09:31 README.txt
-rw-r--r-- 1 sonarUser sonarUser 1563976 Jun 14 17:29 sonar-csharp-plugin-7.2.0.5463.jar
-rw-r--r-- 1 sonarUser sonarUser 1551459 Jun 14 16:26 sonar-flex-plugin-2.4.0.1222.jar
-rw-r--r-- 1 sonarUser sonarUser 3904078 Jun 15 14:50 sonar-go-plugin-1.1.0.1612.jar
-rw-r--r-- 1 root      root      4779909 May 29  2017 sonar-java-plugin-4.10.0.10260.jar
-rw-r--r-- 1 sonarUser sonarUser 8181910 Jun 14 17:29 sonar-java-plugin-5.4.0.14284.jar.bak
-rw-r--r-- 1 sonarUser sonarUser 3405969 Jun 14 16:26 sonar-javascript-plugin-4.1.0.6085.jar
-rw-r--r-- 1 sonarUser sonarUser   40663 Jun 24 00:50 sonar-l10n-zh-plugin-1.21.jar
-rw-r--r-- 1 sonarUser sonarUser  300503 Jun 14 17:29 sonar-ldap-plugin-2.2.0.608.jar
-rw-r--r-- 1 sonarUser sonarUser 2550393 Jun 14 16:26 sonar-php-plugin-2.13.0.3107.jar
-rw-r--r-- 1 sonarUser sonarUser 1562193 Jun 14 16:26 sonar-python-plugin-1.10.0.2131.jar
-rw-r--r-- 1 sonarUser sonarUser 2650234 Jun 14 17:29 sonar-scm-git-plugin-1.4.1.1128.jar
-rw-r--r-- 1 sonarUser sonarUser 7213239 Jun 14 16:26 sonar-scm-svn-plugin-1.7.0.1017.jar
-rw-r--r-- 1 sonarUser sonarUser 2098783 Jun 15 14:50 sonar-typescript-plugin-1.7.0.2893.jar
-rw-r--r-- 1 sonarUser sonarUser 1783028 Jun 14 17:29 sonar-xml-plugin-1.5.1.1452.jar
[root@i-vzdytl5t plugins]# pwd
/opt/snoar/sonarqube-7.2/extensions/plugins
[root@i-vzdytl5t plugins]# export JAVA_HOME=/opt/java/jdk1.8.0_144
[root@i-vzdytl5t plugins]# export CLASSPATH=.:${JAVA_HOME}/jre/lib/rt.jar:${JAVA_HOME}/lib/dt.jar:${JAVA_HOME}/lib/tools.jar
[root@i-vzdytl5t plugins]# export PATH=$PATH:${JAVA_HOME}/bin
[root@i-vzdytl5t plugins]# chown -R sonarUser:sonarUser sonar-java-plugin-4.10.0.10260.jar

问题解决

[root@i-vzdytl5t spring-boot-demo]# sonar-runner 
WARN: sonar-runner script is deprecated. Please use sonar-scanner instead.
INFO: Scanner configuration file: /opt/snoar/sonarqube-7.2/bin/linux-x86-64/sonar-scanner-2.8/conf/sonar-scanner.properties
INFO: Project root configuration file: /opt/spring-boot-demo/sonar-project.properties
INFO: SonarQube Scanner 2.8
INFO: Java 1.8.0_144 Oracle Corporation (64-bit)
INFO: Linux 3.10.0-862.3.2.el7.x86_64 amd64
INFO: User cache: /root/.sonar/cache
INFO: Publish mode
INFO: Load global settings
INFO: Load global settings (done) | time=84ms
INFO: Server id: AWQth_mHvVbNs4eeWXBd
...
...
INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
INFO: More about the report processing at http://localhost:9000/api/ce/task?id=AWQvY-VXYJqwJEgUPdw-
INFO: Task total time: 5.776 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 6.959s
INFO: Final Memory: 53M/242M
INFO: ------------------------------------------------------------------------

出现以上结果就表示分析成功了
打开您安装sonarqube服务,http://ip:9000,就可以看到相关的分析结果了
sonarqube linux 安装以及简单使用_第5张图片

查看分析结果
sonarqube linux 安装以及简单使用_第6张图片

sonarqube linux 安装以及简单使用_第7张图片

end
参考:
linux安装sonarQube的安装、配置与使用
sonarqube
Get+Started+in+Two+Minutes
持续集成 SonarQube 分享 (一):介绍与安装
ldap对接

你可能感兴趣的:(snoarqube)