1 introduction
Good code habits are a quality that a good programmer should have, but it is always not a reliable thing to rely on people’s habits and memory to ensure the quality of code. In the computer industry should know, as long as it is artificial, there will be operational risk. This article shows you how to build SonarQube with Docker and use it to test your Maven project code.
2 Docker install SonarQube
2.1 installation
Install by Docker, easy and fast, when you don’t need to directly delete containers and images.
Docker run --name sonarqube -p 9000: 9000-d Sonarqube: 8.3.1 - communityCopy the code
Then go to http://localhost:9000/. The default administrator user name and password are admin/admin.
Here choose the free Community version Community, in addition to Developer, Enterprise and other paid versions, more powerful, specific differences are as follows:
2.2 Specifying a Database
We usually start a database ourselves like Oracle, MySQL or PostgreSQL. The related System information can be viewed in the administration-system. If we do not specify it, the default is to use the embedded H2 database. If you want to specify another database, you need to specify when starting Docker:
-e SONARQUBE_JDBC_USERNAME="xxx" \
-e SONARQUBE_JDBC_PASSWORD="***" \
-e SONARQUBE_JDBC_URL="jdbc:mysql://xxx"Copy the code
Using an H2 database has the following limitations:
Embedded databases can only be used in test scenarios. The embedded database cannot be expanded or upgraded to a new version of SonarQube, and there is no support for migrating your data to another database engine.
Therefore, it is recommended not to use the embedded H2 database.
2.3 Entering containers
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#Copy the code
You can enter SonarQube containers. Configuration files, plug-ins, data files, and log files can be viewed in /opt/sonarqube. The actual use should be mapped to the host directory so that an instance can be restarted with data and configuration still in place.
2.4 Installing plug-ins
SonarQube provides powerful plug-in management functions. Using the Chinese language package as an example, explain how to install plug-ins:
Administration-marketplace -Plugins, type Chinese in the search box to select install.
If the status is Displayed as Install Pending, the plug-in is installed. Click Restart Server to take effect.
3 Check the code using Maven
3.1 Using the Account and Password
SonarQube: SonarQube: SonarQube: SonarQube: SonarQube: SonarQube
mvn clean verify sonar:sonar -Dsonar.host.url=http://localhost:9000 -Dsonar.login=admin -Dsonar.password=adminCopy the code
3.2 Using a Token
Of course, it’s not a good idea to use admin directly and expose the password. You can create a user by configuring – Permissions – User and creating a token.
Copying the token: 9656 c84090b2481db6ea97b6d14d87d546bff619.
Thus, we can operate with tokens:
mvn clean verify sonar:sonar -Dsonar.host.url=http://localhost:9000 -Dsonar.login=9656c84090b2481db6ea97b6d14d87d546bff619Copy the code
After executing the command, an item will be automatically created on the interface and the detection result will be given:
Sonar provides many metrics such as test coverage, complexity and so on, which can greatly help us write better code:
4 summarizes
SonarQube is a powerful tool for DevOps that needs to be understood and mastered.
Visit pumpkin Talk www.pkslow.com for more exciting articles!
Welcome to pay attention to the wechat public number “Pumpkin slow Talk”, will continue to update for you…
Read more and share more; Write more. Organize more.