开发者

Wrong package name when using automatically added imports in Eclipse

开发者 https://www.devze.com 2023-01-31 15:41 出处:网络
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 fro

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:开发者_如何学JAVA

  • 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


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


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.


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..


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消