Tuesday, September 9, 2014

Jar Artifiact Dependency Search Not Working in Eclipse - Solved

Recently I face an strange issue while using Maven in Eclipse via M2Eclipse plugin. I have created a Maven Java project in Eclipse and subsequently tried to add Spring framework as dependency, to my surprise, nothing was happening. Eclipse was not able to search dependency in Maven central repository. I was surprised because I have never faced this issue while I was using M2Eclipse plugin from long time. After some trial and error, I realized that this is my brand new workspace and I am running with Eclipse Luna, newest version of Eclipse which I had just downloaded couple days of back. My first hunch was that Eclipse might not able to connect to Internet, and to verify that I tried connecting Eclipse market place, and boom it was working fine. Then I quickly checked Maven settings in Eclipse and found that the option which is required to download and build the index was not turned on. The check box was unchecked there. To give you some background about how Eclipse search dependency on Maven remote repository, be it default Maven central or internal Nexus repository. Eclipse search artifact dependency on repository's index file. If that file is not download or not exist due to any reason, Maven's dependency search will not work in Eclipse. That's why you need to check the option "Download repository Index updates on startup". If this option is enabled then Eclipse will download repository index from configured location e.g. Maven central or Nexus and every time Eclipse will start it will just do update. You should also check the option "Do not automatically update dependencies from remote repositories", because that will download latest build every time they were pushed into Maven central repository. By the way, this error has nothing to do with Eclipse Luna. I received emails from my readers that some of them not able to add dependency in Maven project even in older versions of Eclipse e.g. Kepler and Indigo version. It is totally due to absent of artifact index file. One you enable "Download repository Index updates on startup" option and restart Eclipse, you will see that Eclipse is updating index at the bottom right corner, once this update this finish, you would be able to add dependency using Eclipse Maven GUI. In worst case, if your problem doesn't resolve even after enabling and restarting Eclipse, try re-building Index from Maven repository view in Eclipse, as shown in images attached here.

How to Fix Maven Eclipse Dependency Problem 

Let me give you step by step guide to deal with this problem in Eclipse. If you have fresh workspace, just try creating a Maven project then option it's pom.xml file and go to Eclipse GUI (don't open XML view). Now try adding a dependency by clicking into "add" button, this will open another dialog box with a search bar to search artifacts. If you know the artifact id and group id you can directly add any dependency but most of us doesn't remember that, what we do is search for that artifact or JAR by typing e.g. to add JAR file related to Hibernate or SpringFramework you will type hibernate or spring framework in given search bar. At first you will see it's not working as shown in first screenshot.

1. Eclipse Maven Plugin Not able to add dependency by Searching artifacts
You can see I was searching for spring framework Java but there is no search result, in fact it was saying artifact id cannot be empty. Don't be fool with that error this was just because we are not adding any dependency explicitly. Main problem here is that Eclipse is not able to search for dependencies when you enter some keyword in search box.
Not able to add Maven dependency in Eclipse Luna

2. Maven Settings Option in Eclipse to Enable Dependency Search from Remote Repository
In order to enable Maven dependency management feature in Eclipse, it's not just you download and install M2Eclipse plugin but also you need to enable this options. It would be better if they are by default enabled but in Eclipse Luna it's not.  "Download repository Index updates on startup" is the option which should be enabled otherwise you would not be able to add dependency from Eclipse GUI. Second option  "Do not automatically update dependencies from remote repositories" is more to keep your start-up faster. If you enable it and you have couple of projects with lot of Maven dependencies then it will take some time before you can do anything. 
Maven Settings on Eclipse

3. Eclipse start updating Maven Index after restart
This screenshot is immediately after restarting Eclipse, you can see that at bottom right corner it showing as Updating index, which is the proof that it's now downloading index file. If you double click there it will open the view which we have shown in next screenshot, there you can see that it downloads dependencies form Maven central remote repository.
Eclipse Updating Maven Index on startup

4. Eclipse by default download Maven dependency from Maven central remote repository
You can see that Eclipse is updating indexes and it's doing it from Maven central repository, http://repo.maven.apache.org/maven2. If you had configured internal nexus repository then it will download from there.
Eclipse downloading index form Maven central repository

5. Maven Dependency Search is Now working in Eclipse

Problem is fixed now, you can see that Eclipse is now able to search dependency from Maven repository. Usually after enabling those option and restarting eclipse, it will start updating index and once it finished, you would be able to search and add artificats into your pom.xml using Eclipse.
Maven Dependency Search is now working in Eclipse

6. Re-build Maven Index from Maven Repository view in Eclipse
In order to open Maven repository view, goto Window  -> Show View  -> Other -> Maven -> Maven Repositories . It will show you all kind of maven repositories e.g. local repository, Global repository, project repository and Custom repository. It is the Global repository on which Maven central library (http://repo.maven.apache.org/maven2) is configured. Just right click there and select Rebuild Index option. This will rebuild repository index for local use, and will help you to quickly add any JAR file dependency in your Maven project.
How to open Maven repository View in Eclipse

That's all about how do you fix this problem of Maven not able to search dependencies in Eclipse. As I said it usually happens when you are the first time trying to create a Maven project in a brand new workspace and new installation of Eclipse where this options are not enabled by default. This is one-time exercise, so there is very less likely hood of happening this again. Once you enable option to "Download repository Index updates on startup", Maven will remember it and it will keep updating index every time you restart the Eclipse. Sometime this makes your Eclipse little bit slower so you might want to disable it and then do it manually. You can also face this problem of not downloading artifact if your Eclipse is behind corporate proxy network and not able to connect to Maven central remote repository using internet. In that case just use proper proxy settings in your network connection, see this tutorial for step by step guide of fixing Eclipse not able to connect market place problem. Another scenario on which you can face this error is You can also get this if you are using internal Maven repository, for example Nexus and the index into the proxy is messed up somehow. In that case just contact your local Maven repository admin for help.

Further Learning
Maven Fundamentals by Bryan Hansen
The Eclipse Guided Tour - Part 1 and 2
Java Fundamentals: The Java Language

Other Java Eclipse articles you may like to explore
  • 30 Useful Eclipse Shortcuts for Java Developers (list)
  • How to remote debug Java application in Eclipse? (tutorial)
  • 10 Eclipse debugging tips Java developer should know? (see here)
  • How to attach source code for JAR file in Eclipse? (guide)
  • Eclipse shortcut to print System.out.println statements? (shortcut)
  • How to increase console buffer size in Eclipse? (steps)
  • How to use spaces instead of tabs in Eclipse? (guide)
  • How to create an executable JAR file from Eclipse? (example)
  • 3 Books to Learn Eclipse IDE for Java developers (list)
  • How to Increase Heap Size of Java Program running in Eclipse? (guide)

Thanks for reading this article so far. If you like this article then please share with your friends and colleagues. If you have any questions or feedback then please drop a comment.


Anonymous said...

I don't know what to do but my Eclipse's Maven is not able to search dependency at all. Its able to download dependencies already listed on pom.xml but when I try to search for a new library, its simply not working. It only shows the libraries in local repository. I was facing this problem in Luna and found that my settings.xml is deleted from .m2 folder, so I tried to update the m2eclipse plugin without any success. Then I installed an older version of Eclipse Kepler but its still not working. It was picking same maven settings and settings.xml was still missing, so I downloaded apache maven 3.1 version and provided the path of conf directory, still not working. Then I deleted the .m2 directory but Eclipse was able to created it when restarted and able to download dependency already required by project, but still search is not working.

Then I tried to update the Maven dependency index as suggested by you in Maven view, only to realize that I am not able to set it to full index, instead I am getting following error :
unable to set the index details due to the following error read past eof

Can anyone please suggest what is wrong and how to fix this error? I am really frustrated after spending hours to make maven dependency search work in Eclipse without any luck.

Brendon said...

The solution of the error is "unable to set the index details due to the following error read past eof" is

Close Eclipse
Delete workspace_location/.metadata/.plugins/org.maven.ide.eclipse/nexus
Delete workspace_location/.metadata/.plugins/org.eclipse.m2e.core/nexus
Go to Window then Preferences- then Maven and enable checkbox named “Download repository index updates on startup”.
OK and restart Eclipse.

After this you will see at right bottom corner of the Eclipse that its updating indexes. Now if you go to Maven view and expand Global repository you will see it updating as opposed to nothing when that error was there. You don't need to downgrade to Kepler from Luna to solve this error.

Alex said...

I had to update Maven Plugin. It was not working. (Help menu > Eclipse Marketplace > type Maven > click magnifying glass) > Update

Murugan said...

Really helpful .., But while I was trying to rebuild the index, it takes more time to complete the process. Any idea on what might be the reason of it?

Javin Paul said...

@Murugan, could be the slow internet, but it usually does take some time because of so many artifacts on maven central repository.

Unknown said...

Awesome explanation for the problem. This works. Really helpful. Huge thanks to the author.

Post a Comment