How do I get my Eclipse-Maven project to automatic

2019-01-09 10:30发布

I’m using Eclipse Mars with Maven (v 3.3). When I update a dependency in my pom (change the version), my Eclipse-Maven project doesn’t pick it up, even when I right click my project, and select “Maven” -> “Update Project.” I know this because I do not see compilation errors in the Eclipse Java editor that I see when I build the project on the command line using

mvn clean install

When I remove the project from the workspace and re-import it, then things get back to normal. However this is a cumbersome process. How do I get my Maven-Eclipse project to automatically detect changes in my pom and update the project libraries appropriately?

And yes, in the “Project” menu, “Build Automatically” is checked.

5条回答
Deceive 欺骗
2楼-- · 2019-01-09 10:46

Three Mandatory checks you should do for automatic update in your classpath

  1. Your Repository is not in-sync with your Eclipse IDE, Please check the below settings in your IDE. External Maven
  2. Right Click your any POM.xml from your IDE and check for the Maven profile which should be auto-activated. Also offline and Force update check box shouldn't be enabled. Please refer the below image.autoactivated
  3. Always check for your user settings which should reflect your local maven settings.xml, as shown in the below figure. enter image description here
  4. After performing all these checks, refresh your Eclipse Work-space to get these changes reflected.
查看更多
你好瞎i
3楼-- · 2019-01-09 10:49

Eclipse should be updating your classpath. If it's not, that implies something is going wrong.

It's hard to say what the problem could be exactly without knowing more about your project's pom.xml. More information might be necessary to solve the issue, but I'll just make a stab in the dark:

Open the .project file in your project's root folder and check the ordering of builders and natures there. It might be possible that some other nature on the project is also causing maven2Nature to fail. Move maven nature up and see if that helps any.

Alternatively you might be thinking that Eclipse does not update your dependencies because it does not add some some error indicators in the project that should be there with new dependencies. If that's the case try cleaning the current project (project>clean...). Maven in Eclipse does not necessarily trigger a full rebuild when dependencies are updated.

If none of this works, closing/opening the project might solve the issue quicker than re-importing.

查看更多
我命由我不由天
4楼-- · 2019-01-09 10:51

What you wrote, should work. Did you check this:

  • does "pure" mvn install from terminal see your changes in POM?

  • maybe some Maybe plugin is buggy, cached some dependencies in target, and mvn clean install is needed

  • you can run Eclipse in a new workspace, and import your project there, sometimes it helps in case of such strange problems

  • instead of importing Maven project to Eclipse via m2eclipse, you can try to create Eclipse files via the old mvn eclipse:eclipse and see what happens then

  • does it work well when you try to import your Maven project to other IDE, the free IntelliJ Community Edition for example?

查看更多
一纸荒年 Trace。
5楼-- · 2019-01-09 10:52

As a last resort, you can delete your current Eclipse installation and install a new version. When you add several plugins, they might interfere with one another and create weird behavior. After you do that, do not import your Maven project into your workspace, but rather create a new one and copy and paste the files that you had.

查看更多
爷、活的狠高调
6楼-- · 2019-01-09 11:02

When you import the project into Eclipse, use Eclipse's own built-in Maven support (aka, m2e). I recommend against using mvn eclipse:eclipse as it doesn't give the best results (as you're seeing). Maven is a build and dependency management tool, not an IDE; expecting it to manage IDE-specific stuff is silly, in my opinion (I realize the Maven team thinks differently, that Maven should be responsible for managing your IDE, but that's nonsense).

So if you have the project available on your system, delete any Eclipse-specific files (typically just .classpath, .project, and folder .settings), they were generated by mvn eclipse:eclipse and you don't want them interfering with the "proper" import process described here. Then inside Eclipse, use File > Import > Maven > Existing Maven Projects to import the project. That should result in better integration between Eclipse and maven, including automatically updating the Eclipse build path when the pom is changed.

As a quick check, after doing the import that way, you should see a group called Maven Dependencies in the Libraries tab of the project's Build Path (in Properties dialog). Like this:

enter image description here

If you want the Eclipse project configuration to be automatically updated every time the pom is changed, there's a (experimental) setting for that under Preferences > Maven. Be aware that doing so might not be desirable, though - as mentioned in this feature request, it's a somewhat lengthy process that touches a bunch of stuff in the Eclipse Project; doing that automatically on every pom.xml change could end up being more trouble than it's worth.

enter image description here

查看更多
登录 后发表回答