10

I am adding M2_REPO variable in eclipse using Window --> Preferences --> Java --> Build Path --> Classpath Variables --> New --> and then providing name as M2_REPO and selecting maven repository folder, then I see this variable getting added in the Classpath variables list. Then I click OK button.

After that, if again I see the above Classpath variables, then i don't find the M2_REPO variable there, due to which I am getting Unbound classpath variable: M2_REPO/....jar error in my project.

Can someone please help.

Anand
  • 20,708
  • 48
  • 131
  • 198

6 Answers6

5

Check for settings.xml in

Window --> Preferences --> Maven ---> User Settings

If not, set it there and change localRepository path in settings tag inside settings.xml. Normally you will find settings.xml in .m2 folder under the user folder (for eg. C:\Documents and Settings\userName.m2).

Riyaz Mohammed Ibrahim
  • 9,505
  • 5
  • 26
  • 33
1

I think that you're doing it wrong. If you're using Eclipse (a later version, something like Juno), you shouldn't deal with the M2_REPO classpath variable at all. You should adjust your Maven settings by going to Window -> Preferences -> Maven -> User Settings. The M2_REPO is derived from those settings and you must not override it manually.

Isaac
  • 16,458
  • 5
  • 57
  • 81
  • 1
    Why must not? You can change the var whether the mvn plugin is installed or not. – xeye Dec 03 '12 at 09:23
  • Actually, in Eclipse Juno, if you have the Maven plugin installed, you can't modify `M2_REPO`. It's set as unmodifiable. – Isaac Dec 03 '12 at 09:38
1

My M2_REPO variable kept disappearing as well, the hint for me was here:

http://maven.40175.n5.nabble.com/Eclipse-Plugin-how-to-remove-M2-REPO-classpath-variable-re-post-td3375082.html

I'm running Spring Tool Suite 3.6.2 (which is based off Eclipse Luna).

In my workspace there's a file workspace-sts-3.6.2.RELEASE\.metadata\.plugins\org.eclipse.core.runtime\.settings\org.eclipse.jdt.core.prefs

Add the following line:

org.eclipse.jdt.core.classpathVariable.M2_REPO=<your repo location>

Restart Eclipse/STS, and the M2_REPO classpath variable should be there again.

Edit: It happened to me again. :'(

The step above didn't work the second time, but doing

mvn -Declipse.workspace="(path to workspace)" eclipse:configure-workspace

added the M2_REPO variable again.

Fodder
  • 564
  • 1
  • 10
  • 23
  • If some is still facing the issue related to M2_REPO getting disappeared while creating the new the variable. The solution is to clean the folders/files in the metadata folder and restart the eclipse-sts. Once that is done point to the correct settings.xml file in the maven folder. M2_REPO will appear updated. – Nayan Mar 08 '17 at 09:32
0

In my case it turned out that I had a problem with maven. Paste

mvn --version

in your command line. Does it show maven version correctly? If not, then check your system variables : M2_HOME, M2. If they are not set -> set it to the correct maven directory and restart Eclipse. Everything should be fine, M2_REPO will be added automatically.

If they exist and they are set to the correct maven directory, check your JAVA_HOME and PATH variables as different maven versions require different java version. If it's not correct version of java -> set it to the correct one.

This solved my problem for good:).

user2455862
  • 585
  • 10
  • 26
0

Possible cause of this problem is the m2e maven plugin. If using this plugin, I would suggest to remove it and use mvn eclipse:eclipse instead.

How to remove m2e, click here for help:

  1. Uninstall from Eclipse menu: About Eclipse -> Installation Details -> Installed Software
  2. Delete plugin files and folder with name m2e from plugin folder in eclipse installation location
Community
  • 1
  • 1
Barun
  • 1,520
  • 2
  • 12
  • 18
0

The direct form to resolve it:

mvn -Declipse.workspace="/path/to/workspace" eclipse:configure-workspace

Description of command in the documentation:

Configures The following Eclipse Workspace features

Adds the classpath variable MAVEN_REPO to Eclipse. Optionally load Eclipse code style file via a URL.

-Declipse.workspace is a required parameter Directory location of the Eclipse workspace. User property is: eclipse.workspace.

Ref: http://maven.apache.org/plugins/maven-eclipse-plugin/configure-workspace-mojo.html