The use of sonar: use sonar-scanner to detect code

After configuring the sonar server, the next step is to use sonar to detect our code. Sonar mainly uses the client detection tool to detect the code, so to use sonar, we must configure the client detection tool locally.

The client can scan and analyze through the IDE plug-in, Sonar-Scanner plug-in, Ant plug-in and Maven plug-in. Sonar-Scanner is used here, sonar's command-line analysis software and Sonar-Runner are similar to Scanner.

Download Scanner from the sonar official website , and configure the environment variables after decompression

Modify sonar-scanner.properties and add the following:


sonar.host.url=http://ip:9000(sonar的ip地址)
sonar.sourceEncoding=UTF-8
sonar.jdbc.url=jdbc:mysql://ip:3306/sonar?useUnicode=true&characterEncoding=utf8   
sonar.jdbc.username=your_username
sonar.jdbc.password=your_password
sonar.login=admin  

sonar.password=admin 

Create a new sonar-project.properties in the root directory of the project to be detected, with the following contents:

# must be unique in a given SonarQube instance
sonar.projectKey=your_project(自己定义)
# this is the name displayed in the SonarQube UI
sonar.projectName=your_project(自己定义)
sonar.projectVersion=1.0
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# Since SonarQube 4.2, this property is optional if sonar.modules is set. 
# If not set, SonarQube starts looking for source code from the directory containing 
# the sonar-project.properties file.
sonar.sources=src(代码目录)
sonar.java.binaries=target(classes文件目录,不加会报错)
sonar.language=java
# Encoding of the source code. Default is default system encoding

sonar.sourceEncoding=UTF-8

Then, open the command line in the project root directory, enter sonar-scanner to run, you can see that the detection is successful, and you can see the detection results on the sonar homepage

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325711754&siteId=291194637