I've managed to do this by calling manual maven scripts from jenkins. So everything is working without Sonar plugin for jenkins. It's not perfect, but it works.
How to force Sonar to run Integration tests from Jenkins
-
23-07-2023 - |
Pregunta
I use following things:
maven - to build project
jenkins - for CI
sonar - for code analysis
My project contains unit and integration tests. I have configured above mentioned tools to work together.
Integration tests are separated from unit tests. To run integration test I need to run maven with specific profile. Sonar executes unit tests and gather metrics.
How can I force Sonar to execute specific profile and gather result from integration tests?
EDIT
My surefire and failsafe sections:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
<configuration>
<skipTests>${skip.unit.tests}</skipTests>
<excludes>
<exclude>**/*IT*.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.12</version>
<executions>
<execution>
<id>integration-tests</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<skipTests>${skip.integration.tests}</skipTests>
</configuration>
</execution>
</executions>
</plugin>
EDIT 2
When I run jenkins build with clean packag -DskipTests
because otherwise my tests will be executed twice.
First time during jenkins build and second time when sonar executes them. The problem is that when I start jenkins build with clean verify
goal. I can see that both unit and integration tests are executed. And after this step Sonar starts his job and only unit tests are executed.
EDIT 3
I have managed to work sonar with maven from console. I've added following profile and proper plugin to my pom.xml
:
<profile>
<id>sonar</id>
<!-- Enable coverage computation via JaCoCo for Sonar's needs -->
<build>
<plugins>
<!--
Compute integration test coverage for Sonar
Note: REQUIRES MAVEN 3 - throws InstantiationException: java.util.List otherwise
Beware: Sonar doesn't run the verify phase; you should always 'mvn clean install' before running sonar
-->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<propertyName>jacoco.agent.argLine</propertyName> <!-- default: argLine -->
<includes>
<include>pl/cyfronet/**</include>
</includes>
<destFile>${project.build.directory}/jacoco-integration.exec</destFile> <!-- agent -->
<dataFile>${project.build.directory}/jacoco-integration.exec</dataFile> <!-- report -->
</configuration>
<executions>
<execution>
<id>agent</id>
<goals><goal>prepare-agent</goal></goals>
</execution>
<execution>
<!--
Generate coverage report html in target/site/jacoco/ from target/jacoco.exec
Exec.: mvn verify site
-->
<id>report</id>
<phase>site</phase>
<goals><goal>report</goal></goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
I've also added settings.xml
file to my ~/.m2/
directory.
<settings>
<profiles>
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<!-- Example for MySQL-->
<sonar.jdbc.url>
jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
</sonar.jdbc.url>
<sonar.jdbc.username>sonar</sonar.jdbc.username>
<sonar.jdbc.password>sonar</sonar.jdbc.password>
</properties>
</profile>
</profiles>
</settings>
After that I run mvn with following parameters:
mvn sonar:sonar -Dsonar.jacoco.itReportPath=target/jacoco-integration.exec
. Reports were correctly generated to sonar.
I was trying to use it in my jenkins. I have replaced post-build sonar with maven goal sonar:sonar -Dsonar.jacoco.itReportPath=target/jacoco-integration.exec
but it doesn't work. My integration tests are not invoked. Any ideas?
Solución