Search code examples
javamavenunit-testing

Is there a maven plugin to analyze the project dependencies before running unit tests?


I have a maven-built java project where the test phase takes approximately 1 hour.

However, after moving to mvnd (https://github.com/apache/maven-mvnd), tests now take ~32 minutes. (Further, I also tried graalVM with a native Profile in POM. However, no noticeable difference is seen.)

It is a multi-module project and I have a feeling the test phase can still run within a fraction of 32 minutes If there was a mechanism to analyse the tests needed to be run based on something like the below:

  1. List the changed source files in the current build and the modules containing them. (I understand maven builds entire project and no notion of 'changed' files which is best left to git/version control)
  2. List the projects that use these modules i.e. those having this module as a dependency)
  3. Compile the entire project; however, run the tests only on the set of modules in [1] and [2]

Questions:

  1. Is this assumption correct?
  2. If so, is there already a plugin that can help with such optimization?

Solution

  • So, first of all, one should try to make the local builds faster, i.e.

    • check if some tests are overly complicated
    • move integration tests from the test phase to the integration-test phase
    • Use parallel builds

    When the long running tests are only part of integration-test, you can avoid running them locally, you just run them on the build server.

    On the build server, you can try to construct a pattern as you described. How you exactly do this depends of course on the build server you use.

    The general approach could be:

    1. Find out which modules changed by looking at changed directories in git. This could e.g. be done via a shell script and maybe some perl.
    2. Run something mvn clean deploy -pl module1,module2 -amd. This will automatically figure out the dependant modules.