Firstly, you need to have a SonarQube Server to analyse your projects.
There are many ways you can run a SonarQube Server locally or remotely. The details you can find from https://docs.sonarqube.org/latest/.
Here is a fast way to run a SonarQube locally.
1. prepare the database
-
CREATE
DATABASE
sonar
CHARACTER
SET
utf8
COLLATE
utf8_general_ci;
CREATE
USER
'sonar'
IDENTIFIED
BY
'sonar'
;
GRANT
ALL
ON
sonar.*
TO
'sonar'
@
'%'
IDENTIFIED
BY
'sonar'
;
GRANT
ALL
ON
sonar.*
TO
'sonar'
@
'localhost'
IDENTIFIED
BY
'sonar'
;
FLUSH
PRIVILEGES
;
2.Download the sonar zip file (
https://www.sonarqube.org/success-download-community-edition/)
3.Config the SonarQube
cd
conf/sonar-runner.properties- add below configuration
sonar.host.url=http://localhost:9000
sonar.sourceEncoding=UTF-8
sonar.login=admin
sonar.password=admin
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
4. cd bin & ./macosx-universal-64/bin/sonar.sh
5. open localhost:9000 in browser, use admin/admin to loggin
6. Generate a token which will be used in later steps.
So far, the Sonar server is running. Then what we need to do next is to let sonar analyse projects. Now let's do magic thing in our project. Take maven project as an example.
1. add plugin to you pom.xml
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.2</version>
<executions>
<execution>
<id>report</id>
<goals>
<goal>report-aggregate</goal>
</goals>
<phase>verify</phase>
</execution>
</executions>
</plugin>
2. run mvn clean test jacoco:report (or maven clean install)
then you can find jacoco.exec and site/jacoco/XXX from target directory.
3. mvn sonar:sonar -Dsonar.host.url=http://localhost:9000 -Dsonar.login=token (token was created in the former step 6)
4. now you find details about your codes from localhost:9000