SonarQube 代码质量检测
  DcpJeOZ6VzTX 2023年11月13日 36 0

介绍 主要检测,代码规范,代码错误,代码漏洞。

  1. SonarQube基础java开发,需安装open JDK8版本
  2. SonarQube需要依赖MySQL数据库,至少5.6版本以上
  3. SonarQube的小型实例至少4G内存,如果大型实例需要16G内

在7.6以后版本,需要安装JDK11,并且不再支持MySQL需要使用PostgreSQL

在10.1以后版本,server需要安装JDK17,并且不再支持MySQL需要使用PostgreSQL

老版本7.6

安装JDK

安装MySQL数据库5.7(sonar7.6版本)

# 安装依赖
yum install -y libaio-devel autoconf

# 下载MySQL

# 解压
tar xf mysql-5.7.42-linux-glibc2.12-x86_64.tar.gz

# 创建app目录
mkdir /app

# 移动目录
mv /root/mysql-5.7.42-linux-glibc2.12-x86_64 /app/mysql-5.7.42

# 做软连接
ln -s /app/mysql-5.7.42/ /app/mysql

# 创建MySQL用户
useradd mysql -s /sbin/nologin -M

# 进入初始化脚本目录
cd /app/mysql/bin/

# 初始化
./mysqld --initialize-insecure --user=mysql --basedir=/app/mysql --datadir=/app/mysql/data

# 拷贝启动脚本
cp /app/mysql/support-files/mysql.server /etc/init.d/mysqld

# 编辑MySQL配置文件(删除多余的)
vim /etc/my.cnf
[mysqld]
basedir=/app/mysql
datadir=/app/mysql/data

# 启动数据库
[root@db04 ~]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS! 

# 进入数据库
[root@db04 ~]# /app/mysql/bin/mysql
-#环境变量
添加环境变量后只需要用mysql就行
echo 'PATH="/app/mysql/bin:$PATH"' > /etc/profile.d/mysql57.sh
生效环境变量source /etc/profile



# 启动MySQL用systemd脚本
[root@db02]# cd /usr/lib/systemd/system
[root@db02 system]# vim /usr/lib/systemd/system/mysqld.service

[Unit]
Description=mysqld

[Service]
#Type=notify
ExecStart=/app/mysql/bin/mysqld --defaults-file=/etc/my.cnf --user=mysql
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target

##先 systemctl daemon reload(5.6二进制需要,其他直接启动)
##再 systemctl start mysqld

##加入开机自启 systemctl enable mysqld

# 7.创建用户
CREATE USER 'sonar_user' IDENTIFIED BY '123';
# 8.创建数据库
create database sonar_db;
# 9.授权
grant all on database sonar_db to sonar_user;
GRANT
# 10.测试连接数据库
[root@web01 ~]# psql -d sonar_db -U sonar_user -h127.0.0.1

安装sonar7.6

# 安装sonarqube
wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-7.6.zip

# 解压
unzip sonarqube-7.6.zip 
# 创建目录
mkdir /app

# 移动并改名
[root@web01 ~]# mv sonarqube-7.6 /app/
# 软连接
[root@web01 app]# ln -s sonarqube-7.6 sonarqube
[root@web01 app]# ll
total 0
lrwxrwxrwx  1 root root  13 Aug 29 20:01 sonarqube -> sonarqube-7.6
drwxr-xr-x 11 root root 141 Jan 22  2019 sonarqube-7.6
# 修改配置文件
[root@web01 app]# vim /app/sonarqube/conf/sonar.properties
sonar.jdbc.username=sonar_user
sonar.jdbc.password=123
因为数据库也安装这台机器,所以sonar.jdbc.url=jdbc:postgresql://localhost/sonarqube?currentSchema=my_schema不改
# 优化

# 创建sonar系统用户
[root@web01 ~]# useradd sonar
# 授权
[root@web01 ~]# chown -R sonar.sonar /app/sonar*
# 启动

新版本9.8

安装JDK11

# 1.安装jdk11
[root@web01 ~]# yum install -y java-11-openjdk

安装数据库postgreSQL

# 1.更换postgreSQL yum源(去清华源找到想要的版本,复制链接地址)
[root@web01 ~]# rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/postgresql/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-42.0-32.noarch.rpm

# 2.安装postgreSQL
[root@web01 ~]# yum install -y postgresql13-server

# 3.初始化
[root@web01 ~]# /usr/pgsql-13/bin/postgresql-13-setup initdb
Initializing database ... OK
[root@web01 ~]# ll /var/lib/pgsql/13/data/
total 52
drwx------ 5 postgres postgres 41 Aug 29 10:37 base
drwx------ 2 postgres postgres 4096 Aug 29 10:37 global
drwx------ 2 postgres postgres 6 Aug 29 10:37 log
drwx------ 2 postgres postgres 6 Aug 29 10:37 pg_commit_ts
drwx------ 2 postgres postgres 6 Aug 29 10:37 pg_dynshmem
-rw------- 1 postgres postgres 4548 Aug 29 10:37 pg_hba.conf
-rw------- 1 postgres postgres 1636 Aug 29 10:37 pg_ident.conf
drwx------ 4 postgres postgres 68 Aug 29 10:37 pg_logical
drwx------ 4 postgres postgres 36 Aug 29 10:37 pg_multixact
drwx------ 2 postgres postgres 6 Aug 29 10:37 pg_notify
drwx------ 2 postgres postgres 6 Aug 29 10:37 pg_replslot
drwx------ 2 postgres postgres 6 Aug 29 10:37 pg_serial
drwx------ 2 postgres postgres 6 Aug 29 10:37 pg_snapshots
drwx------ 2 postgres postgres 6 Aug 29 10:37 pg_stat
drwx------ 2 postgres postgres 6 Aug 29 10:37 pg_stat_tmp
drwx------ 2 postgres postgres 18 Aug 29 10:37 pg_subtrans
drwx------ 2 postgres postgres 6 Aug 29 10:37 pg_tblspc
drwx------ 2 postgres postgres 6 Aug 29 10:37 pg_twophase
-rw------- 1 postgres postgres 3 Aug 29 10:37 PG_VERSION
drwx------ 3 postgres postgres 60 Aug 29 10:37 pg_wal
drwx------ 2 postgres postgres 18 Aug 29 10:37 pg_xact
-rw------- 1 postgres postgres 88 Aug 29 10:37 postgresql.auto.conf
-rw------- 1 postgres postgres 28100 Aug 29 10:37 postgresql.conf

# 4.启动postgre
[root@web01 ~]# systemctl start postgresql-13
检查端口netstat -lntup
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      42750/postmaster
启动报错原因:
[root@web01 ~]# ll /var/lib/pgsql/13/data/ 数据目录为空

# 5.切换用户
[root@web01 data]# su - postgres

# 6.登录终端
-bash-4.2$ psql
psql (13.12)
Type "help" for help.

# 7.创建用户
postgres=# create user sonar_user with password '123';
CREATE ROLE

# 8.创建数据库
postgres=# create database sonar_db owner sonar_user;
CREATE DATABASE

# 9.授权
postgres=# grant all on database sonar_db to sonar_user;
GRANT

# 10.测试连接数据库
[root@web01 ~]# psql -d sonar_db -U sonar_user -h127.0.0.1
Password for user sonar_user:   //密码是123,上面创建的
psql (13.12)
Type "help" for help.

sonar_db=>

安装sonar9.8

新版本10.1

安装JDK17

# 1.创建安装目录
[root@web01 ~]# mkdir /app

# 2.下载JDK17(Java17和Java8一样,都是LTS长期支持版本,因此这里直接下载)
[root@web01 ~]# wget https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.tar.gz

# 3.解压
[root@web01 ~]# tar xf jdk-17_linux-x64_bin.tar.gz -C /app/

# 4.做软连接
[root@web01 app]# ln -s /app/jdk-17.0.8 /app/jdk

# 5.添加环境变量
[root@web01 app]# vim /etc/profile.d/java17.sh
export JAVA_HOME=/app/jdk       //要看你把Java放在那里,root下就/use/local/jdk
export PATH=$PATH:$JAVA_HOME/bin

# 6.生效环境变量
source /etc/profile

# 7.查看版本(添加并生效完环境变量才能查看)
[root@web01 app]# java --version
java 17.0.8 2023-07-18 LTS
Java(TM) SE Runtime Environment (build 17.0.8+9-LTS-211)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.8+9-LTS-211, mixed mode, sharing)

安装数据库postgreSQL

# 1.更换postgreSQL yum源(去清华源找到想要的版本,复制链接地址)
[root@web01 ~]# rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/postgresql/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-42.0-32.noarch.rpm

# 2.安装postgreSQL
[root@web01 ~]# yum install -y postgresql13-server

# 3.初始化
[root@web01 ~]# /usr/pgsql-13/bin/postgresql-13-setup initdb
Initializing database ... OK
[root@web01 ~]# ll /var/lib/pgsql/13/data/
total 52
drwx------ 5 postgres postgres 41 Aug 29 10:37 base
drwx------ 2 postgres postgres 4096 Aug 29 10:37 global
drwx------ 2 postgres postgres 6 Aug 29 10:37 log
drwx------ 2 postgres postgres 6 Aug 29 10:37 pg_commit_ts
drwx------ 2 postgres postgres 6 Aug 29 10:37 pg_dynshmem
-rw------- 1 postgres postgres 4548 Aug 29 10:37 pg_hba.conf
-rw------- 1 postgres postgres 1636 Aug 29 10:37 pg_ident.conf
drwx------ 4 postgres postgres 68 Aug 29 10:37 pg_logical
drwx------ 4 postgres postgres 36 Aug 29 10:37 pg_multixact
drwx------ 2 postgres postgres 6 Aug 29 10:37 pg_notify
drwx------ 2 postgres postgres 6 Aug 29 10:37 pg_replslot
drwx------ 2 postgres postgres 6 Aug 29 10:37 pg_serial
drwx------ 2 postgres postgres 6 Aug 29 10:37 pg_snapshots
drwx------ 2 postgres postgres 6 Aug 29 10:37 pg_stat
drwx------ 2 postgres postgres 6 Aug 29 10:37 pg_stat_tmp
drwx------ 2 postgres postgres 18 Aug 29 10:37 pg_subtrans
drwx------ 2 postgres postgres 6 Aug 29 10:37 pg_tblspc
drwx------ 2 postgres postgres 6 Aug 29 10:37 pg_twophase
-rw------- 1 postgres postgres 3 Aug 29 10:37 PG_VERSION
drwx------ 3 postgres postgres 60 Aug 29 10:37 pg_wal
drwx------ 2 postgres postgres 18 Aug 29 10:37 pg_xact
-rw------- 1 postgres postgres 88 Aug 29 10:37 postgresql.auto.conf
-rw------- 1 postgres postgres 28100 Aug 29 10:37 postgresql.conf

# 4.启动postgre
[root@web01 ~]# systemctl start postgresql-13
检查端口netstat -lntup
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      42750/postmaster
启动报错原因:
[root@web01 ~]# ll /var/lib/pgsql/13/data/ 数据目录为空

# 5.切换用户
[root@web01 data]# su - postgres

# 6.登录终端
-bash-4.2$ psql
psql (13.12)
Type "help" for help.

# 7.创建用户
postgres=# create user sonar_user with password '123';
CREATE ROLE

# 8.创建数据库
postgres=# create database sonar_db owner sonar_user;
CREATE DATABASE

# 9.授权
postgres=# grant all on database sonar_db to sonar_user;
GRANT

# 10.测试连接数据库
[root@web01 ~]# psql -d sonar_db -U sonar_user -h127.0.0.1
Password for user sonar_user:   //密码是123,上面创建的
psql (13.12)
Type "help" for help.

sonar_db=>

安装sonar10.1

下载社区版:

# 1.环境优化(直接贴进去执行)(官网文档链接 https://docs.sonarsource.com/sonarqube/latest/requirements)
sysctl vm.max_map_count
sysctl fs.file-max
ulimit -n
ulimit -u
sysctl -w vm.max_map_count=524288
sysctl -w fs.file-max=131072
ulimit -n 131072
ulimit -u 8192
-----------------------------------------
[root@web01 ~]# vim /etc/security/limits.conf  (加到配置文件最后一行)
sonarqube - nofile 131072
sonarqube - nproc 8192




# 2.创建安装目录
[root@web01 ~]# mkdir /app

# 3.解压
[root@web01 ~]# unzip sonarqube-10.1.0.73491.zip

# 4.移动到安装目录
[root@web01 ~]# mv sonarqube-10.1.0.73491 /app/sonarqube-10.1

# 5.软链接
[root@web01 ~]# ln -s /app/sonarqube-10.1 /app/sonarqube

# 6.修改sonar配置文件(与数据库做关联)
[root@web01 ~]# vim /app/sonarqube/conf/sonar.properties
 26 sonar.jdbc.username=sonar_user
 27 sonar.jdbc.password=123
----------------44行没有改是因为数据库和sonar装在同一台机器-------------------------------
 44 #sonar.jdbc.url=jdbc:postgresql://localhost/sonarqube?currentSchema=my_schema
------------------------------------------------------------------------------------

# 7.创建sonar系统用户
[root@web01 ~]# useradd sonar

# 8.授权
[root@web01 ~]# chown -R sonar.sonar /app/sonar*

# 9.启动
[root@web01 ~]# su - sonar -c "/app/sonarqube/bin/linux-x86-64/sonar.sh start"
/app/jdk/bin/java
Starting SonarQube...
Started SonarQube.
#(或者先切换用户su - sonar 再启动/app/sonarqube/bin/linux-x86-64/sonar.sh start)
-#检查端口(9000)
[root@web01 ~]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      9293/sshd           
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      42750/postmaster    
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      9451/master         
tcp6       0      0 127.0.0.1:43431         :::*                    LISTEN      45130/java          
tcp6       0      0 :::9000                 :::*                    LISTEN      45192/java          
tcp6       0      0 127.0.0.1:9001          :::*                    LISTEN      45130/java          
tcp6       0      0 :::22                   :::*                    LISTEN      9293/sshd           
tcp6       0      0 ::1:5432                :::*                    LISTEN      42750/postmaster    
tcp6       0      0 ::1:25                  :::*                    LISTEN      9451/master         
tcp6       0      0 127.0.0.1:44122         :::*                    LISTEN      45450/java          
tcp6       0      0 127.0.0.1:9092          :::*                    LISTEN      45192/java          

# 浏览器访问
10.0.0.7:9000
访问后的登陆页面

SonarQube 代码质量检测_SonarQube

修改密码

SonarQube 代码质量检测_SonarQube_02

生成token
squ_50eb8733f6c211f524210fb86c4456851f829da7

SonarQube 代码质量检测_代码质量检测_03

SonarQube 代码质量检测_代码质量检测_04

创建项目

SonarQube 代码质量检测_代码质量检测_05

SonarQube 代码质量检测_代码质量检测_06

SonarQube 代码质量检测_SonarQube_07

SonarQube 代码质量检测_代码质量检测_08

把token放到刚创建的项目里

Other CI点进去

SonarQube 代码质量检测_SonarQube_09

SonarQube 代码质量检测_代码质量检测_10

生成代码质量检测命令

SonarQube 代码质量检测_SonarQube_11

mvn clean verify sonar:sonar \
  -Dsonar.projectKey=hello-world \
  -Dsonar.projectName='hello-world' \
  -Dsonar.host.url=http://10.0.0.7:9000 \
  -Dsonar.token=squ_50eb8733f6c211f524210fb86c4456851f829da7
执行代码质量检测

把检查命令贴到代码目录下,扫描检测一下代码

SonarQube 代码质量检测_SonarQube_12

去sonar页面看一下

SonarQube 代码质量检测_代码质量检测_13

查看bug

SonarQube 代码质量检测_代码质量检测_14

SonarQube 代码质量检测_SonarQube_15

SonarQube 代码质量检测_SonarQube_16

SonarQube 代码质量检测_SonarQube_17

修改bug

SonarQube 代码质量检测_SonarQube_18

再次扫描查看便没有bug了

SonarQube 代码质量检测_代码质量检测_19

在Jenkins上安装maven3.9

报错:执行 Maven 插件的过程中遇到了 API 不兼容性问题。具体来说,问题是由于 不同版本的 Java 编译造成的 解决方法:升级java或者降低maven版本

SonarQube 代码质量检测_SonarQube_20

# 先把原来的卸载
yum remove maven
# 安装适配版本
[root@jenkins app]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.9.4/binaries/apache-maven-3.9.4-bin.tar.gz --no-check-certificate
[root@jenkins app]# tar xf apache-maven-3.9.4-bin.tar.gz
[root@jenkins app]# mv apache-maven-3.9.4 maven-3.9.4
[root@jenkins app]# ln -s /app/maven-3.9.4 /app/maven
# 环境变量
[root@jenkins app]# vim /etc/profile.d/maven39.sh
export MAVEN_HOME=/app/maven/
export PATH=${PATH}:${MAVEN_HOME}/bin
# 更换mvn源
[root@jenkins app]# vim /app/maven/conf/settings.xml
加在 <mirrors></mirrors>
<mirror>
<id>aliyunmaven</id>
<mirrorOf>*</mirrorOf>
<name>阿里云公共仓库</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>

# 安装jdk11
也是把原来的先卸载 yum remove java-1.8.0-openjdk-headless.x86_64
[root@jenkins hello-word-maven]# yum reinstall -y java-11-openjdk

SonarQube 代码质量检测_SonarQube_21

SonarQube 代码质量检测_SonarQube_22

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月13日 0

暂无评论

推荐阅读