Sonar是一个用于代码质量管理的开源平台,用于管理源代码的质量,可以从多个维度检测代码质量
,通过插件形式,可以支持包括java,C#,C/C++,PL/SQL,Cobol,JavaScrip,Groovy等等二十几种编程语言的代码质量管理与检测。
Sonar可以通过多种方式来实现代码质量检查,官方资料显示Sonar 运行分析的方法有以下几种方式:
SonarQube Scanner: Launch analysis from the command line
SonarQube Scanner for MSBuild: Launch analysis of .Net projects
SonarQube Scanner for Ant: Launch analysis from Ant
SonarQube Scanner for Maven: Launch analysis from Maven with minimal configuration
SonarQube Scanner for Gradle: Launch Gradle analysis
SonarQube Scanner For Jenkins: Launch analysis from Jenkins
本系列文章重点实现命令行方式(Scanner2.8、Runner2.4)、集成Eclipse实时分析分式、Jenkins+Maven对代码资源库分析方式。
一、安装版本及下载地址
Sonarqube版本:6.0
sonar-runner版本:sonar-runner-dist-2.4
sonar-scanner版本:sonar-scanner-2.8
汉化包版本:sonar-l10n-zh-plugin-1.8.jar
数据库版本:MySQL-5.6.32
操作系统版本:Centos7.2-最小化安装
JDK版本:JDK1.8.0_102 64位
二、下载各软件
cd /usr/local/srcwget http://cdn.mysql.com/Downloads/MySQL-5.6/MySQL-5.6.32-1.linux_glibc2.5.x86_64.rpm-bundle.tar
wget http://download.oracle.com/otn-pub/java/jdk/8u102-b14/jdk-8u102-linux-x64.rpm
wget http://repo1.maven.org/maven2/org/codehaus/sonar/runner/sonar-runner-dist/2.4/sonar-runner-dist-2.4.zip
wget https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-6.0.zip
三、安装软件
1、Mysql5.6.23安装
tar xvf MySQL-5.6.32-1.linux_glibc2.5.x86_64.rpm-bundle.taryum remove -y mariadb-libs-5.5.50-1.el7_2.x86_64 #删除冲突包yum -y install autoconf #安装依赖包yum -y install MySQL-server-5.6.32-1.linux_glibc2.5.x86_64.rpm #安装服务端yum -y install MySQL-client-5.6.32-1.linux_glibc2.5.x86_64.rpm #安装客户端
MySQL的默认安装位置
/var/lib/mysql #数据库目录 datadir/usr/share/mysql #配置文件目录/usr #相关命令目录 basadir/etc/init.d/mysql #启动脚本
在安装时已经建立了默认用户mysql,并对默认目录进行了访问授权。
vim /etc/init.d/mysql
找到并修改如下:
basedir='/usr'
datadir='/var/lib/mysql'
cp /usr/share/mysql/my-default.cnf /etc/my.cnf
vim /etc/my.cnf
找到并如下修改
basedir = /usr
datadir = /var/lib/mysql
port = 3306
# server_id = .....
socket = /var/lib/mysql/mysql.sock
mysql_install_db --user=mysql #生成新的MySQL授权表,并且只能在mysql服务停止的情况下运行
在提示文件中看到两个“OK”后才表示成功
systemctl daemon-reload #启动守护进程
systemctl start mysql.service
systemctl enable mysql.service
mysql Curoot
show databases;
exit
mysqladmin -uroot password 'll1qaz369*'
mysql -uroot -pll1qaz369*
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;
2、安装JDK1.8.0_102
cd /usr/local/src
yum Cy install jdk-8u102-linux-x64.rpm
vim /etc/profile
JAVA_HOME=/usr/java/jdk1.8.0_102/
JAVA_BIN=/usr/java/jdk1.8.0_102/bin
JRE_HOME=/usr/java/jdk1.8.0_102/jre
PATH=$PATH:/usr/java/jdk1.8.0_102/bin:/usr/java/jdk1.8.0_102/jre/bin
CLASSPATH=/usr/java/jdk1.8.0_102/jre/lib:/usr/java/jdk1.8.0_102/lib:/usr/java/jdk1.8.0_102/jre/lib/charsets.zip
export JAVA_HOME JAVA_BIN JRE_HOME PATH CLASSPATH
source /etc/profile
java Cversion
java version "1.8.0_102"
Java(TM) SE Runtime Environment (build 1.8.0_102-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)
3、安装配置Sonarqube6.0
yum Cy install unzip
unzip sonarqube-6.0.zip
mv sonarqube-6.0 /usr/local/
vim /usr/local/sonarqube-6.0/conf/sonar.properties
找到下面内容并把前面的#号去掉,打开对mysql5.6的支持(所有用默认,根据实际需求可以自己改配置)
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useCOnfigs=maxPerformance
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
sonar.web.host=0.0.0.0
sonar.web.cOntext=
sonar.web.port=80
cd /usr/local/sonarqube-6.0/bin/linux-x86-64
./sonar.sh start #启动服务(其它操作: ./sonar.sh stop 停止服务 ./sonar.sh restart 重启服务
Starting SonarQube...
Started SonarQube.
cd /usr/local/sonarqube-6.0/extensions/plugins/
wget -c http://repo1.maven.org/maven2/org/codehaus/sonar-plugins/l10n/sonar-l10n-zh-plugin/1.8/sonar-l10n-zh-plugin-1.8.jar
(这是中文语言包的源码地址:https://github.com/SonarCommunity/sonar-l10n-zh)
Sonar自带的语言规则只有:C#,Java,Javascript,如果需要其它语言规则可以自己下载到plugins目录下后再重启sonar服务就行,
下载网址:http://repo1.maven.org/maven2/org/codehaus/sonar-plugins/
/usr/local/sonarqube-6.0/bin/linux-x86-64/sonar.sh restart
重启sonar服务后再次登陆地址,已经改成中文版了,所有规则已经就绪。
vim /etc/init.d/sonar
新建文件并增加如下内容:
#!/bin/sh
#
# rc file for SonarQube
#
# chkconfig: 345 96 10
# description: SonarQube system (www.sonarsource.org)
#
### BEGIN INIT INFO
# Provides: sonar
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 3 4 5
# Default-Stop: 0 1 2 6
# Short-Description: SonarQube system (www.sonarsource.org)
# Description: SonarQube system (www.sonarsource.org)
### END INIT INFO
/usr/bin/sonar $*
ln -s /usr/local/sonarqube-6.0/bin/linux-x86-64/sonar.sh /usr/bin/sonarchmod 755 /etc/init.d/sonarchkconfig --add sonarchkconfig sonar onchkconfig start
本文出自 “坚强的技术交流blog” 博客,请务必保留此出处http://newthink.blog.51cto.com/872263/1860665