preface

Code review tool, many advantages of Google, hahaha 🤣🤣🤣.

To prepare

The following uses the Installation of Sonar on MAC as an example. However, it is only a front-end installation process and does not involve back-end data installation.

  • Use the latest versionSonarQube 8.5.1(Note the version on the official websiteA prerequisite for);
  • The version in use is requiredjavaVersion 11;
  • SonarQubeIf you need mysql, please download version 7.8, because 7.8 or later versions do not support mysql database.
  • SonarQube versions above 7.7 are no longer supported in projectssonar.analysis.mode=preview; (If the higher version does not use the database, go to preview mode)
  • Pay more attention to the log, there are error and other abnormal information;/ usr/local/sonarqube - 8.5.1.38104 / logs/sonar log
  • SonarQube profile directory:/ usr/local/sonarqube 8.5.1.38104 / conf/sonar. The properties

1. The JDK installation

Download JDK

2. The sonar – SonarQube installation

Sonar -SonarQube download address

General download community version can be, conditionally can choose other;

  1. Download complete: unzip to/usr/local ;
  2. Terminal access:/ usr/local/sonarqube 8.5.1.38104 / bin/macosx - universal - 64 ;
  3. Start, stop, or restart the service
    • ./sonar.sh startStart the service
    • ./sonar.sh statusCheck the status
    • ./sonar.sh stopStop the service
    • ./sonar.sh restartRestart the service
  4. Open the urlhttp://localhost:9000
    • Login (initial account: admin Initial password: admin) :
  5. localization
    • The search ofchineseAnd theninstall ;
    • differentsonarQubeThere may be some differences in the version pages, but the general installation is the same;

After the installation is successful, restart the sonarQube service and visit http://localhost:9000/ again. You can see the Chinese interface.

3. Sonar -scanner installation

Sonar -scanner Scanner download address

  1. Download complete: decompress to /usr/local;

  2. Configure environment variables:

    • Run the vi ~/.bash_profile command to go to the bash.profile file and enter the text mode (press I).

    • Configure sonar_HOME, sonar_scanner_HOME and PATH (replace the absolute path of your sonarqube and sonar-scanner files) as follows:

      Export SONAR_HOME = / usr/local/sonarqube - 8.5.1.38104 export SONAR_SCANNER_HOME = / usr/local/sonar - scanner - 4.5.0.2216 - macosx export PATH=$PATH:$SONAR_SCANNER_HOME/binCopy the code
    • Exit the text editing mode (press Esc or :wq) and run the source ~/. Bash_profile command for the configuration to take effect immediately.

    • Test whether the installation of sonar-scanner is successful, enter the command: sonar-scanner -v, the successful installation is as shown in the following figure:

4. Use sonar

Create a project

ProjectKey is unique

Add a file named sonar-project.properties to the root directory of the project you want to review.

Sonar.projectkey = sonar.projectName= sonar.sources= SRC sonar.projectversion =0.1.0 sonar.host.url=http://localhost:9000 sonar.login= sonar.exclusions=**/*.less,assets/images/**,assets/less/**Copy the code

Project implementation

Use the command line tool to go to the root directory of the project and run the sonar-scanner command.

The results view

5. Add the rule file of Sonar

  1. Log in to the SonarQube platform with the admin account (initial account: admin initial password: admin) :

  2. Enter the configuration page from the Quality Configuration menu.

  1. Enter the rule name, select the language, and click Create:

  1. Creation completed:

  1. Go to Quality Configuration and find the language of your choice:

  1. Set the rule file for your own additions:

  1. You can set your own rules as the default rules to activate the rules you want

  2. Another way to add a rule file: you can copy an existing rule directly

The difference between the two types of adding rule files:

The new rule file will copy all rules from that rule file. The new rule file is empty.Copy the code
Js, CSS and other languages can only now in the existing rules to activate the rules you want, can not add the rule table does not have the rule (I have not found a custom rule method), Java and other languages can upload their own definition of the rule file for custom rules.Copy the code

To view existing rules for a language:

Select language from Code Rules to view all existing rules in the list:

6. Other

SonarQube configuration file directory: / usr/local/sonarQube 8.5.1.38104 / conf/sonar. The properties

If you want to configure the database or other parameters, open the above file:

. #-------------------------------------------------------------------------------------------------- # DATABASE # # IMPORTANT: # - The embedded H2 database is used by default. It is recommended for tests but not for # production use. Supported databases are Oracle, PostgreSQL and Microsoft SQLServer. # - Changes to database connection URL (sonar.jdbc.url) can affect SonarSource licensed products. # 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. JDBC. Password = sonar.. JDBC url = JDBC: mysql: / / 127.0.0.1:3306 / sonar? useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance sonar.jdbc.username=sonar  sonar.jdbc.password=sonar sonar.sorceEncoding=UTF-8 sonar.login=admin sonar.password=admin ...Copy the code

The last

Above, also is all kinds of Baidu, all kinds of Google, and step on a lot of pit to write out, if there is a better solution, welcome guidance.