Sonar配置与使用

一、前置条件:

安装工具如下:

  • JDK
  • MySql服务器
  • SonarQube
  • SonarScanner

从官网下载安装包进行安装,具体安装步骤略

二、配置

MySql配置:

打开SQL,创建sonar数据库,sonar用户,并赋予sonar用户sonar数据库权限;

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;

Ps:mysql8.0以上版本配置如下

CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
delete from mysql.user where user='sonar';  //如果存在sonar用户,先删除再创建,无sonar用户,略过此步
CREATE USER 'sonar'@'localhost' IDENTIFIED BY 'sonar';
CREATE USER 'sonar'@'%' IDENTIFIED BY 'sonar';

GRANT ALL ON sonar.* TO 'sonar'@'%';
GRANT ALL PRIVILEGES ON sonar.* TO 'sonar'@'localhost';
flush privileges;

Sonar、SonarScanner环境变量配置:

添加SONAR_HOME、SONAR_RUNNER_HOME环境变量,并将SONAR_RUNNER_HOME加入PATH

修改sonar配置文件sonar.properties

# 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

#----- Embedded Database (default)
# H2 embedded database server listening port, defaults to 9092
#sonar.embeddedDatabase.port=9092
#----- MySQL 5.6 or greater
# 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=true
#Optional properties
sonar.jdbc.driverClassName=com.mysql.jdbc.Driver
View Code

修改SonarScanne配置文件sonar-scanner.properties

#----- Default SonarQube server
sonar.host.url=http://localhost:9000

#----- Default source code encoding
sonar.sourceEncoding=UTF-8

#----- Global database settings (not used for SonarQube 5.2+)
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar

#----- PostgreSQL
#sonar.jdbc.url=jdbc:postgresql://localhost/sonar

#----- MySQL
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8

#----- Oracle
#sonar.jdbc.url=jdbc:oracle:thin:@localhost/XE

#----- Microsoft SQLServer
#sonar.jdbc.url=jdbc:jtds:sqlserver://localhost/sonar;SelectMethod=Cursor
#----Security(when 'sonar.forceAuthentication'is set to 'true')
sonar.login=admin
sonar.password=admin
View Code

三、启动

进入sonar的bin目录找到sonar启动文件,启动sonar服务

输入localhost:9000访问,页面展示如图

三、使用

通过maven进行集成

修改maven配置文件setting.xml

<?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:/repository</localRepository>

  <pluginGroups>
    
    <pluginGroup>org.sonarsource.scanner.maven</pluginGroup>
  </pluginGroups>

 
  <proxies>
    
  </proxies>
  <servers>
    
      <id>siteServer</id>
      <privateKey>/path/to/private/key</privateKey>
      <passphrase>optional; leave empty if not used.</passphrase>
    </server>
    -->
    <server>
      <id>releases</id>
      <username>admin</username>
      <password>admin123</password>
    </server>

  </servers>

  <mirrors>
     <mirror>
        <id>nexus</id>
        <name>nexus Mirror,3rd party,chenshu repository</name>
        <url>http://192.168.102.92:8081/nexus/content/groups/public/</url>
        <mirrorOf>central</mirrorOf>
   </mirror>   
  </mirrors>

  <profiles>
  <profile>
        <id>jdk1.8</id>
        <activation>
            <activeByDefault>true</activeByDefault>
            <jdk>1.8</jdk>
        </activation>
        <properties>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
            <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
        </properties>
        <repositories>
            <repository>
                <id>snapshots</id>
                <url>http://192.168.102.92:8081/nexus/content/repositories/snapshots/</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                      <updatePolicy>always</updatePolicy>
                </snapshots>
            </repository>
        </repositories>
    </profile>
    <profile>
           <id>sonar</id>
           <activation>
               <activeByDefault>true</activeByDefault>
           </activation>
           <properties>
               <!-- Optional URL to server. Default value is http://localhost:9000 -->
               <sonar.jdbc.url>jdbc:mysql://localhost:3306/sonar</sonar.jdbc.url>  
               <sonar.jdbc.driver>com.mysql.jdbc.Driver</sonar.jdbc.driver>  
               <sonar.jdbc.username>sonar</sonar.jdbc.username>  
               <sonar.jdbc.password>sonar</sonar.jdbc.password>  
               <sonar.host.url>http://localhost:9000</sonar.host.url>  
           </properties>
       </profile>
  </profiles>
    <activeProfiles>
            <activeProfile>sonar</activeProfile>
    </activeProfiles>

</settings>
View Code

然后把配置文件copy至C:\Users\Administrator\.m2目录

进入项目pom文件所在目录,地址栏输入cmd,进入命令窗口

执行mvn sonar:sonar

执行成功会把分析结果保存至数据库,通过浏览sonar地址访问

 

转载于:https://www.cnblogs.com/chenchen-tester/p/7268703.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在GitLab中配置Sonar,需要按照以下步骤进行操作: 1. 首先,确保已经在服务器上成功安装并运行了SonarQube。可以通过访问SonarQube的URL来验证其是否成功运行。 2. 进入GitLab的项目页面,并点击左侧菜单中的“Settings”选项。 3. 在“Settings”页面上,点击“CI/CD”选项卡,并找到“General pipelines settings”部分。 4. 在“General pipelines settings”部分,找到“Secret variables”子选项,并点击“Expand”按钮。 5. 点击“Add variable”按钮,输入以下变量信息: - Key:SONAR_TOKEN - Value:SonarQube中的访问令牌,用于连接GitLab和SonarQube。 6. 点击“Add variable”按钮以保存配置变量。 7. 返回到GitLab项目页面,并找到代码仓库中的“.gitlab-ci.yml”文件。 8. 编辑该文件,并添加以下配置信息: ```yaml sonarqube: image: sonarsource/sonar-scanner-cli script: - sonar-scanner -Dsonar.login=$SONAR_TOKEN -Dsonar.projectKey=项目标识符 ``` 其中,“项目标识符”需要替换为你在SonarQube中创建的项目标识符。 9. 保存并提交更改。GitLab会自动触发CI/CD流水线,将代码提交到SonarQube进行代码质量分析。 10. 在GitLab的项目页面上,点击左侧菜单中的“CI/CD”选项,可以查看CI/CD流水线的执行情况。 配置完成后,GitLab将会在每次代码推送到仓库时,自动触发SonarQube执行代码质量分析,并将分析结果反馈给GitLab。这样就可以更好地管理和控制代码质量,提高项目的软件开发效率。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值