MAC SonarQube Static Code Check

SonarQube is an open platform for managing code quality to quickly locate potential or obvious errors in your code. The installation, configuration, and use of this tool are described below.(This article refers to others'writings and adds a summary of your own problems) Dead work; 1. jdk (please Baidu yourself, very easy) 2,sonarqube: http://www.sonarqube.org/downloads/ 3,SonarQube+Scanner:https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/sonar-scanner-2.5.zip 4. mysql database (see: http://www.jianshu.com/p/0dace9c19333) ###1, Installation Paper 1. After downloading the sonarqube, unzip the directory, go to the sonarqube directory from the command line and start

Command: sudo {directory above sonarqube}/sonarqube/bin/macosx-universal-64/sonar.sh start Enter Mac secret code to start sonarqube

2. Browser Access http://localhost:9000, if the image below indicates successful installation.

###2. Configuration Paper 1. Open mysql and create a new database

On the command line, do the following: 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. Open the sonar.properties file

CD /{directory above sonarqube}/sonarqube-6.5/conf/sonar.properties

3. Add the following information:

sonar.jdbc.username=root sonar.jdbc.password=123456 sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance

url is the database connection address, username is the database user name, and jdbc.password is the database password.

4. Restart the sonarqube service and access it again http://localhost:9000, which is slightly slower because the database information needs to be initialized

Restart sonarqube service command Sudo {directory above sonarqube}/sonarqube/bin/macosx-universal-64/sonar.sh restart

5. Log on (admin/admin) after successful database initialization 6. Follow the click order shown below to enter the plug-in installation page 7. Search chinese Pack and install Chinese Language Pack 8. After successful installation, restart the sonarqube service and access it again http://localhost:9000/, you can see the Chinese interface 3. Usage Papers 1. Open the sonar-runner.properties file

/{directory up to sonar-scanner}/sonar-scanner-2.5/conf/sonar-runner.properties

2. Enter the following information under the MySQL node

sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance

Note: If the test project is not on the same machine as the server, you need to add the IP of the server:

/#----- Default SonarQube serversonar.host.url=http://XXX.XXX.XXX.XXX:9000

3. Configuring environment variables a. Enter sudo vim ~/.bash_profile on the command line and paste the following information

export PATH=$PATH:/Applications/sonarqube-6./bin export PATH=$PATH:/Applications/sonar-scanner-2.5/bin b. After saving, execute the following command source ~/.bash_profile

4. Enter the project directory to be checked and create a new sonar-project.properties 5. Enter the following information:

# must be unique in a given SonarQube instance sonar.projectKey=Autobom # this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1. sonar.projectName=bom 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=. # Encoding of the source code. Default is default system encoding sonar.sourceEncoding=UTF-8

Where: projectName is the project name, and sources is the directory where the source file is located 6. Execute the command under the current project directory: sonar-runner, the following image will appear after the analysis is successful 8. Open http://localhost:9000/, and we'll see a summary of the analysis projects on the home page

In anticipation of more use, please log on to the official website to learn by yourself. http://www.sonarqube.org/ http://docs.sonarqube.org/display/SONAR/Analyzing+with+SonarQube+Scanner Problems encountered by ## #####Pit 1: The following error occurred while performing code detection (sonar-runner)

Caused by: com.mysql.jdbc.PacketTooBigException: Packet for query is too large (8488572 > 4194304). You can change this value on the server by setting the max_allowed_packet' variable.

Solution:

  1. View the maximum allowable package size for mysql, and execute the following command to see the current allowable size for mysql:

show VARIABLES like '%max_allowed_packet%';

  1. Since my.cnf doesn't come with Mac, we can create this new file, which is created in the mysql u directory by default

Cd/usr/local/mysql/etc/ (You can also create a new one without an etc directory) touch my.cnf Open my.cnf and enter the following information: [mysqld] max_allowed_packet=32M

  1. Restart mysql

sudo /usr/local/mysql/support-files/mysql.server restart

  1. Restart sonarqube here

Sudo {directory above sonarqube}/sonarqube/bin/macosx-universal-64/sonar.sh restart

####Pit 2: Prerequisite: The code being checked is JAVA, and the following error occurs when executing the command sonar-runner:

ERROR: Error during SonarQube Scanner execution ERROR: Please provide compiled classes of your project with sonar.java.binaries property ERROR: ERROR: To see the full stack trace of the errors, re-run SonarQube Scanner with the -e switch. ERROR: Re-run SonarQube Scanner using the -X switch to enable full debug logging.

Solution: Modify the sonar-project.properties properties file as follows:

#required metadata
#Unique identification of project key project, cannot be duplicated
sonar.projectKey=Auto
sonar.projectName=YingkeAuto
sonar.projectVersion=1.0
sonar.sourceEncoding=UTF-8
sonar.modules=java-module # Java module
java-module.sonar.projectName=Java Module
java-module.sonar.language=java
# Represents the directory specified by projectBaseDir
java-module.sonar.sources=.
java-module.sonar.projectBaseDir=src
sonar.binaries=classes sonar.java.binaries=/Users/forkey/Documents/Tool/Workspace/YingkeAuto/target/classes,/Users/forkey/Documents/Tool/Workspace/YingkeAuto/target/test-classes sonar.java.test.binaries=/Users/forkey/Documents/Tool/Workspace/YingkeAuto/target/test-classes

Tags: MySQL Java Database JDBC

Posted on Thu, 19 Mar 2020 03:08:31 -0400 by kentopolis