sonarqube For PHP 代码质量管理

下载jdk&sonarqube&sonar-scanner

安装jdk

基于ubuntu 16.04+apache[具体webserver采用缺省安装]
推荐下载安装【底部有网盘地址】

sonarqube-5.6.6.zip 
jdk-8u121-linux-x64.tar.gz 
sonar-scanner-2.8.zip

解压安装配置环境变量

我的在usr/local下,uzip解压 mv等操作略过 .

root@ubuntu:/usr/local/sonar# ls
sonarqube-5.6.6  sonar-scanner-2.8
root@ubuntu:/usr/local/sonar#
root@ubuntu:/usr/local/java# ls
jdk1.8.0_121
root@ubuntu:/usr/local/java#
vim ~/.bashrc

打开之后在末尾添加,以下只是参考,只要放到环境变量就行

export JAVA_HOME=/usr/local/java/YOUR_DOWN_LOAD_VERSION
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH

export SONAR_HOME=/usr/local/sonar/sonarqube-5.6.6
export SONAR_RUNNER_HOME=/usr/local/sonar-scanner-2.8
export JAVA_HOME=/usr/local/java/jdk1.8.0_121
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${SONAR_RUNNER_HOME}/bin:$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH

验证一下java环境

root@ubuntu:/usr/local/java# java
Usage: java [-options] class [args...]
           (to execute a class)
   or  java [-options] -jar jarfile [args...]
           (to execute a jar file)
where options include:
    -d32      use a 32-bit 
    ...
    ...

mysql数据源配置,sonar配置文件

mysql -u root -p

以下除了第一行都不是必须

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;
vim /usr/local/sonar/sonar-scanner-2.8/conf

注意以下配置mysql数据源,截取部分..

#----- Global database settings (not used for SonarQube 5.2+)
sonar.jdbc.username=root
sonar.jdbc.password=root

#----- PostgreSQL
#sonar.jdbc.url=jdbc:postgresql://localhost/sonar

#----- MySQL
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
vim /usr/local/sonar/sonarqube-5.6.6/conf
# The schema must be created first.
sonar.jdbc.username=root
sonar.jdbc.password=root

#----- Embedded Database (default)
# H2 embedded database server listening port, defaults to 9092
#sonar.embeddedDatabase.port=9092
#----- MySQL 5.6 or greater
# Only InnoDB storage engine is supported (not myISAM).
# Only the bundled driver is supported. It can not be changed.
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance

启动sonarqube,配置中文插件

/usr/local/sonar/sonarqube-5.6.6/bin/linux-x86-64/sonar.sh start

我这里已经启动了,

sonarqube For PHP 代码质量管理

tb@ubuntu:/usr/local/sonar/sonarqube-5.6.6/bin/linux-x86-64$ /usr/local/sonar/sonarqube-5.6.6/bin/linux-x86-64/sonar.sh status
SonarQube is running (2187).

更好的启动sonarqube

vim /etc/systemd/system/sonar.service

[Unit]
Description=SonarQube service
After=syslog.target network.target

[Service]
Type=forking

## 可以具体路径再改下
ExecStart=/usr/local/sonar/sonarqube-5.6.6/bin/linux-x86-64/sonar.sh start
ExecStop/usr/local/sonar/sonarqube-5.6.6/bin/linux-x86-64/sonar.sh stop

User=root
Group=root
Restart=always

[Install]
WantedBy=multi-user.target

可以通过以下语句控制

systemclt start sonar
systemctl stop sonar
systemctl status sonar

如果有报错: Process exited with exit value [es]: *

可以追踪sonarqube下的log文件,我这里经过定位是启动es的时候用root启动了
需要adduser sonaradmin;passwd sonarsonaradmin;然后chown -R sonaradmin:sonaradmin /sonar_path即可
然后启动用sonaradmin启动即可成功。

如果有报错: Java heap space

官方文档即可

export SONAR_SCANNER_OPTS="-Xmx1028m"
set SONAR_SCANNER_OPTS=-Xmx512m

是时候来张图了

具体的php和汉化,可以通过web页面install对应的插件。很方便。可以直接搜索如 php chinese等,后台默认用户名 密码都是 admin

sonarqube For PHP 代码质量管理


引入具体项目

需要在你的检测目录下建立一个配置文件即可

tb@ubuntu:~/sonartest/CI_Cart$ ls
admin  application  ci_cart.sql  css  images  index.php  js  LICENSE  README.md  sonar-project.properties  system  uploads
tb@ubuntu:~/sonartest/CI_Cart$ pwd

上面的sonar-project.properties内容参考如下即可:

sonar.projectKey=org.codehaus.sonar:ci_cart
sonar.projectName=PHP CI_Cart  sonar test 
sonar.projectVersion=1.0 
#这里是php文件放的地方 
sonar.sources=/home/tb/sonartest/CI_Cart
# 单元测试代码目录
#sonar.tests=./tests
#单元测试覆盖率报告文件
#sonar.php.coverage.reortPath=./logs/clover.xml
#单元测试报告文件
#sonar.php.tests.reportPath=./logs/junit.xml
# Language 
sonar.language=php 
sonar.dynamicAnalysis=false 
# Encoding of the source files 
sonar.sourceEncoding=UTF-8
~

需要检测代码目录执行scanner【需要有sonar配置文件】

root@ubuntu:/home/tb/sonartest/CI_Cart# /usr/local/sonar/sonar-scanner-2.8/bin/sonar-scanner 
INFO: Scanner configuration file: /usr/local/sonar/sonar-scanner-2.8/conf/sonar-scanner.properties
INFO: Project root configuration file: /home/tb/sonartest/CI_Cart/sonar-project.properties
INFO: SonarQube Scanner 2.8
INFO: Java 1.8.0_121 Oracle Corporation (64-bit)
INFO: Linux 4.4.0-62-generic amd64
INFO: User cache: /root/.sonar/cache
INFO: Load global repositories
INFO: Load global repositories (done) | time=221ms

自动化Jenkins了解一下

安装jenkins
sonarqube For PHP 代码质量管理

需要安装locale插件汉化,之后需要重启

Locale     Downloaded Successfully. Will be activated during the next boot
Locale     Success

由于通过apt-get isntall jenkins安装的,我直接/etc/init.d/jenkins {start|stop|status|restart|force-reload}即可

百度网盘下载地址
how-to-install-sonarqube-on-ubuntu-16-04

相关推荐