Skip to content

Instantly share code, notes, and snippets.

@danilobatistaqueiroz
Last active May 7, 2020 22:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danilobatistaqueiroz/efe4400a9e095239bc1a2c71dcab5ef6 to your computer and use it in GitHub Desktop.
Save danilobatistaqueiroz/efe4400a9e095239bc1a2c71dcab5ef6 to your computer and use it in GitHub Desktop.
sonar jacoco

Configuring a Maven project to run test coverage with SonarQube, Jacoco

Install the SonarQube server, go the download page, choose the community edition:
https://www.sonarqube.org/downloads/

sonarqube_download_page

Add Jacoco plugin in POM.xml:

<build>
    <finalName>maven-code-coverage</finalName>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.sonarsource.scanner.maven</groupId>
                <artifactId>sonar-maven-plugin</artifactId>
                <version>3.4.0.905</version>
            </plugin>
        </plugins>
    </pluginManagement>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M1</version>
        </plugin>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.2</version>
            <executions>
                <execution>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>report</id>
                    <phase>test</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

You can add executions, for example, a rule for minimun of coverage:

<execution>
    <id>jacoco-check</id>
    <goals>
        <goal>check</goal>
    </goals>
    <configuration>
        <rules>
            <rule>
                <element>PACKAGE</element>
                <limits>
                    <limit>
                        <counter>LINE</counter>
                        <value>COVEREDRATIO</value>
                        <minimum>0.9</minimum>
                    </limit>
                </limits>
            </rule>
        </rules>
    </configuration>
</execution>

Start Sonarqube server:
~/sonarqube/bin/linux-x64/sonar.sh console

Generate and get a token:
Go to Administrator Profile, Security, Generate Tokens

Download a sample project to use as an example:
https://github.com/danilobatistaqueiroz/sonar_jacoco_coverage

Run the Maven's project tests:
mvn clean install
mvn test

Here you'll see the Jacoco reports:
jacoco_reports

To integrate with Sonar, you need to call:
mvn sonar:sonar -Dsonar.host.url=http://localhost:9000 -Dsonar.login=your-generated-token

Navigate to Sonar web interface:
http://localhost:9000

Enter in the menu Projects:

sonar_projects_coverage

Running a SonarQube inside a container Docker configured with Postgres:
docker-compose.yml

version: "3.0"
services:
  sonarqube:
    image: sonarqube
    ports:
      - "9000:9000"
      - "9092:9092"
    networks:
      - sonarnet
    environment:
      - SONARQUBE_JDBC_URL=jdbc:postgresql://db:5432/sonar
    links:
      - db
    volumes:
      #- $(pwd)/extension:/opt/sonarqube/extensions/plugins
      - sonarqube_conf:/opt/sonarqube/conf
      - sonarqube_data:/opt/sonarqube/data
      - sonarqube_extensions:/opt/sonarqube/extensions

  db:
    image: postgres
    networks:
      - sonarnet
    environment:
      - POSTGRES_USER=sonar
      - POSTGRES_PASSWORD=sonar
    volumes:
      - postgresql:/var/lib/postgresql
      - postgresql_data:/var/lib/postgresql/data
networks:
  sonarnet:
    driver: bridge

volumes:
  sonarqube_conf:
  sonarqube_data: 
  sonarqube_extensions:
  sonarqube_bundled-plugins:
  postgresql:
  postgresql_data:

Now, you need to run docker-compse:
docker-compose up

SonarSource/docker-sonarqube#279

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment