Docker builds the code detection platform SonarQube and detects the process of Maven projects

Docker builds the code detection platform SonarQube and detects the process of Maven projects

1 Introduction

Good coding habits are qualities that a good programmer should possess, but relying on people's habits and memory to ensure code quality is not always a reliable thing. People in the computer industry should be well aware that as long as it is man-made, there will be operational risks. This article explains how to build the code detection platform SonarQube through Docker and use it to detect the code of maven projects.

2 Install SonarQube with Docker

2.1 Installation

Installation through Docker is quick and easy. You can simply delete the container and image when you don’t need it.

# Pull the Sonar image docker pull sonarqube:8.3.1-community
# Run the example docker run --name sonarqube -p 9000:9000 -d sonarqube:8.3.1-community

Then visit: http://localhost:9000/, the default administrator user and password are: admin/admin .

Here we choose the free Community version. There are also paid versions such as Developer and Enterprise , which have more powerful functions. The specific differences are as follows:

2.2 Specifying a database

Generally we will start a database such as Oracle , MySQL or PostgreSQL ourselves. Relevant system information can be viewed in Administration-System . If we do not specify, the embedded H2 database is used by default. If you want to specify another database, you need to specify it when starting Docker:

-e SONARQUBE_JDBC_USERNAME="xxx" \
-e SONARQUBE_JDBC_PASSWORD="***" \
-e SONARQUBE_JDBC_URL="jdbc:mysql://xxx"

Using the H2 database has the following limitations:

The embedded database can only be used in testing scenarios. The embedded database cannot be extended or upgraded to new versions of SonarQube, and does not support migrating your data to other database engines.

Therefore, it is recommended not to use the embedded H2 database in actual use.

2.3 Entering the container

By command:

$ docker exec -it container_id bash

bash-5.0# ls
COPYING bin conf data elasticsearch extensions lib logs temp web
bash-5.0#

Can enter the SonarQube container. In the directory /opt/sonarqube you can view configuration files, plug-ins, data files, log files, etc. The actual use should be mapped to the directory of the host machine, so that when you restart an instance, the data and configuration are still there.

2.4 Installing plugins

SonarQube provides powerful plug-in management functions. Taking the Chinese language pack as an example, we will explain how to install the plug-in:

Administration-Marketplace-Plugins , enter Chinese in the search box and choose to install it.

When the status shows Install Pending , the plug-in installation is complete. Click Restart Server to take effect.

3. Detect code through Maven

3.1 Use by account and password

Specify the address of the SonarQube platform and specify the username and password to detect the code. The specific commands are as follows:

mvn clean verify sonar:sonar -Dsonar.host.url=http://localhost:9000 -Dsonar.login=admin -Dsonar.password=admin

3.2 Use via Token

Of course, it is not a good habit to directly use admin and expose the password. You can create a user and a token through配置-權限-用戶.

Copy the token: 9656c84090b2481db6ea97b6d14d87d546bff619 .

In this way, you can operate through the token:

mvn clean verify sonar:sonar -Dsonar.host.url=http://localhost:9000 -Dsonar.login=9656c84090b2481db6ea97b6d14d87d546bff619

After executing the command, a new project will be automatically created on the interface and the detection results will be given:

Sonar provides many indicators such as test coverage, complexity, etc., which can greatly help us write better code:

4 Conclusion

SonarQube is powerful and is one of the important DevOps tools that needs to be understood and mastered.

This is the end of this article about using Docker to build the code detection platform SonarQube and detect Maven projects. For more related content about using Docker to build the code detection platform SonarQube and detect Maven projects, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to build sonarqube using docker

<<:  VUE implements token login verification

>>:  Solve the problem of invalid utf8 settings in mysql5.6

Recommend

How to turn off eslint detection in vue (multiple methods)

Table of contents 1. Problem Description 2. Probl...

Summary of 6 solutions for implementing singleton mode in JS

Preface Today, I was reviewing the creational pat...

MySQL master-slave principle and configuration details

MySQL master-slave configuration and principle, f...

Details of various font formats in HTML web pages

This section starts with the details of text modi...

JavaScript canvas text clock

This article example shares the specific code of ...

vue-electron problem solution when using serialport

The error is as follows: Uncaught TypeError: Cann...

Docker installation and deployment example on Linux

After reading the following article, you can depl...

How to build a tomcat image based on Dockerfile

Dockerfile is a file used to build a docker image...

Pygame code to make a snake game

Table of contents Pygame functions used Creating ...

Nexus private server construction principle and tutorial analysis

one. Why build a Nexus private server? All develo...

Solution to the low writing efficiency of AIX mounted NFS

Services provided by NFS Mount: Enable the /usr/s...