How can I import Jacoco test report using sonarscaner

** I am using sonarqube 7.9.3-community version.
** For scanner, I am using docker sonar-scanner-cli , running in GitLab CI job to scan my code for bugs.
** Now I want to include unit testing as well. For this what I have understood is:
Sonarqube does not generate unit test reports. Instead it relies on test report generated by tools such as Jacoco.
** I have stumbled upon beautifully written guide . Which includes scanner for Gradle and Maven but I am unable to find a way to achieve it in gitlab ci job.
which approach should I follow in order to implement the same guide with docker sonar-scanner-cli ?

A quick update:
I have added the profiles from the guide in the pom.xml file and the profiles section looks like this:

<profiles>
		<profile>
		<id>coverage</id>
		<activation>
			<activeByDefault>true</activeByDefault>
		</activation>
		<build>
			<plugins>
			<plugin>
				<groupId>org.jacoco</groupId>
				<artifactId>jacoco-maven-plugin</artifactId>
				<executions>
				<execution>
					<id>prepare-agent</id>
					<goals>
					<goal>prepare-agent</goal>
					</goals>
				</execution>
				<execution>
					<id>report</id>
					<goals>
					<goal>report</goal>
					</goals>
				</execution>
				</executions>
			</plugin>
			</plugins>
		</build>
		</profile>
</profiles>
</project>

I am running sonar:sonar in my script section of gitlab-ci.yml file and my gitlab-ci.yml file looks like this:


image: maven:3-jdk-8

stages:
  - build
  - static-test

maven-build:
  stage: build
  script: 
      - mvn package
      - mvn sonar:sonar
  
  artifacts:
    paths:
      - target/surefire-reports/TEST-*.xml
      - target/site/jacoco/jacoco.xml
    reports:
      junit:
        - target/surefire-reports/TEST-*.xml
        - target/site/jacoco/jacoco.xml


#===============Code Quality /Static Testing==========

sonarqube-pretest:
    stage: static-test
    #tags: [old-runner]
    image: sonarsource/sonar-scanner-cli:latest
    variables:
        SONAR_TOKEN: "0239abe136224e84ea14d48201f0cb7bcd09edae"
        SONAR_HOST_URL: "http://192.168.2.30:9000"
        SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar" 
        GIT_DEPTH: 1
    script:
        - pwd
    cache:
        key: ${CI_JOB_NAME}
        paths:
        - .sonar/cache
sonarqube-test:
    stage: static-test
    #tags: [old-runner]
    image: sonarsource/sonar-scanner-cli:latest
    variables:
        SONAR_TOKEN: "0239abe136224e84ea14d48201f0cb7bcd09edae"
        SONAR_HOST_URL: "http://192.168.2.30:9000"
        SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar" 
        GIT_DEPTH: 1

    script:
        - sonar-scanner -Dsonar.qualitygate.wait=true
    allow_failure: true

Is this the right approach to follow?

Hi Ahsan,

First, The pom.xml section that you provide above is not compatible with your command mvn package to generate the jacoco.xml report. By default the jacoco:report goal is bonded to the verify phase (see jacoco:report)
So you should replace mvn package by mvn verify (or bind the jacoco:report goal to the package phase in the pom.xml)

Secondly, I don’t understand why you need to run sonar-scanner, for a maven project you only need to run the analysis using mvn sonar:sonar.

There’s an example in the SonarScanner for Maven: section of the GitLab CI/CD SonarQube documentation. In summary, you can only run one maven command to do all the duty:

mvn verify sonar:sonar -Dsonar.qualitygate.wait=true