When running a Maven build on a CI server like Jenkins, the first approach is to use a command similar to
mvn -Dmaven.test.failure.ignore=true clean deploy
However, when faced with big projects consisting of many modules, this has some drawbacks:
- It can take a pretty long time until you get the feedback that you have broken the build. If e.g. there is a compilation problem in the last module, every unit test and integration test of every other module will be executed until this fatal error is reported.
- If the build on a module fails, all previous modules have already been deployed to the Maven repository, which leaves the repository in a potential unstable state (the latest SNAPSHOT versions may not be compatible to each other)
To work around these problems, I am using four Maven invocations instead of only one:
mvn --fail-at-end -Dmaven.test.skip=true clean install
mvn --fail-at-end test
mvn --fail-at-end failsafe:integration-test failsafe:verify
mvn -Dmaven.test.skip=true -Dmaven.install.skip=true deploy
This results in a very quick compilation run first, then a quick execution of the unit tests (the source files won’t be compiled again because they haven’t changed since the first run), followed by a not-so-quick execution of the integration tests (which have already been compiled in the previous step), and finally the deployment of all artifacts.
But this also has some downsides:
- I lose Jenkins’ “build unstable” feature, since a failed test now also fails the whole build and therefore turns the job red instead of yellow.
- There is a fast feedback about failed unit test, but feedback about integration tests is only available if all unit tests have passed.
- The third command is a dirty hack that will work only if the integration test don’t need the “pre-integration-test” and “post-integration-test” phases (I wanted to come up with a solution that needs no modification of the pom.xml files).
I will deal with these issues in my next post.
(Note that the “maven.install.skip” option is only available for the maven-install-plugin version 2.4 and above)
A final note about parallel builds: I wish that there was a way to tell Maven to recklessly parallelize the build without caring for module dependencies – because this would greatly reduce the execution time of the second, third, and forth build step…