SonarQube安装教程与简单使用(基于Centos7,JDK1.8)

SonarQube

若要转载本文,请务必声明出处:https://www.cnblogs.com/zhongyuanzhao000/p/11686522.html

概念:

1571107400565
SonarQube是一种自动代码审查工具,用于检测代码中的错误,漏洞和代码异味。它可以与您现有的工作流程集成,以便在项目分支和拉取请求之间进行连续的代码检查。
优点:对大量的持续集成工具提供了接口支持,可以很方便地在持续集成中使用 Sonar;集成不同的测试工具,代码分析工具,以及CI工具,比如pmd-cpd、checkstyle、findbugs、Jenkins;

在CentOS系统中安装SonarQube:

环境需求:Jdk 1.8MySQL

另外,本次实践的CentOS7服务器的IP为:10.141.211.174

  1. 数据库配置:

    进入mysql,创建数据库sonar,密码为sonar
    $ mysql -u root -p
    
    mysql> CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
    mysql> CREATE USER 'sonar' IDENTIFIED BY 'sonar';
    mysql> GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar';
    mysql> GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar';
    mysql> FLUSH PRIVILEGES;
    
  2. 使用wget命令下载安装包,链接如下:

    sonarqube:         https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-7.2.zip
    sonar-scanner-cli:   https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-2.8.zip
    可以自己更改需要的版本号(进入地址的Distribution/中进行查看)
    

    具体命令如下:

    # 下载压缩包并解压
    cd /usr/local
    wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-7.2.zip
    unzip sonarqube-7.2.zip
    
    # 添加用户sonar,并更改 该目录 的owner(原因:sonarqube中的es不许以root启动,故以sonar用户来启动)
    useradd sonar
    chown  -R  sonar. /usr/local/sonarqube-7.2
    
  3. 编辑sonarqube配置文件

    vi /usr/local/sonarqube-7.2/conf/sonar.properties
    相应的修改处如下:
    1.
    # User credentials.
    # Permissions to create tables, indices and triggers must be granted to JDBC user.
    # The schema must be created first.
    sonar.jdbc.username=sonar
    sonar.jdbc.password=sonar
    
    2.
    #----- DEPRECATED
    #----- MySQL >=5.6 && <8.0
    # Support of MySQL is dropped in Data Center Editions and deprecated in all other editions
    # Only InnoDB storage engine is supported (not myISAM).
    # Only the bundled driver is supported. It can not be changed.
    sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
    
    3.
    # Binding IP address. For servers with more than one IP address, this property specifies which
    # address will be used for listening on the specified ports.
    # By default, ports will be used on all IP addresses associated with the server.
    sonar.web.host=10.141.211.174 (填写本机IP)
    

    然后保存并退出。

  4. 运行脚本启动服务

    cd /usr/local/sonarqube-7.2/
    # 以普通用户sonar启动服务,不然es启动会报错,用法:console、start、status、stop...
    su sonar bin/linux-x86-64/sonar.sh start
    
    # 查看状态,但这个状态只是暂时的,并不可信
    su sonar bin/linux-x86-64/sonar.sh status
    # 跟踪日志,确保启动成功(先跟着sonar.log日志,如果报es错误,可以去查看es.log;如果报了web错误,那么就是查看web.log)
    tail -f logs/sonar.log
    
  5. 登录web端:

    在浏览器输入:http://IP:9000 ,即可成功进入(初始用户:admin ,初始密码:admin,设置好token即可)。

安装Sonar-Scanner:

  1. 下载压缩包并解压(最好用sonar-scanner-2.8版本,支持jdk1.8,否则其他版本会出错)

    cd /usr/local
    wget https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-2.8.zip
    unzip sonar-scanner-2.8.zip  # 解压后,目录名改为sonar-scanner
    
  2. 编辑 /etc/profile 文件

    把以下配置添加到文件末尾,如下:
    
    #set sonar-scanner environment
    export SONAR_SCANNER_HOME=/usr/local/sonar-scanner
    export PATH=${SONAR_SCANNER_HOME}/bin:${PATH}
    
    然后执行命令: source /etc/profile
    
  3. 查看sonar-scanner版本:sonar-scanner -v
    然后根据提示,编辑sonar-scanner.properties文件,如下:

    vi /usr/local/sonar-scanner/conf/sonar-scanner.properties
    
    修改SonarQube server的地址,改为前面SonarQube的地址(我的是http://10.141.211.174:9000)
    #----- Default SonarQube server
    sonar.host.url=http://10.141.211.174:9000
    
    去掉mysql的注释
    #----- MySQL
    sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&amp;characterEncoding=utf8
    

尝试使用SonarQube:

  1. 保证SonarQube已经启动,若没有启动可以运行:su sonar /usr/local/sonarqube-7.2/bin/linux-x86-64/sonar.sh start

  2. 克隆一个项目到本地:git clone xxx

  3. 在该项目的根目录中,创建文件 sonar-project.properties,并对其进行编辑,编辑内容如下:

    # must be unique in a given SonarQube instance
    sonar.projectKey=simple-java-maven-app
    sonar.projectName=simple-java-maven-app
    sonar.projectVersion=1.0
    sonar.sources=src
    sonar.language=java
    sonar.sourceEncoding=UTF-8
    sonar.java.binaries=/usr/local/workspace/simple-java-maven-app/target/classes
    
  4. 使用sonar-scanner进行分析,在项目根目录中执行命令:sonar-scanner,然后就可以到http://10.141.211.174:9000 查看分析结果了。
    除了sonar-scanner命令,我们也可以使用maven的命令来实现同样的代码分析效果,命令如下:

    mvn sonar:sonar \
      -Dsonar.host.url=http://10.141.211.174:9000 \
      -Dsonar.login=acfa9b0585a3c0a10366826143edebf4abd36f6b   # 这个是sonarqube登录时的token
    

参考:

https://www.cnblogs.com/ding2016/p/8065241.html
https://www.cnblogs.com/owenma/p/7891170.html
https://blog.csdn.net/qq_21816375/article/details/80787993

posted @ 2019-10-16 16:50  <予安>  阅读(5010)  评论(1编辑  收藏  举报