user3552178
user3552178

Reputation: 2971

How to avoid running "test" again when running "install"

In our maven project we have a couple of targets: clean, test, and install.

If I run mvn clean install, it runs test which is a prerequisite for install. If I then run mvn clean install again without any code changes, it runs test again.

How can I make it smart enough to avoid unnecessarily running test the 2nd time?

Upvotes: 0

Views: 180

Answers (4)

Mohd Waseem
Mohd Waseem

Reputation: 1374

You can tell maven to include/exclude test via arguments:

# Exclude one test class, by using the explanation mark (!)
mvn clean install -Dtest=!LegacyTest
# Exclude one test method
mvn clean install -Dtest=!LegacyTest#testFoo
# Exclude two test methods
mvn clean install -Dtest=!LegacyTest#testFoo+testBar
# Exclude a package with a wildcard (*)
mvn clean install -Dtest=!com.mycompany.app.Legacy*

To tell maven to include specific test:

# Include one file
mvn clean install -Dtest=AppTest
# Include one method
mvn clean install -Dtest=AppTest#testFoo
# Include two methods with the plus sign (+)
mvn clean install -Dtest=AppTest#testFoo+testBar
# Include multiple items comma separated and with a wildcard (*)
mvn clean install -Dtest=AppTest,Web*
# Include by Package with a wildcard (*)
mvn clean install -Dtest=com.mycompany.*.*Test

Note: We might need to escape "!" while using bash.

mvn clean install -Dtest=\!LegacyTest

To escape, we will have to use back slash(\)

Upvotes: 1

Federico Piazza
Federico Piazza

Reputation: 30995

You can leverage two useful properties to manipulate test cases

# Skip test cases compilation
mvn -Dmaven.test.skip install

# Compile test cases but not execute them
mvn -DskipTests install

However, if you want maven to only run test that have changed then you cannot do that. This is one of the very handy things that Gradle provides over Maven.

You can take a look at this site: https://dzone.com/articles/reducing-test-times-only where the guy created a poc, although it is a workaround (not provided by maven), so I think you want Gradle smart things for your case.

Upvotes: 2

Michael
Michael

Reputation: 44150

Every Maven phase runs every proceeding Maven phase of the lifecycle except clean. i.e. test runs validate, compile, and test.

So since install already runs every proceeding phase, including test, don't bother calling mvn test explicitly. If a test fails, it won't proceed any further.

tl;dr: what you want is

mvn clean install

Upvotes: 1

Jagadesh
Jagadesh

Reputation: 2116

This skips tests

mvn clean install -Dmaven.test.skip=true

Upvotes: 1

Related Questions