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).
If you are using the build tool Apache Maven, Maven can run your unit tests for you. In fact, you pretty much get that for free when using Maven.
It only cleans the build's files: The Maven Clean Plugin, as the name implies, attempts to clean the files and directories generated by Maven during its build. While there are plugins that generate additional files, the Clean Plugin assumes that these files are generated inside the target directory.
mvn package: Creates JAR or WAR file for the project to convert it into a distributable format. mvn install: Deploys the packaged JAR/ WAR file to the local repository. mvn deploy: Copies the packaged JAR/ WAR file to the remote repository after compiling, running tests and building the project.
On Maven, each time you want to compile, the best practice is to run mvn clean . It clears out the existing classes that you compiled from last compile. If you don't want to run 3 lines, just do "mvn test" after mvn clean . You don't have to always do mvn compile .
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.
As you said, Sonar is a post compile step. Sonar requires that the build be complete and pass all of it's tests before it can run, otherwise the things it does don't make any sense.
Sonar runs the tests with instrumenting (cobertura if I remember correctly), which gives the code coverage for the tests.
So, you need to do install (or at least compile & test) before Sonar runs, and then Sonar reruns the tests with instrumenting for it's own purposes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With