13

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.

Dave
  • 15,639
  • 133
  • 442
  • 830
  • does this help http://stackoverflow.com/questions/1212633/can-eclipse-refresh-resources-automatically? – Naman Feb 20 '16 at 04:34
  • I don't think its the same thing -- that setting was already marked in my Eclipse distro, but the link referes to "resources," and I'm talking about getting classpath dependencies updated in my project based on a Maven pom dependency changing its versions. Does "resources" cover what I'm talking about because I'm not seeing those updates occurring right now. – Dave Feb 22 '16 at 15:06
  • Did you update everything? Eclipse? M2E? Make sure you're using the latest versions. – Tunaki Mar 11 '16 at 20:35
  • Yup, tried that, restarted Eclipse but still get the same compilation errors (errors that don't occur when I run a Maven build on the command line) indicating that my Maven dependencies weren't updated, when they were. – Dave Mar 14 '16 at 15:36
  • Can you show a screen shot of the Project's build path (Libraries tab)? I'd like to see if the Maven classpath container is actually there. – E-Riz Mar 15 '16 at 19:34
  • There is a scrolling list of entries so they wouldn't all fit in a screen shot but they all look like this -- "M2_REPO/antlr/2.7.7/antlr-2.7.7.jar - /Users/davea/.m2/repository/antlr/2.7.7/antlr-2.7.7.jar" (all begin with M2_REPO). – Dave Mar 16 '16 at 14:06
  • If the project's Libraries tab does not include a group called "Maven Dependencies" then you're not using Eclipse's m2e maven support. Explain the steps you took to import the project into Eclipse. – E-Riz Mar 17 '16 at 14:09

5 Answers5

28

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

E-Riz
  • 31,431
  • 9
  • 97
  • 134
  • K, I deleted those three files, deleted and re-imported the project using your suggestion (incdietnally doing that recreated those three files). Then I changed a depednency version in teh parent pom, did a project refresh, but the dependencies in the Library window you show above still showed the old dependencies. – Dave Mar 18 '16 at 16:31
  • 2
    Did you right-click on the project and select **Maven** > **Update Project...** ? I thought it was automatic but I might be wrong about that detail. – E-Riz Mar 18 '16 at 18:21
  • Its a far cry from things updating automatically, but it absolutely worked. Thanks! – Dave Mar 18 '16 at 19:41
  • I updated the answer - there is a setting to automatically update when the pom changes. – E-Riz Mar 18 '16 at 19:52
6

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.
Praveen Kumar K S
  • 3,024
  • 1
  • 24
  • 31
  • Thanks for this very detailed explanation. I'm still not noticing my project getting refreshed, even when I click Project -> Refresh. Does it matter that I'm making a change to the parent pom from which my current project inherits? Does that change how I shoudl apply your answer? – Dave Mar 18 '16 at 13:53
  • ... because it has nothing to do with repos if project reimport solves the issue; it's something wrong with m2eclipse most likely – iirekm Mar 18 '16 at 16:20
  • @iirekm, When you use Eclipse Mars version, it is required to check these above steps and at sometimes it works when we do project re-import (even for me it worked), but it is always recommended to perform these checks to avoid such type of build issues. – Praveen Kumar K S Mar 18 '16 at 16:26
0

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.

Teemu Ilmonen
  • 316
  • 1
  • 5
  • Re-arranging the natures in the .project file doesn't work. Also closing and re-opening the project doesn't work. I know the dependencies aren't updated in Eclipse beacuse I see comiplation errors in my Java file, complaining about a version of something that was updated in the pom. Eclipse doens't pick up this updated version of the pom until I re-import the project, which I want to avoid. – Dave Mar 14 '16 at 15:29
0

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?

iirekm
  • 8,890
  • 5
  • 36
  • 46
  • 1
    Hi, Yes, everything works when I do "mvn install" from teh command line. I'm not going to create a new workspace or import this into a new IDE. As I said, dependencies update when I delete the project and re-import it but my question is what can I do to avoid having to go through those steps? – Dave Mar 18 '16 at 16:34
-3

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.

Miguel Velez
  • 586
  • 2
  • 8
  • 19