Search code examples
maventesting

Maven still compiling project despite already being compiled


I have a Maven project which I'm trying to use in some GHAs. I'm attempting to parallelize tests and before-hand I build the project thus allowing the tests to simply run without compiling as-well, at-least in theory.

The steps I follow are:

  1. run mvn -B test-compile
  2. run mvn -B -Dtest=${random-test-name} test

The problem despite containing the compiled target folder, the maven project still re-compiles when running the test. Is there a way to avoid this occuring?


Solution

  • You can simply run the goal surefire:test, instead of the phase test, that should only execute tests:

    mvn -B -Dtest=${random-test-name} surefire:test
    

    The output looks like:

    [INFO] Scanning for projects...
    [INFO]
    [INFO] -------------------< com.example:guide-spring-boot >--------------------
    [INFO] Building spring-boot-complete 0.1.0
    [INFO]   from pom.xml
    [INFO] --------------------------------[ jar ]---------------------------------
    [INFO]
    [INFO] --- surefire:3.0.0:test (default-cli) @ guide-spring-boot ---
    [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider
    [INFO]
    [INFO] -------------------------------------------------------
    [INFO]  T E S T S
    [INFO] -------------------------------------------------------
    [INFO] Running com.example.springboot.HelloControllerTest
    true
    [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.111 s - in com.example.springboot.HelloControllerTest
    [INFO]
    [INFO] Results:
    [INFO]
    [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
    [INFO]
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  2.450 s
    [INFO] Finished at: 2024-02-09T10:56:21+01:00
    [INFO] ------------------------------------------------------------------------