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.
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.
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.
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.
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.
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.
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 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.
Tidak ada komentar:
Posting Komentar