Java – How to configure eclipse classpath used for junit tests

eclipsejavajunit

I have an eclipse project where every source folder has its own associated output folder. Instead of /classes it's called /eclipse-classes.

So if I have a folder:
src/main/java (typical maven thing)
the target folder is:
target/eclipse-classes

And likewise for resources etc.

This seems to work (i.e. eclipse generates .class files that are put inside these folders) but running any Junit tests throws an exception stating "class not found". I'm running JUnit using the built-in eclipse test runner (i.e. right click the class, "run as", "Junit test").

Copying the /eclipse-classes folder to /classes makes them succeed, meaning eclipse is using /classes, but I can't find any configuration options to change it. Is there any way to find out where and why eclipse is still using the /classes folder?

(perhaps relevant, I'm also using the m2eclipse plugin)

Some additional information inspired by Rich Seller's answer:
Maven is configured to run the following on resource changes:

process-resources resources:testResources

While this won't do anything useful (copies to the wrong directory) the resources are not problematic atm since they end up in the correct location.

The .classpath entries look okay. There's a bunch of maven subprojects so the nesting goes a bit deeper than what Rich posted, but otherwise it's exactly the same except for this line:

<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>

I think we might not need that one but it's not hurting anything atm.

edit2: Further testing reveals that eclipse is generating class files in both the /eclipse-classes folders and the /classes folder. It seems that m2eclipse is running mvn build in the background when building automatically, but I can't seem to find a way to disable this. I'll try to get in touch with the m2eclipse developers if nobody here has any other ideas.

Best Answer

If you use m2eclipse, then the config in the Eclipse project is overwritten by the plugin. See this article for a solution.

The reason for this is that some maven plugins can't cope with a directory that is outside of target/, so the m2eclipse devs force the folders for compiled classes to be target/classes and target/test-classes, no matter what you configure in Eclipse.

By using a profile, you can use different folders for Eclipse. Still, it's not wise to change the output folders for maven (and its plugins).