How do I get my Eclipse-Maven project to automatically update its classpath when I change a dependency in my pom.xml file?

拥有回忆 提交于 2019-11-26 17:46:44

问题


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.


回答1:


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:

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.




回答2:


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.
  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.
  3. Always check for your user settings which should reflect your local maven settings.xml, as shown in the below figure.
  4. After performing all these checks, refresh your Eclipse Work-space to get these changes reflected.



回答3:


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:


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?




回答5:


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.



来源:https://stackoverflow.com/questions/35511860/how-do-i-get-my-eclipse-maven-project-to-automatically-update-its-classpath-when

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!