April 7, 2020
Estimated Post Reading Time ~

How to integration SonarQube and JaCoCo

SonarQube Report

Why is code quality important?

Code quality is important for overall software quality as it impacts how safe, secure, and reliable your codebase is. Poorly written code is always more expensive to maintain. Tools such as SonarLint and SonarQube etc. can be used to measure and analyze the quality of code.

What is SonarQube?

SonarQube (formerly known as Sonar) is an open-source platform for continuous inspection of code. It ensures code quality, reliability, and maintainability over the life-span of the project. It supports 25+ languages such as Java, C/C++, C#, PHP, Flex, Groovy, JavaScript, Python, PL/SQL, COBOL, etc., It offers reports like:
  • Duplicated code
  • Dead code
  • Inappropriate coding standards
  • Unit tests – Code coverage
  • Code complexity
  • Potential bugs
  • Commented code etc.,

Prerequisites

You must have Java installed on your machine.
For more information on the hardware requirements and supported platforms, click here.

Installation

  1. Download the SonarQube Community Edition.
  2. Unzip it to the local drive.
  3. Start the SonarQube Server and follow the below steps.
  4. Go to the installation directory and execute the StartSonar.bat file.
    Note: On other operating systems, execute ./sonar.sh
  5. By default, the SonarQube runs on the port 9000.
  6. Go to http://localhost:4502 with admin credentials listed below and you are all good to analyze your first project.
    Username =admin; Password=admin.Note: If your instance fails to start, check your logs to find the cause.

How to integrate your MAVEN project with SonarQube?

To analyze your project, you need to integrate your MAVEN project with SonarQube. Add the below plugin and profile to your project’s parent pom.xml.


<build>
<plugins>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
                <artifactId>sonar-maven-plugin</artifactId>
                <version>3.6.0.1398</version>
</plugin>
     </plugins>
</build>
 <profiles>
            <profile>
                         <id>sonar</id>
                         <activation>
                                     <property>
                                                 <name>sonar.login</name>
                                     </property>
                         </activation>
                         <properties>
                                     <sonar.host.url>
                                                http://localhost:9000
                                     </sonar.host.url>
                         </properties>
            </profile>
</profiles>

How to generate the SonarQube report?

Now that you’ve integrated SonarQube with your maven project, execute the below command to generate the SonarQube report.
“mvn clean install sonar:sonar”

Sample reports will look like this:SonarQube Report

How to exclude files from the SonarQube report?

To exclude files from SonarQube report, add sonar exclusion rules as shown below:
<properties>
<sonar.exclusions>
                        *.xml
            </sonar.exclusions>
            <sonar.test.exclusions>
                         src/test/java/**
            </sonar.test.exclusions>
<properties>

Notes:
1. sonar.exclusions
: It will exclude all the xml files in your project from SonarQube report.
2. sonar.test.exclusions: SonarQube will report issues like inappropriate coding standards etc., in your unit test classes. If you like to exclude your unit test cases from the SonarQube report, you can put them in sonar.test.exclusions.

Code coverage

If you want to measure the code coverage percentage, you can use the JaCoCo Maven plugin which is an actively developed line coverage tool. You can also generate reports. SonarQube will reuse and import these reports.

JaCoCo + SonarQube integration

Below are the steps to integrate JaCoCo with SonarQube –
  • Add JaCoCo configuration in project’s parent pom.xml

    <!– Jacoco – Code Coverage Plugin –>
    <plugin>
                <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.8.4</version>
        <executions>
                <execution>
                 <goals>
                <goal>prepare-agent</goal>
                </goals>
            </execution>
            <execution>
                <id>generate-code-coverage-report</id>
                <phase>test</phase>
                <goals>
                      <goal>report</goal>
                 </goals>
            </execution>
        </executions>
    </plugin>
  • Execute “mvn clean install sonar:sonar” command.
  • Check your SonarQube dashboard for the code coverage report.
    You will see a report as shown in the above screenshot.

How to exclude files from the code coverage reports?

To exclude files from the code coverage report, add sonar coverage exclusion rules as shown below:

<properties>
<sonar.coverage.exclusions>
                        **/**.js,
                         **/**.jsp,
                        src/main/java/com/myproject /vo/*.java
            </sonar.coverage.exclusions>
<properties>

Note: This will exclude all js, jsp and all java files under com.myproject.vo from code coverage report.

Source: https://aem.adobemarketingclub.com/sonarqubejacoco-integration/


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.