Search code examples
javaeclipsemavenm2eclipse

Why does Eclipse add M2_REPO to Java Build Pat > Libraries > Classpath?


Eclipse will automatically add M2_REPO/... to the Project > Properties > Java Build Path > Libraries > Classpath. Here is a picture showing this.

Classpath with M2_REPO/...

Instead, I would like to only see Maven Dependencies under Classpath. This makes it easy to set the External Annotations and other properties for all the dependencies instead of one by one. Here is a picture showing what I want.

Desired Classpath

I can correct the Classpath manually, but at some point later Eclipse will revert it. How do I make Eclipse maintain the smaller Classpath?


Solution

  • First of all, if there's any question, you should never use "mvn eclipse:eclipse". It is well-known that that is obsolete, and it will produce exactly the inconvenient structure you are trying to get away from.

    My general strategy with a project that m2e hasn't quite figured out is to delete the project from the workspace (don't delete the source), and then rename the ".classpath" file, adding ".bak" to it, then reimporting the project. That generally will result in a sane .classpath file.

    Unfortunately, I also find that there are some projects that m2e just cannot figure out, and the import just punts on creating the .classpath file correctly. I have tried to file an issue for that, but it's too hard to produce a project that both reproduces it and which I can publish in the issue, so I'd say the issue has never been addressed. In those rare cases, as all of our projects are structured similarly, that's when I just cut/paste the .classpath from a working project into the bad one, sometimes with some minor edits. Works every time.