centos安装使用sonarqube

架构

架构

环境

centos 7
mysql
sonarqube7.1

下载

yum install unzip -y

wget http://tar.matosiki.site/zip/sonarqube-7.3.zip

unzip sonarqube-7.3.zip

mv sonarqube-7.3 /opt/



sudo groupadd sonar

sudo useradd -g sonar sonar

chown -R sonar.sonar /opt/sonarqube-7.3/

进入mysql创建数据库

CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;

CREATE USER 'sonar'@'%' IDENTIFIED BY 'sonar';

GRANT all privileges ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar';

flush privileges;

cd /opt/sonarqube-7.3/conf/

vi sonar.properties

 #数据库用户
sonar.jdbc.username=root
#数据库密码
sonar.jdbc.password=123456
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance

sonar.web.host=0.0.0.0
sonar.web.port=9000

##启动

su sonar

/opt/sonarqube-7.3/bin/linux-x86-64/sonar.sh start

登录 访问地址http://172.27.82.119:9000/

用户名和密码都是admin

然后紧接着官方引导教程

  1. 输入name做为tokenn

    matosiki

    生成token:6a831d80491fe2e2d43c5d19270741c2b619ac05
    把token复制保存下来
  2. 选择项目得开发语言

java>maven

执行测试

mvn sonar:sonar \
-Dsonar.host.url=http://172.27.82.119:9000 \
-Dsonar.login=d81cb0f66e3ec4dd87adb782c6bdc67f0f7fe96c

  1. 安装插件
    安装汉化包Administrator中MarketPlace搜索chinese
    Checkstyle

重启生效

maven 配置sonar使用

配置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://myserver:9000
</sonar.host.url>
</properties>
</profile>
</profiles>
</settings>

jenkins 插件使用sonar

  1. jenkins安装插件
    系统管理—>插件管理,搜索一下sonar安装

  2. 配置sonar

系统管理—>系统设置,配置name和server url

粘贴保存下的token

  1. 配置全局工具
    下载地址
wget http://tar.matosiki.site/zip/sonar-scanner-2.8.zip

unzip sonar-scanner-2.8.zip

mv sonar-scanner-2.8 /opt/

vi /opt/sonar-scanner-2.8/conf/sonar-scanner.properties

# 编辑配置文件
sonar.host.url=http://localhost:9000
sonar.sourceEncoding=UTF-8
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
sonar.jdbc.url=jdbc:mysql://172.27.82.11:3306/sonar?useUnicode=true&amp;characterEncoding=utf8



# 添加环境变量
vi /etc/profile

export SCANNER_HOME=/opt/sonar-scanner-2.8
export PATH=$PATH:$SCANNER_HOME/bin


# 测试是否设置成功
source /etc/profile

sonar-scanner -h

jekins中配置:
global tool configuration > SonarQube Scanner>add>

SONAR_RUNNER_HOME=/opt/sonar-scanner-2.8
  1. 选择项目

项目构建>配置>构建> Add build step>Execute SonarQube Scanner
execute shell

export MAVEN_HOME=/opt/apache-maven3.5
export PATH=$PATH:$MAVEN_HOME/bin
cd ${WORKSPACE}
mvn install

###千万不要在Task to run中输入值

sonar.projectKey=spring-boot-example
sonar.projectName=spring-boot-example
sonar.projectVersion=1.0
sonar.language=java
sonar.sources=${WORKSPACE}
sonar.java.binaries=${WORKSPACE}

  1. 在项目中配置 project properties 文件
    编辑sonar-project.properties
    sonar.projectKey=org.sonarqube:spring-boot-example

    #在SonarQube中展示的项目名称

    sonar.projectName=项目名称

    #在SonarQube中展示的版本号
    sonar.projectVersion=1.0

    #设置源代码的路径

    sonar.sources=/var/lib/jenkins/workspace/spring-boot-example/spring-boot-email/src/

    #设置字节码文件的路径
    sonar.binaries=/var/lib/jenkins/workspace/spring-boot-example/spring-boot-email/target/classes
    sonar.language=java
    sonar.sourceEncoding=UTF-8

    #设置字报告存放的路径

    sonar.jacoco.itReportPath=/var/lib/jenkins/workspace/spring-boot-example/spring-boot-email/reports/jacoco.exec

maven检查代码命令

mvn clean verify sonar:sonar
或者
mvn clean install org.sonarsource.scanner.maven:sonar-maven-plugin:3.5.0.1254:sonar

报错

elasticsearch 错误

解决方案
查看es日志

cat /opt/sonarqube-7.1/logs/es.log

can not run elasticsearch as root

新建sonar用户 和sonar组

sudo groupadd sonar

sudo useradd -g sonar sonar

chown -R sonar:sonar /opt/sonarqube-7.1

failed to obtain node locks
查看进程

ps aux | grep ‘elastic’

获取pid

kill -9 90123

删除elasticsearch data文件

Cannot execute statement: impossible to write to binary log since BINLOG_FORMAT = STATEMENT and at least one table uses a storage engine limited to row-based logging. InnoDB is limited to row-logging when transaction isolation level is READ COMMITTED or READ UNCOMMITTED.

在mysql里设置binlog_format为ROW,此时binlog会增大,但是一般来说对数据复制支持的更好,建议单机高性能环境下使用。

删除初始化得database*sonar

vi /etc/my.conf

binlog_format=ROW

参考1
参考2

本文总阅读量