Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up M2_REPO classpath

I have a few questions corresponding to this.

  1. What is M2_REPO?
  2. What does this command do?

    mvn -Declipse.workspace=<your_workspace_location> eclipse:add-maven-repo

  3. Why do we need to do the above?

like image 904
theJava Avatar asked Dec 23 '10 09:12

theJava


People also ask

What is M2_REPO?

M2_REPO is a variable that defines where maven 2 repository is on your disk. This means: add definition of M2_REPO to XML file that defines the eclipse workspace. You can do the same manually if you want. That is what I personally did. Just go to Window/Preferences and then choose Java/Build Path/Classpath Variables.

How do I open the classpath in Eclipse?

In Eclipse, go to Window > Preferences > Java > Build Path > Classpath Variables. Click New, enter the following information and click OK.

What is .m2 folder in Eclipse?

m2/repository by default. It also contains a repository that represents the Maven projects contained in your Eclipse workspace. Global Repositories. This folder contains any global Maven repositories that are referenced by all Maven projects.


3 Answers

  1. M2_REPO is a variable that defines where maven 2 repository is on your disk
  2. This means: add definition of M2_REPO to XML file that defines the eclipse workspace
  3. You can do the same manually if you want. That is what I personally did. Just go to Window/Preferences and then choose Java/Build Path/Classpath Variables. Once you did it you can enjoy maven integration with eclipse. Every time you add new dependency to your pom.xml, run

    mvn eclipse:eclipse

and refresh you workspace you get all new libraries into classpath of your project.

EDIT

This answer was written 5 years ago. These days any eclipse distribution has maven plugin, so eclipse can open maven project directly. Every dependency you add to your pom.xml is automatically downloaded and almost immediately ready to use.

like image 78
AlexR Avatar answered Oct 12 '22 23:10

AlexR


Go to Windows-> Preference -> Java ->Build Path -> Classpath Variables -> New and add the following ;

Name = M2_REPO (or what ever name you gave for your Maven repository) Path = C:\Users\Administrator.m2\repository (Mine is saved here)

like image 39
Illep Avatar answered Oct 12 '22 23:10

Illep


No, you don't want all your maven artifacts strewn all over the Libraries folder in eclipse project/package explorer.

You want all the maven artifacts wrapped nicely under one subfolder of

Java Resources > Libraries > Maven Dependencies

just like in the gd'old days.

After you done everything suggested in the rest of the answers, and all you get is still having all your maven artefacts strewn over the top level of the pkg/prj explorer's top level listing, the reason is because you have just upgraded to the latest and greatest Eclipse version, and the m2eclipse plugin version you have has not raced uptodate with that Eclipse version.

You ALSO don't want to run mvn eclipse:eclipse or eclipse:config.. everytime you add new maven dependencies. That simply goes against what maven stands for.

Work around

While m2eclipse is groping around in the dark in your spanking new eclipse version,

  1. Save your current .classpath.

  2. replace your eclipse .classpath with the POME.C (plain old maven eclipse .classpath): http://code.google.com/p/synthfuljava/source/browse/apps/durian/.classpath .

  3. Remember to change the java version to yours.

  4. Remember to add back all your custom non-maven paths, found in your orig .classpath.

  5. Now, be a good citizen and go file an m2eclipse bug (if I haven't yet) for this eclipse version, telling them the m2eclipse is not constructing the good'ol POME.C, explaining how lovely and convenient that POME.C is .

But since googlecode is going away this august (that's a pain, google), here is the body of the file.

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
  <classpathentry kind="src" output="target/classes" path="src/main/java">
    <attributes>
      <attribute name="optional" value="true"/>
      <attribute name="maven.pomderived" value="true"/>
    </attributes>
  </classpathentry>
  <classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
    <attributes>
      <attribute name="maven.pomderived" value="true"/>
    </attributes>
  </classpathentry>
  <classpathentry kind="src" output="target/test-classes" path="src/test/java">
    <attributes>
      <attribute name="optional" value="true"/>
      <attribute name="maven.pomderived" value="true"/>
    </attributes>
  </classpathentry>
  <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
    <attributes>
      <attribute name="maven.pomderived" value="true"/>
      <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
    </attributes>
  </classpathentry>
  <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
    <attributes>
      <attribute name="maven.pomderived" value="true"/>
    </attributes>
  </classpathentry>
  <classpathentry kind="output" path="target/classes"/>
</classpath>
like image 37
Blessed Geek Avatar answered Oct 13 '22 01:10

Blessed Geek