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.
Open eclipse → right click on project and click on property > Build Path > Configure Build Path and add the junit-4.10. jar in the libraries using the button Add External Jar. We assume that your Eclipse has inbuilt JUnit plugin.
To use JUnit you must create a separate . java file in your project that will test one of your existing classes. In the Package Explorer area on the left side of the Eclipse window, right-click the class you want to test and click New → JUnit Test Case. A dialog box will pop up to help you create your test case.
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).
The Eclipse JUnit integration has no special classpath configuration, it will work off the output folders defined in your classpath and should find all classes compiled to those folders. It may be that there is something dodgy in your .classpath file, so that JUnit is confused (by default the .classpath file is hidden from view, it is located in the root of the project).
Based on your description I would expect to see entries something like below (note the default output folder and the override for src/main/java and src/main/resources). Does your classpath look markedly different, if so that may be the issue
<classpathentry excluding="**" kind="src" output="target/eclipse-classes" path="src/main/resources"/>
<classpathentry kind="src" output="target/eclipse-classes" path="src/main/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
<classpathentry kind="output" path="target/classes"/>
This is a long shot, but it may also be that a Maven clean is configured on your project, if that is the case the contents of target/eclipse-classes would be dropped whenever the clean goal is run, so your tests would be deleted from the file system before the tests are run. You can see what goals are run by Maven by opening the project properties (alt-enter) and selecting the Maven item.
This part doesn't directly answer your question, but you may find useful anyway. I tend to have my Eclipse output directories the same as for Maven and have no issues inside Eclipse (I modify the Maven builder to only run process-resources so it doesn't attempt to compile).
If I do a Maven build the Maven compiler will build any changed classes (that would be all of them if a clean is included). A subsequent modification in Eclipse is detected by the incremental compiler and processed, all is fine. I do turn off Build Automatically, but that's just because it annoys me, it may be that Maven and Eclipse fght if you have them both turned off.
Just in case you're open to trying a different plugin for this: I use the maven-eclipse-plugin to generate my Eclipse project settings. I configure the plugin to configure my Eclipse project to use a completely separate output directory for classes (see below). It's relative to the project root, so it sits outside of target.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.6</version>
<configuration>
<outputDirectory>eclipse_build</outputDirectory>
</configuration>
</plugin>
This works great for me, including being able to run tests right out of the box, both via Maven and Eclipse.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With