Improved pipeline build time with Maven, Jenkins and Surefire

Currently, I am dealing with a bug that leads to unstable build times. The setup consists of Jenkins, Maven and the Surefire Test plugin. Through this work, I’ve come across the fact that all the optimizations we use on our developer machines never found their way into our Jenkins pipeline.

So then I started tuning our pipeline and the Maven process. All in all, I was able to reduce the build time from 29 minutes to 19 minutes. That took three adjustments.

Multi threaded Maven build

Using Maven you can try to build your code with more than 1 thread.
mvn -T1C clean install [...]
1C meaning „1 * CPU core count“

Multi threaded tests using Surefire

The surefire plugin can be configured to use multiple threads on e.g. class or method level.

I did run some tests with forking and the different parallel configuration. The only one which did make a difference positively was to use „class“ level threading. While using forking or method level parallelization, the build time did dramatically increase.
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <parallel>classes</parallel> <!-- Run tests in parallel threads inside a single process -->
    </configuration>
</plugin>

Parallel steps in Jenkins

You can run steps in parallel on different Jenkins nodes. I did parallelize the deployment and Sonar Qube analyses phase in our build process. This only makes sense when you have enough build nodes available.

To turn on parallelization you can just wrap your steps in an additional parallel block stage:
stage("Deploy and Check") {
    parallel {
        stage('When nightly (develop or custom team branch), deploy') {
           [...]
        }
        stage('When nightly (develop or custom team branch), run sonar') {
           [...]
        }
    }
}