4

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

  • Please add logs as text, not as images next time. – Gerold Broser Jan 02 '21 at 20:44
  • Noted..Will add logs as text, next time – saikumar reddy Jan 03 '21 at 05:38
  • Can anyone help me here ? Not sure what wrong am doing in my configuration. – saikumar reddy Jan 04 '21 at 04:40
  • Hi @saikumarreddy, Please reference to the docs about [SonarScanner for Azure DevOps](https://sonarcloud.io/documentation/analysis/scan/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](https://community.sonarsource.com/) 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 Jan 14 '21 at 09:19

1 Answers1

1

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.

Bright Ran-MSFT
  • 5,190
  • 1
  • 5
  • 12
  • Hi, Am trying to run from the Maven task. where can i share my yaml configuration in Maven task – saikumar reddy Jan 04 '21 at 08:26
  • Shared my Maven task in yaml – saikumar reddy Jan 04 '21 at 08:50
  • Hi @saikumarreddy, Thanks for your reply. I have updated my answer with more suggestions. Please check it. Any questions or updates about this, feel free to tell me. – Bright Ran-MSFT Jan 05 '21 at 06:05
  • Hi @Bright Ran-MSFT, I have added prepare analysis configuration before the Maven task and selected integrate with Maven or gradle option in the Prepare analysis task. but still I don't see any code coverage in Sonar Cloud – saikumar reddy Jan 05 '21 at 06:08
  • Hi @saikumarreddy, Please try using the extension provides tasks "**Prepare Analysis Configuration**", "**Run Code Analysis**" and "**Publish Quality Gate Result**" in your pipeline to see if they can work. In addition, I find this issue ([microsoft/azure-pipelines-tasks#12499](https://github.com/microsoft/azure-pipelines-tasks/issues/12499)) for the similar problem. – Bright Ran-MSFT Jan 05 '21 at 06:31
  • Thanks but I have tried with these options using Run analysis and Publish Quality. But no luck – saikumar reddy Jan 05 '21 at 06:33
  • Updated with the Build logs Generating Jacoco report with 338 classes, even after this i don't see any increase in the code coverage – saikumar reddy Jan 05 '21 at 09:00