Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to install and deploy Sonar

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article is to share with you about how to install and deploy Sonar, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Preface

Sonar is an open platform for code quality management. Through the plug-in mechanism, Sonar can integrate different testing tools, code analysis tools and continuous integration tools. Different from continuous integration tools (such as Hudson/Jenkins, etc.), Sonar does not simply display different code inspection results (such as FindBugs, PMD, etc.) directly on the web UI interface, but reprocesses these results through different plug-ins and measures the change of code quality in a quantitative way, so that it is convenient to manage code quality for projects of different sizes and types.

In terms of support for other tools, Sonar not only provides support for IDE, you can view the results online in tools such as Eclipse and IntelliJ IDEA; at the same time, Sonar also provides interface support for a large number of continuous integration tools, which makes it easy to use Sonar in continuous integration.

In addition, Sonar plug-ins can also provide support for programming languages other than Java, as well as good support for internationalization and reporting documentation.

The function of Sonar is to check whether the code has BUG. In addition to checking whether the code has bug, there are other functions, such as: what is the comment rate of your code, some suggestions for code, and suggestions for writing syntax. So it's called quality management.

Downloads and documentation about Sonar can be moved to its official website.

First, deploy Sonar

Note: the next configuration is based on the blog deployment Jenkins+Gitlab to achieve continuous integration environment for deployment.

All the following source packages and plug-ins can be downloaded from this network disk link.

[root@jenkins src] # unzip sonarqube-5.6.zip [root@jenkins src] # mv sonarqube-5.6 / usr/local/sonarqube [root@jenkins src] # ln-s / usr/local/sonarqube/bin/linux-x86-64/sonar.sh / usr/local/bin II, install MySQL

Sonar needs to use the database. Here I use the MySQL database. If there is a database in the environment, there is no need to deploy, just create the corresponding library and account.

# deploy MySQL [root@jenkins mysql] # ls # using RPM packages to make sure these packages are available in the current directory You can download mysql-community-client-5.7.25-1.el7.x86_64.rpmmysql-community-common-5.7.25-1.el7.x86_64.rpmmysql-community-libs-5.7.25-1.el7.x86_64.rpmmysql-community-libs-compat-5.7.25-1.el7.x86_64.rpmmysql-community-server-5.7.25-1.el7.x8664.rpm in the link at the beginning of my article [root @ Jenkins mysql] # yum localinstall mysql-community-*-y # install MySQL [root @ jenkins mysql] # grep password / var/log/mysqld.log # check the root password of the database in the MySQL log 2019-11-18T14:11:15.565475Z 1 [Note] A temporary password is generated for root@localhost: qzg6X) qt%MQg# is the default root password of MySQL [root@jenkins mysql] # mysql- uroot-p'qzg6X) qt%MQg' # Log in to the database The password needs to be enclosed in single quotation marks # create the corresponding library and user mysql > alter user 'root'@'localhost' identified by' Ljz@123.com' Mysql > create database sonar character set utf8 collate utf8_general_ci;mysql > grant all on sonar.* to 'sonar'@'%' identified by' Ljz@123.com';mysql > grant all on sonar.* to 'sonar'@'localhost' identified by' Ljz@123.com';mysql > flush privileges 3. Configure Sonar [root@jenkins mysql] # cd / usr/local/sonarqube/conf/ [root@jenkins conf] # vim sonar.properties # Edit the main configuration file sonar.jdbc.username=sonar # define the user connecting to the database sonar.jdbc.password=Ljz@123.com # specify the password of the database user sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance# is used to define the address and port to connect to the database Sonar.web.port=9000 # specify its listening port # configure sonar to support Chinese pages and support PHP [root@jenkins src] # cd / usr/local/sonarqube/extensions/plugins/ [root@jenkins plugins] # cp / usr/src/sonar-l10n-zh-plugin-1.11.jar. [root@jenkins plugins] # cp / usr/src/sonar-php-plugin-2.9-RC1.jar. [root@jenkins conf] # sonar.sh start # launch sonar Give it a point to initialize the time [root@jenkins conf] # tail-2 / usr/local/sonarqube/logs/sonar.log # View the sonar log 2019.11.18 22:26:16 INFO ce [o.s.ce.app.CeServer] Compute Engine is up2019.11.18 22:26:16 INFO app [o.s.p.m.Monitor] Process [ce] is up# when the above two lines end with "up", sonar starts normally. [root@jenkins conf] # netstat-anpt | grep 9000 # make sure that the port is listening on tcp 00 0.0.0.0 anpt 9000 0.0.0.0 * LISTEN 10655/java

Client accesses sonar's web interface:

IV. Configure Code scanning function # configure Code scan [root@jenkins src] # unzip sonar-scanner-cli-3.3.0.1492-linux.zip [root@jenkins src] # mv sonar-scanner-3.3.0.1492-linux/ / usr/local/sonar-scanner [root@jenkins src] # ln-s / usr/local/sonar-scanner/bin/sonar-scanner / usr/local/bin/ [root@jenkins src] # ln-s / usr/local/sonar-scanner / bin/sonar-scanner / usr/bin [root@jenkins src] # cd / usr/local/sonar-scanner/conf/ [root@jenkins conf] # egrep-v'^ $| ^ # 'sonar-scanner.properties # modify this configuration file as follows: sonar.host.url= http://localhost:9000sonar.sourceEncoding=UTF-8# the following is copied from the main configuration file of sonar: / usr/local/sonarqube/conf/sonar.properties Used to connect to the database sonar.jdbc.username=sonarsonar.jdbc.password=Ljz@123.comsonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance. Test the code scanning function [root@jenkins src] # unzip testalyzer-master.zip [root@jenkins src] # cd testalyzer-master/projects/languages/php/php-sonar-runner-unit-tests/ [root@jenkins php-sonar-runner-unit-tests] # cat sonar-project.properties # to see what sonar.projectKey=org.sonarqube:php-ut-sq-scanner # custom keys are contained in the following file If the secret key is the same Will automatically overwrite the previous test results sonar.projectName=PHP:: PHPUnit:: SonarQube Scanner # web interface display name sonar.projectVersion=1.0 # version sonar.sources=src # package storage path sonar.tests=tests # test path sonar.language=php # language to be tested sonar.sourceEncoding=UTF-8 # Encoding format # Test PHP code [root@jenkins php-sonar-runner-unit- Tests] # pwd # confirm to test in the current path / usr/src/testalyzer-master/projects/languages/php/php-sonar-runner-unit-tests [root@jenkins php-sonar-runner-unit-tests] # sonar-scanner # Test js code [root@jenkins php-sonar-runner-unit-tests] # cd. /.. / javascript/javascript-sonar-runner [root@jenkins javascript-sonar-runner] # sonar-scanner # to test

When you have finished testing js and PHP, you can see the following in the web interface of sonar:

Click to enter to see the details:

6. Configure Jenkins to enable Sonar

To log in to the web interface of Jenkins, you need to install plug-ins. There are two ways: online installation and offline installation. I choose offline installation here. You can install online by yourself.

1. Click: system Management = "plug-in Management = =" Advanced, and then drop down the page:

Download the plug-ins I provide and add them in the following order. For online installation, search for "SonarQube Scanner", "Gerrit Trigger" and "Sonar Gerrit Plugin" to install them:

2. Click: system Management = system Settings, and then configure as follows:

3. Click: system Management = "Global tool configuration, and then click as follows:"

4. Enter the project built in the previous blog post, and then click enter, as follows:

Copy the code at the terminal:

[root@jenkins sonar-scanner] # cd / usr/src/testalyzer-master/projects/languages/php/php-sonar-runner-unit-tests# switch to this path [root@jenkins php-sonar-runner-unit-tests] # egrep-v'^ $| ^ # 'sonar-project.properties # copy the following code sonar.projectKey=org.sonarqube:php-ut-sq-scannersonar.projectName=PHP:: PHPUnit:: SonarQube Scannersonar.projectVersion=1.0sonar.sources=srcsonar.tests=testssonar.language=phpsonar.sourceEncoding=UTF-8sonar.php.coverage.reportPath=reports/phpunit. Coverage.xmlsonar.php.tests.reportPath=reports/phpunit.xml

5. Then paste the copied code in the figure (it is recommended to modify the value of "sonar.projectKey" in order to distinguish the test results):

6. Go back to the terminal and submit the code to gitlab to realize automatic sonar scanning.

[root@jenkins php-sonar-runner-unit-tests] # pwd/usr/src/testalyzer-master/projects/languages/php/php-sonar-runner-unit-tests [root@jenkins php-sonar-runner-unit-tests] # cp-r * ~ / test01/# copy the test code to the local git library (which I created in the previous blog post, if there is no local git library) You can clone it again) [root@jenkins php-sonar-runner-unit-tests] # cd ~ / test01/ # switch to the local git library [root@jenkins test01] # rm-rf sonar-project.properties # to delete this file, you don't need to use it to specify the code to be tested # because we have already written its configuration file in the web interface in the web interface. Priority is given to the detection code configured by the web interface. # it's OK not to delete this file # then submit it to the remote gitlab library [root@jenkins test01] # git add * [root@jenkins test01] # git commit-m "test sonar" [root@jenkins test01] # git push origin master

When submitted to the remote gitlab library, you can see that the web interface of sonar has been scanned and the scan result is displayed as follows:

7. Configure Jenkins mail alarm

Here is a screenshot of all the operations, because after I have finished, there are some minor problems, it is already 12:30 in the morning, lazy row wrong, you can combine the following picture to call the police by email:

Start configuring mail alarms:

In the web interface of Jenkins, click: system Management = system Settings, and then enter the email address of the system administrator below and save:

Click again: system Management = system Settings

Configure the project as follows:

Add post-build actions:

In fact, it is to add two actions after construction, as follows (finally, there is something wrong with my email alarm. It is recommended to add the following E-mail Notification and Editable Email Notification in turn. Do not add these two together):

Then on the drop-down page, click as follows:

At this point, the mailbox alarm is configured. At this point, you can close gitlab in the console or manually build the code to test whether you can receive the alarm email.

The above is how to install and deploy Sonar, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report