Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

like image 499
Robber Avatar asked Sep 20 '11 14:09

Robber


People also ask

Does Maven install run unit tests?

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.

What happens after mvn clean install?

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.

What is the use of mvn clean install and mvn clean package?

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.

What to do after mvn clean?

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 .


2 Answers

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.

like image 147
pushy Avatar answered Oct 19 '22 00:10

pushy


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.

like image 2
Matthew Farwell Avatar answered Oct 19 '22 02:10

Matthew Farwell