How to add M2_REPO classpath variable to Eclipse IDE

Normally, when you use Maven command mvn eclipse:eclipse to convert existing Java project to support Eclipse project, Maven will create the entire dependency classpath by using the M2_REPO variable, which is not defined in Eclipse by default.

Nothing special, M2_REPO is just a normal “classpath variable” in Eclipse to find your local Maven repository.

Here, we show you two ways to add M2_REPO classpath variable into Eclipse IDE.

1. Add M2_REPO Manually

Define and add M2_REPO classpath variable manually into Eclipse IDE. Follow below steps :

  1. Eclipse IDE, menu bar
  2. Select Window > Preferences
  3. Select Java > Build Path > Classpath Variables
  4. Click on the new button > defined a new M2_REPO variable and point it to your local Maven repository
  5. Done.

2. Add M2_REPO Automatically – eclipse:configure-workspace

Alternatively, you can define and add M2_REPO via Maven command “eclipse:configure-workspace“.


mvn -Declipse.workspace="your Eclipse Workspace" eclipse:configure-workspace

See an example :


C:\>mvn -Declipse.workspace="C:\Users\mkyong\workspace" eclipse:configure-workspace
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'eclipse'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Default Project
[INFO]    task-segment: [eclipse:configure-workspace] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] [eclipse:configure-workspace {execution: default-cli}]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Thu Apr 14 20:45:17 SGT 2011
[INFO] Final Memory: 9M/112M
[INFO] ------------------------------------------------------------------------

Done.

Verify M2_REPO is Added
Review classpath variable to verify that the M2_REPOis added into Eclipse IDE.

Add ME_REPO to Eclipse IDE

You need to add this M2_REPO classpath variable once only, and this variable is shared among all of your Eclipse’s workspace.

eclipse:add-maven-repo
The old “eclipse:add-maven-repo” command is deprecated , and not working anymore. Uses “eclipse:configure-workspace” instead.

References

  1. Add Maven repo example
  2. Configure Maven workspace

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
tenne
Guest
tenne

hi m,
I hv added the m2_repo as you said. Manually and automatically. it also shown in classpath too. but it doen’t resolve dependencies still i have to resolve those manually by adding external jars. do you have any idea why is this happening

Gábor Szárnyas
Guest
Gábor Szárnyas

The m2e Eclipse plugin also registers the M2_REPO variable. However, please note that “eclipse:eclipse”-generated projects are not compatible with m2e so even if you have m2e, you should *not* convert these project to Maven projects in Eclipse.

The “mvn eclipse:eclipse” command even generates a comment to the .project file:

NO_M2ECLIPSE_SUPPORT: Project files created with the maven-eclipse-plugin are not supported in M2Eclipse.

Dennis Wanjama
Guest
Dennis Wanjama

thanks a lot

JorgeThelaw
Guest
JorgeThelaw

Nice!

Nancy
Guest
Nancy

Hi,
I have created maven home and java home in environmental variables. Can i use that in eclipse? maven plugin which has pom.xml is different from maven setting in environmental variables?

Sri Lakshmi
Guest
Sri Lakshmi
Iam new to android-ndk. Iam trying to build pure c code by using android-ndk finally its working. Now Iam trying to call assembly file from that c code but Iam getting errors. Iam using Linux_GCC /* This is my Android.mk file */ LOCAL_PATH := $(call-mydir) include $(CLEAR_VARS) LOCAL_MODULE := Sample LOCAL_SRC_FILES := test.c assembly.S LOCAL_ARM_MODE := arm LOCAL_LD_LIBS := -llog include $(BUILD SHARED_LIBS) TARGET_ARCH_ABI == armabi-v7a /* This is my application.mk file */ APP_OPTIM := debug APP_ABI := armeabi armeabi-v7a x86 APP_STL := gnustl_static /* This is my c file */ extern int assembly(int a); int main(void) { int a… Read more »
Renso
Guest
Renso

Thank you

Ankur Saxena
Guest
Ankur Saxena

Thanks

Umesh
Guest
Umesh

Awesome tutorial mkyong…..

David
Guest
David

Thanks this helped!

vmi
Guest
vmi

Thank You!

Me
Guest
Me

Thanks so much mate God help you.

ashish
Guest
ashish

Gr8 work yong!

trackback
Bits And Pix » Setting up Eclipse Classpath for Maven project

[…] Source […]

chandan kumar
Guest
chandan kumar

nice blogs

asher
Guest
asher

Hi MkYong,

Are you familiar with Eclipse Application developer installation if yes. I am making response file for pacakges installation. It keeps not accepting the repositories. I tried to create the response file: eclipse>ibmimc.exe -record c:\temp\1.xml -skipInstall e:\data
it recorded the first repository. When I tried to fill rest of the repository it give me error saying these repositories are not acceptable. My issues is embedding the repositories and all the options in a response file so it silently install the Eclipse and its 3 developer environments.

Please email me if you have a response.

Many thanks in advances.

Asher

Terence
Guest
Terence

Thanks this helped me out today.

Sarath Meepagala
Guest
Sarath Meepagala

This was very helpful. Thanks!

Gaurav
Guest
Gaurav

Hi mkyong :)

I have already installed maven and it had by default added .m2 directory in c: drive in the Document and Settings…. folders

but as now the link to change the maven repository path is coming hidden in eclipse

Can i still change it by any other means ???

Please revert me back soon

trackback
Maven Tutorial

[…] Tomcat “mvn tomcat:deploy” to deploy WAR file to Tomcat.Maven FAQSome common Maven FAQs.Add M2_REPO classpath variable to Eclipse IDE To make your Maven project support Eclipse IDE, you need to add this M2_REPO classpath variable.How […]

trackback
Resolving classpath issues with Eclipse + Maven « Vishwanath Krishnamurthi's blog

[…] Now the .classpath file is built by reading M2_REPO classpath variable, which  is not defined in eclipse by default. Check here to see how to add M2_REPO classpath variable to eclipse […]

trackback
Resolving classpath issues with Eclipse + Maven « Vishwanath Krishnamurthi's blog

[…] Now the .classpath file is built by reading M2_REPO classpath varialbe, which eclipse is not defined in eclipse by default. Check here to see how to add M2_REPO classpath variable to eclipse […]