How to use SonarQube

Firstly, you need to have a SonarQube Server to analyse your projects.

There are many ways you can run a SonarQube Server locally or remotely. The details you can find from https://docs.sonarqube.org/latest/

Here is a fast way to run a SonarQube locally. 

1. prepare the database

  •     CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
  • CREATE USER 'sonar' IDENTIFIED BY 'sonar';
  • GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar';
  • GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar';
  • FLUSH PRIVILEGES;

2.Download the sonar zip file (https://www.sonarqube.org/success-download-community-edition/)

3.Config the SonarQube

  • cd conf/sonar-runner.properties
  • add below configuration
sonar.host.url=http://localhost:9000
sonar.sourceEncoding=UTF-8
sonar.login=admin
sonar.password=admin
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar

 4. cd bin & ./macosx-universal-64/bin/sonar.sh

5. open localhost:9000 in browser, use admin/admin to loggin

6. Generate a token which will be used in later steps.

So far, the Sonar server is running. Then what we need to do next is to let sonar analyse projects. Now let's do magic thing in our project. Take maven project as an example.

1. add plugin to you pom.xml

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.2</version>
    <executions>
         <execution>
             <id>report</id>
             <goals>
                 <goal>report-aggregate</goal>
             </goals>
             <phase>verify</phase>
         </execution>
     </executions>
</plugin>

2. run mvn clean test jacoco:report (or maven clean install)

    then you can find jacoco.exec and site/jacoco/XXX from target directory. 

3.  mvn sonar:sonar -Dsonar.host.url=http://localhost:9000 -Dsonar.login=token (token was created in the former step 6)

4. now you find details about your codes from localhost:9000

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值