1
votes

We have a multi module Maven project and using JaCoCo for code coverage analysis. I prepared the pom file to run the test cases and pick up the code coverage.

Below is my pom.xml with JaCoCo and other plugins added and in Azure Devops build am passing the command clean install sonar:sonar, tried clean test sonar:sonar in the Maven task with the argument -Dsonar.coverage.jacoco.xmlReportPaths=target/site/jacoco-ut defined in the pom.xml.

I see below details in the logs.

Maven logs:

Maven logs

Analysis completion logs:

Analysis completion logs

Test runs:

Test runs

Generating Jacoco report:

Generating Jacoco report

I have looked into various blogs and posts, tried many ways but couldn't see code coverage in Sonar.

    <plugins>
        <!--Jacoco Maven Plugin -->
        <plugin>
           <groupId>org.jacoco</groupId>
           <artifactId>jacoco-maven-plugin</artifactId>
           <version>0.8.7-SNAPSHOT</version>
           <executions>
        <!--
        Prepares the property pointing to the JaCoCo runtime agent which
        is passed as VM argument when Maven the Surefire plugin is executed.
        -->
            <execution>
                <id>pre-unit-test</id>
               <goals>
                    <goal>prepare-agent</goal>
              </goals>
              <configuration>
            <!-- Sets the path to the file which contains the execution data. -->
                <destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
            <!--
                Sets the name of the property containing the settings
                for JaCoCo runtime agent.
            -->
                <propertyName>surefireArgLine</propertyName>
            </configuration>
          </execution>
    <!--
        Ensures that the code coverage report for unit tests is created after
        unit tests have been run.
    -->
           <execution>
               <id>post-unit-test</id>
                <phase>test</phase>
                <goals>
                  <goal>report</goal>
                  </goals>
              <configuration>
            <!-- Sets the path to the file which contains the execution data. -->
                   <dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
            <!-- Sets the output directory for the code coverage report. -->
                   <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
              </configuration>
            </execution>
      </executions>
  </plugin>
         
         <!-- Maven ant plugin -->
        <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-antrun-plugin</artifactId>
           <version>3.0.0</version>
        </plugin>
        
        <!-- Maven Surefire Plugin -->
        <plugin>
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-surefire-plugin</artifactId>
             <version>3.0.0-M5</version>
             <configuration>
    <!-- Sets the VM argument line used when unit tests are run. -->
               <argLine>${surefireArgLine}</argLine>
    <!-- Skips unit tests if the value of skip.unit.tests property is true -->
               <skipTests>${skip.unit.tests}</skipTests>
    <!-- Excludes integration tests when unit tests are run. -->
                <excludes>
               <exclude>**/IT*.java</exclude>
                </excludes>
               </configuration>
        </plugin>
        
        <!-- Sonar Maven Plugin -->
        <plugin>
           <groupId>org.sonarsource.scanner.maven</groupId>
           <artifactId>sonar-maven-plugin</artifactId>
           <version>3.7.0.1746</version>
        </plugin>

Sharing my Maven Task YAML configuration

  • task: Maven@3

    inputs:

    mavenPomFile: 'pom.xml'

    goals: 'clean verify sonar:sonar'

    options: '-Dsonar.projectKey=** -Dsonar.organization=**

    -Dsonar.host.url=https://sonarcloud.io -Dsonar.login=**

    -Dsonar.java.binaries=$(build.sourcesdirectory)/**/src/test/java/

    -Dsonar.coverage.jacoco.xmlReportPaths=target/site/jacoco-aggregate/jacoco.xml

    -Dsonar.exclusions=/jaxb/**/*'

    publishJUnitResults: false

    codeCoverageToolOption: 'JaCoCo'

    codeCoverageClassFilesDirectories: 'core/target/classes,/core/target/test-classes'

    codeCoverageFailIfEmpty: true

    javaHomeOption: 'Path'

    jdkDirectory: '/opt/jdk-11.0.2'

    mavenVersionOption: 'Path'

    mavenDirectory: '/usr/local/apache-maven/apache-maven-3.6.3/'

    mavenSetM2Home: false

    mavenOptions: '-Xmx3072m'

    mavenAuthenticateFeed: false

    effectivePomSkip: false

    sonarQubeRunAnalysis: true

    isJacocoCoverageReportXML: true

    sqMavenPluginVersionChoice: 'pom'

After few changes i can see Jacoco report is picked up, but unable to see the Code Coverage

Sensor JaCoCo XML Report Importer [jacoco]

[INFO] Importing 1 report(s). Turn your logs in debug mode in order to see the exhaustive list.

Updating logs

main: [echo] Generating JaCoCo Reports

[report] Loading execution data file /home/AzureUser/adoagent/_work/2/s/CCReport43F6D5EF/jacoco.exec

[report] Writing bundle 'Jacoco report' with 338 classes

[INFO] Executed tasks

[INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS

[INFO] ------------------------------------------------------------------------ [INFO] Total time: 4.743 s

1
Please add logs as text, not as images next time.Gerold Broser
Noted..Will add logs as text, next timesaikumar reddy
Can anyone help me here ? Not sure what wrong am doing in my configuration.saikumar reddy
Hi @saikumarreddy, Please reference to the docs about SonarScanner for Azure DevOps to check the configurations in your pipeline. If the problem still exists, I recommend you open a topic on the SonarSource Community to ask for more help. In addition, please also check if you have disabled Automatic Analysis. Because there is conflict between CI-based and Automatic Analysis.Bright Ran-MSFT

1 Answers

1
votes

Please check if you have followed the following steps to configure your Azure Pipeline:

  1. Install the extension SonarCloud in your organization on Azure DevOps.
  2. Add a new SonarCloud Service Endpoint.
  3. Add "Prepare Analysis Configuration" task before the build step.
  4. Add "Run Code Analysis" task after the build step.
  5. Add "Publish Quality Gate Result" task.

enter image description here

If the problem still exists, for us to investigate this problem further, please share an example to show the detailed configuration of your Azure Pipeline. You can export the pipeline definition as a YAML file, and don't forget mask or hide your private information in the YAML file.

[UPDATE]

I noticed this description for the argument "Run SonarQube or SonarCloud analysis" (sqAnalysisEnabled) in the docs about the Maven task:

This option has changed from version 1 of the Maven task to use the SonarQube and SonarCloud marketplace extensions. Enable this option to run SonarQube or SonarCloud analysis after executing goals in the Goals field. The install or package goal should run first. You must also add a Prepare Analysis Configuration task from one of the extensions to the build pipeline before this Maven task. enter image description here

So, as I mentioned above, please make sure you have added the Prepare Analysis Configuration task before the Maven task in your pipeline.