您的位置:首页 > 运维架构 > Linux

Linux 安装 SonarQube 6.0 及Maven项目的使用

2017-04-19 22:45 579 查看
本文简单对 SonarQube 6.0 版本在Linux中的安装配置进行说明。

准备

Jdk 1.8 (略)

SonarQube 6.0 (下载:http://www.sonarqube.org/downloads/

MySQL(略)

安装

在 MySQL 中创建 Database

CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;


解压 sonarqube-6.0.zip

配置 sonarqube-6.0/conf/sonar.properties

sonar.jdbc.username=root
sonar.jdbc.password=123456
sonar.jdbc.url=jdbc:mysql://127.0.0.1:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
# 启用 sonar.web.javaOpts 并添加 -server 参数
sonar.web.javaOpts=-server -Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError


配置 sonarqube-6.0/conf/sonar.properties

wrapper.java.command=/usr/local/java/jdk1.8.0_101/bin/java


配置服务

创建文件 /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 $*


依次执行如下命令(CentOS、RedHat):

ln -s $SONAR_HOME/bin/linux-x86-64/sonar.sh /usr/bin/sonar
chmod 755 /etc/init.d/sonar
chkconfig --add sonar


启动和停止服务

启动:service sonar start

停止:service sonar stop

重启:service sonar restart

注:第一次启动服务,可以查看日志 conf/sonar.log 观察进度,因为第一次会初始化数据库和相关配置需要一段时间。

访问 Sonar 添加中文插件

默认端口为 9000,默认 context 为 “/”,默认账号密码 admin/admin,如果需要修改,可以在 sonar.properties 中修改。

访问 http://127.0.0.1:9000

按如下截图操作添加中文插件:

Restart 完成后重新访问就是中文界面了

Maven 项目的使用

1、配置 maven 全局配置

在 apache-maven/conf/settings.xml 中配置如下内容:

<settings>
<pluginGroups>
<pluginGroup>org.sonarsource.scanner.maven</pluginGroup>
</pluginGroups>
<profiles>
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<!-- Optional URL to server. Default value is http://localhost:9000 -->
<sonar.host.url> http://MySonarQubeServer:9000 </sonar.host.url>
</properties>
</profile>
</profiles>
</settings>


2、使用命令对代码进行分析

# 分析代码命令
mvn sonar:sonar
# 可以和打包、编译、安装等命令一起使用,例如:
mvn clean package sonar:sonar


3、查看结果

打开 sonarqube 网页,登录进去就可以看到该项目的处理结果了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: