So you think you can skip tests? The "dark side" of Maven

#maven May 31, 2016 2 min Mike Kowalski

Tests are the light side of programming. Period. Running test cases is one of the fundamental duty of every build automation system - including Maven. Even if it’s a kind of ritual and the determinant of style, every experienced programmer knows that sometimes things just have to be MacGavyered instantly. But how to fold up a late night app version using only Maven, bottle of ketchup and an old shoelace if our tests just don’t pass?

#1 I’ll fix that later (-DskipTests)

This one is quite straightforward - running: mvn clean install -DskipTests skips tests execution:

[INFO] --- maven-compiler-plugin:3.5.1:testCompile (default-testCompile) @ skip-test-playground ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /home/user/workspace/skip-test-playground/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ skip-test-playground ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-jar-plugin:2.2:jar (default-jar) @ skip-test-playground ---
[INFO] Building jar: /home/user/workspace/skip-test-playground/target/skip-test-playground-1.0-SNAPSHOT.jar

Problem solved? Now it’s time for a quick release…

#2 Just release it, please…

When using Maven Release Plugin constructs like mvn release:perform -DskipTests don’t work at all. To understand why, let’s look at the docs first:

release:perform will fork a new Maven instance to build the checked-out project. This new Maven instance will use the same system configuration and Maven profiles used by the one running the release:perform goal.

The problem is that -DskipTests is neither a system configuration nor a profile - it’s a command line property. Release plugin creates a forked process for each of our projects and the property won’t be propagated automatically.

The solution is to use -Darguments="-DskipTests". This property defines additional arguments that will be passed to each forked Maven process. So this mean “skip tests for every project you are trying to release”.

#3 No time to explain - don’t even compile!

The last one is probably the most reckless one. In each of the cases before we don’t want to run the tests, but they will be compiled anyway:

[INFO] Compiling 1 source file to /home/user/workspace/skip-test-playground/target/test-classes

But what if our tests don’t even compile? In 99.9999% of cases this should be a sufficent warning - something gone seriously wrong. If you are a fan of the remaining 0.0001% or you have nothing left to loose you can use the mysterious property called maven.test.skip. How does it work? Running mvn clean install -Dmaven.test.skip=true just won’t compile test sources.

[INFO] --- maven-compiler-plugin:3.5.1:testCompile (default-testCompile) @ skip-test-playground ---
[INFO] Not compiling test sources
[INFO] 
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ skip-test-playground ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-jar-plugin:2.2:jar (default-jar) @ skip-test-playground ---
[INFO] Building jar: /home/user/workspace/skip-test-playground/target/skip-test-playground-1.0-SNAPSHOT.jar
Conclusions

There are many real-life scenarios for skipping the tests and not all of them are a pure evil :-) However, consider taking a day off before even thinking of maven.test.skip=true.

Mike Kowalski

Software engineer believing in craftsmanship and the power of fresh espresso. Writing in & about Java, distributed systems, and beyond. Mikes his own opinions and bytes.