26
votes

I have several Maven projects converted into one Eclipse workspace. When I edit the code under Eclipse, I sometimes use CTRL+SHIFT+M combination of keys to automatically add imports to the classes from a project in my workspace. But somehow they are added like this:

import src.main.java.com.mycompany;

while to real package I wanted to import is com.mycompany. This has to be some configuration in Eclipse to fix this but I'm confused. However, this issue is very annoying.

EDIT:

I've forgotten to mention that Eclipse files were generated using mvn eclipse:eclipse command.

Under Eclipse project seems to be configured properly. Its source folders set like this:

  • src/test/java
  • src/test/resources
  • src/main/java
  • src/main/resources

And everything under Eclipse works properly except the situation when I press CTRL+SHIFT+M keys

6
As a side note, there is an Eclipse plugin for handling Maven projects named m2eclipse. - Powerlord

6 Answers

47
votes

The standard source folder for Java projects is

./src

For imported maven projects, simply remove this folder from the list of source folders at the build path settigs. The correct source folder is

./src/main/java
19
votes

In eclipse;

Remove the existing source folders first. -right click -> menu -> build path -> remove from build path

then

Right click on the source folder. build path -> use as source folder.

Seems like your settings are pointing to the parent of the source folder so src is recognized as package by eclipse.

4
votes

It's because eclipse is not aware of the convention over configuration filestructure Maven is following. Install the M2Eclipse plugin and File > New > Other > Maven Project for new projects or for existing ones right click on your imported project on Package Explorer > Maven > Enable Dependency Management. Once successfully done, on the Package Explorer you would see your project nicely gathered following the Maven conventonal filestructure like src/main/java, src/main/resources, src/test/java and from then on you'll start seeing your package structure hierarchy like com.mysite.myproject..

3
votes

First remove it. Then add it back using right click on package->build path->configure build path-> Source->Add Folder and add the entire /src/main/java tree

0
votes

You've got the wrong source folders in your build path, and it's a wonder that anything works at all.

You can use either the maven-eclipse-plugin or M2Eclipse to automate getting this right, or you can manually fix the build path to call our your actual source folders, not their great-grandparents.

0
votes

Non of the above worked for me. Finally I just changed the name of the scr folder to scr-java and this removed the package structure with scr as root package.