一、安装好了SonarQube服务端后,在其它电脑的浏览器上登陆,开始安装其它编程语言检测插件

Sonar6.0应用之二:Sonar Web界面配置及与Runner、Scanner集成进行命令行代码分析

Sonar6.0应用之二:Sonar Web界面配置及与Runner、Scanner集成进行命令行代码分析

系统已经装好的语言插件:

Sonar6.0应用之二:Sonar Web界面配置及与Runner、Scanner集成进行命令行代码分析

Sonar6.0应用之二:Sonar Web界面配置及与Runner、Scanner集成进行命令行代码分析

下载了软件项目中常用的语言:Android、CSS、Web、XML

JAVA相关的:Checkstyle、Findbugs、PMD

Java 静态分析工具分析对象

应用技术

Checkstyle

Java 源文件,缺陷模式匹配

FindBugs

字节码,缺陷模式匹配;数据流分析

PMD

Java 源代码,缺陷模式匹配

下载完分析语言规则后,重启服务

Sonar6.0应用之二:Sonar Web界面配置及与Runner、Scanner集成进行命令行代码分析

 

二、安装命令行分析端

sonar的命令行分析端软件有两种分别是Runner和Scanner,官网文档中写的是Scanner,但Runner和它安装、使用都基本一致。

1、在CentOS上安装sonar-runner-dist-2.4

cd /usr/local/src/

wget http://repo1.maven.org/maven2/org/codehaus/sonar/runner/sonar-runner-dist/2.4/sonar-runner-dist-2.4.zip

unzip sonar-runner-dist-2.4.zip

mv sonar-runner-2.4/ /usr/local/

  • 配置PATH路径

vim /etc/profile

在文件最后加入如下内容,保存并退出。

PATH=$PATH:/usr/local/sonar-runner-2.4/bin  
export PATH

  • 配置sonar-runner启动配置文件

vim /usr/local/sonar-runner-2.4/conf/sonar-runner.properties

把下面内容前#号去掉或增加后,保存并退出

sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8

sonar.jdbc.username=sonar   

sonar.jdbc.password=sonar  
sonar.host.url=http://192.168.1.190    
sonar.login=admin  
sonar.password=admin

  • 安装成功后重启服务器,在命令行运行以上命令并回显,表示运行成功。

[[email protected] local]# sonar-runner -h  
INFO:    
INFO: usage: sonar-runner [options]    
INFO:    
INFO: Options:    
INFO:  -D,--define <arg>     Define property    
INFO:  -e,--errors           Produce execution error messages    
INFO:  -h,--help             Display help information    
INFO:  -v,--version          Display version information    
INFO:  -X,--debug            Produce execution debug output

 

2、在CentOS上安装sonar-scanner2.8

cd /usr/local/src/
wget https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/sonar-scanner-2.8.zip
unzip sonar-scanner-2.8.zip
mv sonar-scanner-2.8/ /usr/local/
  • 配置PATH路径

vim /etc/profile

在文件最后加入如下内容,保存并退出。

PATH=$PATH:/usr/local/sonar-runner-2.4/bin:/usr/local/sonar-scanner-2.8/bin  
export PATH

  • 配置sonar-scanner启动配置文件

vim /usr/local/sonar-scanner-2.8/conf/sonar-scanner.properties

把下面内容前#号去掉或增加后,保存并退出

sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&amp;characterEncoding=utf8  
sonar.jdbc.username=sonar  
sonar.jdbc.password=sonar  
sonar.host.url=http://192.168.1.190    
sonar.login=admin  
sonar.password=admin

  • 安装成功后重启服务器,在命令行运行以上命令并回显,表示运行成功。

[[email protected] local]# sonar-scanner -h  
INFO:    
INFO: usage: sonar-scanner [options]    
INFO:    
INFO: Options:    
INFO:  -D,--define <arg>     Define property    
INFO:  -h,--help             Display help information    
INFO:  -v,--version          Display version information    
INFO:  -X,--debug            Produce execution debug output    
INFO:  -i,--interactive      Run interactively

 

三、把开发程序的源代码打包成zip文件上传到安装有Runner或Scanner的服务器上

Sonar6.0应用之二:Sonar Web界面配置及与Runner、Scanner集成进行命令行代码分析

解压上传的源代码:

cd /usr/local/

unzip whale.zip

 

四、使用sonar-scanner进行代码质量分析

1、在服务器上建立一个准备用Scanner执行的配置文件

cd whale/

vim sonar-project.properties

2、建立文件内容如下:

# must be unique in a given SonarQube instance  
sonar.projectKey=whale:scanner        
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.    
sonar.projectName=whale-scanner    
sonar.projectVersion=1.0

# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.  
# Since SonarQube 4.2, this property is optional if sonar.modules is set.    
# If not set, SonarQube starts looking for source code from the directory containing    
# the sonar-project.properties file.    
sonar.sources=.

# Encoding of the source code. Default is default system encoding  
#sonar.sourceEncoding=UTF-8

3、保存并退出后运行命令进行分析(分析中不能执行Findbugs3.4.3分析,在web端卸载这个规则后可以正常分析):

sonar-scanner

4、在web中查看Scanner代码质量分析的结果。

Sonar6.0应用之二:Sonar Web界面配置及与Runner、Scanner集成进行命令行代码分析

 

五、使用sonar-Runner进行代码质量分析

1、修改下Scanner执行时的配置文件

cd /usr/local/whale/

vim sonar-project.properties

2、修改文件内容如下:

# must be unique in a given SonarQube instance  
sonar.projectKey=whale:runner    
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.    
sonar.projectName=whale-runner    
sonar.projectVersion=1.0

# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.  
# Since SonarQube 4.2, this property is optional if sonar.modules is set.    
# If not set, SonarQube starts looking for source code from the directory containing    
# the sonar-project.properties file.    
sonar.sources=.

# Encoding of the source code. Default is default system encoding  
#sonar.sourceEncoding=UTF-8

3、保存并退出后运行命令进行分析(分析中不能执行Findbugs3.4.3分析,在web端卸载这个规则后可以正常分析):

sonar-runner

4、在web中查看runner代码质量分析的结果。

Sonar6.0应用之二:Sonar Web界面配置及与Runner、Scanner集成进行命令行代码分析

 

结果一样,证明Runner和Scanner功能差不多。