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

持续集成(三)CentOS7下SonarQube代码质量平台的安装与使用

2017-02-13 00:00 1286 查看

1. 环境准备

CentOS7操作系统;

安装JDK并配置环境变量;

安装MySql5.7数据库;

2. 配置MySql

首先需要打开mysql的端口,默认安装后是3306,接着开启远程连接,具体操作如下:

## 打开防火墙端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload

## 登陆mysql数据库
mysql -uroot -p

## 执行远程访问授权
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '你的mysql登录密码' WITH GRANT OPTION;

查看数据库的引擎:

## 查看mysql的引擎,5.7版本的mysql默认就应该是InnoDB了
mysql> show engines;




## 查看当前的存储引擎
mysql> show variables like '%storage_engine%';




## 修改后退出
mysql> exit;

如果你的mysql引擎默认不是InnoDB,那么在/etc/my.cnf文件中可以配置:

## 编辑/etc/my.cnf
vi /etc/my.cnf

## 在配置文件中[mysqld]下面加入(如果是INNODB就不需要配置这行了)
default-storage-engine=INNODB

## 在配置文件中设置插入缓存
innodb_buffer_pool_size = 256M

## 开启查询缓存
query_cache_type=1
query_cache_size=32M

## 设置好以后保存文件,重启mysql服务
systemctl restart mysqld

## 在登陆mysql查看配置是否生效
mysql> show variables like '%query_cache%';




完整的my.cnf配置文件如下:

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html 
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

innodb_buffer_pool_size = 256M
query_cache_type=1
query_cache_size=32M


3. 创建SonarQube数据库



字符集选择utf8就好。

4. 下载安装SonarQube

https://www.sonarqube.org/downloads/



## 下载后,解压文件
unzip sonarqube-5.6.5.zip

## 将sonarqube移动到opt文件夹
mv sonarqube-5.6.5 /opt/sonarqube

## 编辑sonarqube的配置文件
cd /opt/sonarqube/conf
vi sonar.properties

## 修改mysql的数据库连接
sonar.jdbc.username=root
sonar.jdbc.password=你的密码
#-----MySQL 5.x
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonarqube?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance

## 修改sonarqube的web配置
sonar.web.host=0.0.0.0
sonar.web.context=/sonarqube
sonar.web.port=9090

## 打开端口
firewall-cmd --zone=public --add-port=9090/tcp --permanent
firewall-cmd --reload

## 最后,打开sonarqube服务
/opt/sonarqube/bin/linux-x86-64/sonar.sh start

## 第一次运行会初始化数据库,速度有些慢
## 可以监听sonarqube的日志查看具体信息
cd /opt/sonarqube-5.6.5/logs
tail -f sonar.log

浏览器中输入:http://192.168.0.81:9090/sonarqube/



5. 配置SonarQube

5.1 安装补丁



登录后,点击Administrators菜单;

点击System菜单;

点击Update Center菜单;

点击Available选项;

在黄色位置输出Chinese Pack(中文插件);

在最右边有Install的按钮;

安装插件

按照上面的流程,把CheckStyle和PMD插件也都安装了,然后重启sonarqube系统。

5.2 使用maven发布项目到sonarqube

修改maven的配置文件,在profiles标签中加入:

<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<!-- Example for MySQL-->
<sonar.jdbc.url>
jdbc:mysql://192.168.4.221:3306/sonarqube?useUnicode=true&characterEncoding=utf8
</sonar.jdbc.url>
<sonar.jdbc.username>root</sonar.jdbc.username>
<sonar.jdbc.password>你的密码</sonar.jdbc.password>

<!-- Optional URL to server. Default value is http://localhost:9000 -->
<sonar.host.url> http://192.168.4.221:9090/sonarqube </sonar.host.url>
</properties>
</profile>

修改后的maven配置文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <localRepository>D:/Soft/apache-maven-3.3.9/.m2/repository</localRepository>
<interactiveMode>true</interactiveMode>
<offline>false</offline>
<pluginGroups>
<pluginGroup>org.mortbay.jetty</pluginGroup>
<pluginGroup>org.jenkins-ci.tools</pluginGroup>
</pluginGroups>

<!--配置权限,使用默认用户-->
<servers>
<server>
<id>nexus-releases</id>
<username>admin</username>
<password>admin</password>
</server>
<server>
<id>nexus-snapshots</id>
<username>admin</username>
<password>admin</password>
</server>
</servers>

<mirrors>
<!--<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
</mirror>-->
</mirrors>

<profiles>
<profile>
<id>ems</id>
<activation>
<activeByDefault>false</activeByDefault>
<jdk>1.8</jdk>
</activation>
<repositories>
<!-- 私有库地址-->
<repository>
<id>nexus</id>
<url>http://192.168.0.81:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<!--插件库地址-->
<pluginRepository>
<id>nexus</id>
<url>http://192.168.0.81:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>

<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<!-- Example for MySQL-->
<sonar.jdbc.url>
jdbc:mysql://192.168.0.81:3306/sonarqube?useUnicode=true&characterEncoding=utf8
</sonar.jdbc.url>
<sonar.jdbc.username>root</sonar.jdbc.username>
<sonar.jdbc.password>Csit-123456~</sonar.jdbc.password>

<!-- Optional URL to server. Default value is http://localhost:9000 -->
<sonar.host.url> http://192.168.0.81:9090/sonarqube </sonar.host.url>
</properties>
</profile>
</profiles>

<!--激活配置-->
<activeProfiles>
<activeProfile>ems</activeProfile>
</activeProfiles>

</settings>


5.3 发布项目到sonarqube

Eclipse中执行:clean install sonar:sonar

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: