Search code examples
mavenlog4jclasspathsurefire

What is Maven doing with my testclasspath


I'm having issues with a test, which when executed in maven fails to initialize log4j, although a valid log4j.properties is in src/test/resources and therefore should end up on the classpath of the test. But it doesn't, i.e. log4j prints only

log4j:WARN No appenders could be found for logger (org.springframework.test.context.junit4.SpringJUnit4ClassRunner).
log4j:WARN Please initialize the log4j system properly.

In order to debug the problem I printed the classpath from the test itself, using the code here

But instead of a lengthy list of jars and paths I just get

/<projectpath>/target/surefire/surefirebooter6226797341642271676.jar

So my questions are:

  1. WTF is maven doing with the classpath?

  2. Why doesn't my log4j.properties end up on the classpath?

  3. How do I debug this?

Note: In Eclipse I can run the test just fine and everything works as expected.

Another note: the maven project is a multimodule project and I'm only executing a single test from a single submodule, with a commandline like this:

mvn -U -Dtest=de.company.project.SomeTest clean test

Solution

  • Have a good look at the maven-surefire-plugin. By default it creates a jar stuffed with your entire classpath. This is controlled by the useManifestOnlyJar option. This works around the problem of Windows having a classpath limit of 1024 (quoting off the top of my head). Under Linux you wouldn't really feel this pain much as the limit is much higher.

    If you are forking the maven-surefire-plugin, it will use a different classpath than the one you're running Maven (and the compilation).

    Debugging this kind of crappy situation can be done as follows:

    • In one of your tests add a loop that lists all the environment variables along with the java system properties.

    • Debug the tests:

      mvn -Dmaven.surefire.debug="-Xdebug \
          -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=9001 \
          -Xnoagent" \
          test