Linux环境SonarQube部署(JDK1.7+MySQL5.X)

3 篇文章 0 订阅
2 篇文章 0 订阅

Linux环境SonarQube部署

SonarQube安装

预置条件

  1. 已安装JAVA环境(JDK1.7)
  2. 已安装有MySQL数据库

  3. 下载SonarQube 版本5.1.2(支持JDK1.7、Oracle11、MySQL5.X)

  4. 下载sonar-runner-dist-2.4

  5. 下载sonar-scanner2.6(sonar-scanner2.8需要JDK1.8)

  6. 下载中文补丁包

下载之后上传到linux服务器,解压安装:

部署路径:/srv/www/app/sonar

 unzip sonarqube-5.1.2.zip

MySQL建库

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;

编辑conf目录下sonar.properties配置:

vi /srv/www/app/sonar/sonarqube-5.1.2/conf/sonar.properties

添加以下内容:

#数据库连接配置
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance

#根据需求修改默认配置(默认9000端口)
#sonar.web.host=0.0.0.0
#sonar.web.context=

SonarQube会自动下载MySQL驱动包

启动 SonarQube Web Server

/srv/www/app/sonar/sonarqube-5.1.2/bin/linux-x86-64/sonar.sh start
/* 初次启动会自动建表和做相应的初始化 */

浏览器中输入:http://localhost:9000

初始语言是英文
这里写图片描述

添加中文语言包

  1. 将下载好的sonar-l10n-zh-plugin-1.6.jar复制到${SONAR_HOME}/extensions/plugins

  2. 重启SonarQube项目:/srv/www/app/sonar/sonarqube-5.1.2/bin/linux-x86-64/sonar.sh restart

SonarQube安装完毕

sonar-runner-dist-2.4安装

安装目录:/srv/www/app/sonar/sonar-runner-dist-2.4

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-dist-2.4/ /srv/www/app/sonar/

配置PATH路径

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

PATH=$PATH:/srv/www/app/sonar/sonar-runner-dist-2.4/bin
export PATH

source /etc/profile

配置sonar-runner启动配置文件

vi /srv/www/app/sonar/sonar-runner-dist-2.4/conf/sonar-runner.properties

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

sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
sonar.jdbc.username=thinkxdt
sonar.jdbc.password=testpwd
sonar.login=admin
sonar.password=admin

安装成功后重启服务器,在命令行运行以上命令并回显,表示运行成功。
[root@sonar root]# sonar-runner -h
INFO:
INFO: usage: sonar-runner [options]
INFO:
INFO: Options:
INFO: -D,–define 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

sonar-scanner2.6安装

cd /srv/www/app/sonar

下载

wget https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/sonar-scanner-2.6.zip

unzip sonar-scanner-2.6.zip

mv sonar-scanner-2.6/ /srv/www/app/sonar/

配置PATH路径

vi /etc/profile

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

PATH=$PATH:/srv/www/app/sonar/sonar-runner-2.4/bin:/srv/www/app/sonar/sonar-scanner-2.6/bin
export PATH

配置sonar-scanner启动配置文件

vi /srv/www/app/sonar/sonar-scanner-2.6/conf/sonar-scanner.properties

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

sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
sonar.jdbc.username=username
sonar.jdbc.password=testpwd

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

[root@sonar root]# sonar-scanner -h
INFO:
INFO: usage: sonar-scanner [options]
INFO:
INFO: Options:
INFO: -D,–define 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

SonarQube使用

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

解压上传的源代码:

cd /srv/www/app/sonar/project/

unzip prodectName.zip

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

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

cd prodectName/

vim sonar-project.properties

2、建立文件内容如下:

#must be unique in a given SonarQube instance   
sonar.projectKey=ifc-handheld-webapp:scanner         
#this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 5.1.2
sonar.projectName=ifc-handheld-webapp-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
注意:必须在检测项目根路径下执行命令

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

http://localhost:9000/

登录账户:admin
登录密码:admin

Home –> projects

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

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

cd /srv/www/app/sonar/project/

vim sonar-project.properties

2、修改文件内容如下:

#must be unique in a given SonarQube instance   
sonar.projectKey=ifc-handheld-webapp:runner    
#this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 5.1.2
sonar.projectName=ifc-handheld-webapp-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
注意:必须在检测项目根路径下执行命令

在web中查看Runnner代码质量分析的结果

基于Maven配置

<!-- 首先在 Maven 的 settings.xml 文件中添加如下标签,内容需与sonar.properties中设置的一致。 -->
<profile>
      <id>sonar</id>
      <activation>
          <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
          <sonar.jdbc.url>jdbc:mysql://localhost:3306/sona</sonar.jdbc.url>
          <sonar.jdbc.driver>oracle.jdbc.OracleDriver</sonar.jdbc.driver>
          <sonar.jdbc.username>sonar</sonar.jdbc.username>
          <sonar.jdbc.password>sonar</sonar.jdbc.password>
          <sonar.host.url>http://ip:9000</sonar.host.url>
      </properties>   
</profile>

<!-- 修改Maven工程的pom.xml,添加sonar插件:-->
<plugin>
       <groupId>org.codehaus.mojo</groupId>
       <artifactId>sonar-maven-plugin</artifactId>
       <version>2.6</version>
</plugin>

在maven中执行clean sonar:sonar,访问sonar,即可看到分析结果。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值