Eclipse – Maven Eclipse plugin and classpath issues in Eclipse

classpatheclipsemaven-2plugins

I am using Eclipse 3.51, Maven 2.0.9, Java 1.4, with the Maven Eclipse plugin 2.7 with WTP 2.0 (not m2Eclipse).

I have a flat multi module project that is set up as follows (module Parent with the parent pom, module A and B is dependent on C).

Importing the four modules in for the first time will show compile errors as expected since I have not ran the eclipse plugin. With my local repository empty, running eclipse clean will resolve all compile errors and dependencies within the my local workspace.

If I were to make some minor code changes to module B and run the eclipse plugin again, compile errors would show up in module A and B. Compile errors about classes that cannot be found. Its like module C is no longer in the classpath for A and B to see.

I look at the .classpath file and its definitely looking at the right modules in the Eclipse workspace.

If I delete the maven repository and do the eclipse clean again, the compile errors about unresolved classes are fixed. Also, if I run the eclpse clean command with the useProjectReferences flag to false and then rerun it with true, Eclipse would rebuild my workspace and the errors will go away.

Whats going on?

Best Answer

Importing the four modules in for the first time will show compile errors as expected since I have not ran the eclipse plugin. With my local repository empty, running eclipse clean will resolve all compile errors and dependencies within the my local workspace.

So far so good. Just one question: by eclipse clean, you don't mean eclipse:clean but clean under Eclipse, right?

If I were to make some minor code changes to module B and run the eclipse plugin again, compile errors would show up in module A and B. Compile errors about classes that cannot be found. Its like module C is no longer in the classpath for A and B to see.

I'm not sure why you run the maven eclipse plugin after a minor code changes to module B but, let's say you have to e.g. because you added a dependency (it would have been helpful to mention the goal you run but never mind). When using the maven eclipse plugin with a multi projects build, it will use project references by default at the Eclipse level (i.e. A and B depend on the compiled classes of C, not on C JAR). And because Maven and Eclipse share the same target/classes folder, a clean or compile at the Maven level can confuse Eclipse. It doesn't see the compiled classes anymore and won't compile them by himself (because the change didn't occur in Eclipse). In that case, an Eclipse clean or rebuild (i.e. at the Eclipse level) should solve the problem.

I look at the .classpath file and its definitely looking at the right modules in the Eclipse workspace.

Yes, that's not the problem. The problem comes from the fact that both Eclipse and Maven share the same folder for compile classes.

If I delete the maven repository and do the eclipse clean again, the compile errors about unresolved classes are fixed.

You really don't have to do this and I don't get what the maven repository has to do with this if you use project references. Performing a clean and/or rebuilding all projects under Eclipse should fix the problem.

Also, if I run the eclpse clean command with the useProjectReferences flag to false and then rerun it with true, Eclipse would rebuild my workspace and the errors will go away.

Are you using project references or not? Again, next time, maybe provide your plugin configuration in your question, this may help. Anyway, you should not have to touch this between builds. Decide if you want to use project references (I think it's more convenient, without them you'll need to mvn install C to make changes available for A and B) and stick with this configuration. After a mvn build, a rebuild of the Eclipse workspace should be enough.

PS: Note that you could configure the maven eclipse plugin to use a custom buildOutputDirectory, for example target/eclipse-classes but this has other drawbacks (I wasn't doing this).