4

In Maven, is there some way to specify that JUnit tests only run when there are some source code changes?

For instance, I see this message

[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ my_project --- [INFO] Nothing to compile - all classes are up to date 

Now -since no code was compiled (since there were no changes from the last compile), I'd prefer that no tests run. Is this possible?

5
  • Can I ask if no code is changed, why are you running the compile goal? Commented Jun 11, 2012 at 17:59
  • There are 2 things going on here. 1 - I have a project with multiple profiles. Each profile has different config files that will be packaged into the war, but none of these config files are used in the tests (the tests have their own set of 'mock' config files). for this reason, once the tests pass for 1 profile build, they will pass for all. There's really no need to run the tests for each profile that is built. 2 - I'm trying to set up a multimodule build. Each time the build runs for a change in any module - it's running all the tests (even the modules w/ 0 changes). Commented Jun 11, 2012 at 18:45
  • Ah I see. I guess it could be argued that if the tests shouldn't be run after a config check-in, then the config and code shouldn't be part of the same multi-module build. I realise that won't help you! Maybe split your build into 3. BuildA for code, that is triggered by source code changes, and always runs tests. BuildB for config only. And BuildC for the webapp that is triggered by builds of either A or B. BuildC only uses the built artifacts from A and B, so wouldn't trigger compilation/tests. Commented Jun 11, 2012 at 21:59
  • FYI, you may need to override grepcode.com/file/repo1.maven.org/maven2/…, if the standard plugin doesn't do what you want. Commented Jun 11, 2012 at 22:04
  • Your project setup as way to much in one module. Break out the tests into different modules and potentially even factor out the different profiles into separate modules and all will work just fine. Commented Jun 11, 2012 at 22:09

1 Answer 1

1

Your project setup should be broken up into separate modules. However if you can not do that you could set the maven.test.skip property in your profiles in which you want no tests to be run. See more about skipping tests on the site.

You are talking about a war project. Typically a war project should NOT have any code in it, but rather just some configuration and stuff like html, jsp, css and java script and bundle jar projects, that contain java code. The jar projects will have the code. And if you do integration tests a good practice is to have them in separate modules.

Different war configs can be in profiles or potentially also in separate modules using the war overlay feature of the maven war plugin.

Sign up to request clarification or add additional context in comments.

2 Comments

Why would I want to break the unit tests into a different module than the code being tested? That doesn't seem like a good idea. I also don't see how this would automatically run the tests when code changes, but not run them when it doesn't.
Edited the answer to clarify.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.