Search code examples
unit-testingmavenjenkinssonarqubesurefire

How to only run unit tests once when running "maven clean install" and Sonar?


I had the following configuration for my Jenkins job: First clean and build the maven project, then run the unit tests and static analysis: clean install sonar:sonar The problem was that install and sonar:sonar each ran the unit tests which effectively doubled the build time.

I fixed this by changing clean install sonar:sonar to clean install -DskipTests and running Sonar using the Jenkins sonar plugin. Now the unit tests ran only once and sonar showed the results, but Jenkins didn't know about the tests anymore.

My guess is that Jenkins is only looking at the surefire-reports folder after building, not after Sonar (which is a post-build action).


Solution

  • You could just run clean install -DskipTests first and then add a second maven build step sonar:sonar. The tests (and complete sonar analysis) will be run during the build phase, and the surefire results can be collected by jenkins afterwards.