设置 M2_REPO 类路径

发布于 2024-10-09 06:09:51 字数 234 浏览 0 评论 0原文

我有几个与此相对应的问题。

  1. 什么是M2_REPO?
  2. 这个命令有什么作用?

    mvn -Declipse.workspace=; eclipse:add-maven-repo

  3. 为什么我们需要执行上述操作?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无法言说的痛 2024-10-16 06:09:51
  1. M2_REPO 是一个变量,用于定义 Maven 2 存储库在磁盘上的位置。
  2. 这意味着:将 M2_REPO 的定义添加到定义 Eclipse 工作空间的 XML 文件中
  3. 如果需要,您可以手动执行相同的操作。我个人就是这么做的。只需转到 Window/Preferences,然后选择 Java/Build Path/Classpath Variables。一旦完成,您就可以享受 Maven 与 Eclipse 的集成了。每次向 pom.xml 添加新的依赖项时,运行

    mvn eclipse:eclipse

并刷新工作区,您就会将所有新库添加到项目的类路径中。

编辑

这个答案是 5 年前写的。现在任何eclipse发行版都有maven插件,因此eclipse可以直接打开maven项目。您添加到 pom.xml 的每个依赖项都会自动下载,并且几乎可以立即使用。

  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.

情魔剑神 2024-10-16 06:09:51

转到Windows->偏好-> Java -> 构建路径 ->类路径变量 ->新建并添加以下内容;

Name = M2_REPO(或者您为 Maven 存储库指定的任何名称)
Path = C:\Users\Administrator.m2\repository (我的保存在这里)

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)

烟酉 2024-10-16 06:09:51

不,您不希望所有 Maven 工件散布在 Eclipse 项目/包资源管理器中的 Libraries 文件夹中。

您希望所有 Maven 工件都很好地包装在一个子文件夹下

Java 资源 >图书馆> Maven 依赖项

就像过去一样。

完成其余答案中建议的所有操作后,您得到的仍然是所有 Maven 工件散布在 pkg/prj 资源管理器的顶级列表的顶层,原因是因为您刚刚升级到最新版本并且最好的 Eclipse 版本,而您拥有的 m2eclipse 插件版本尚未与该 Eclipse 版本同步更新。

也不想每次添加新的 Maven 依赖项时都运行 mvn eclipse:eclipse 或 eclipse:config..。这根本违背了 Maven 的立场。

解决方法

当 m2eclipse 在你的全新 eclipse 版本中摸索时,

  1. 保存当前的 .classpath。

  2. 用POME.C替换你的eclipse .classpath(普通的旧maven eclipse .classpath):
    http://code.google.com/p/synthfuljava/ source/browse/apps/durian/.classpath .

  3. 记住将java版本更改为您的版本。

  4. 请记住添加回在您的原始 .classpath 中找到的所有自定义非 Maven 路径。

  5. 现在,做一个好公民,为这个 eclipse 版本提交一个 m2eclipse bug(如果我还没有),告诉他们 m2eclipse 没有构建 good'ol POME.C,解释 POME 是多么可爱和方便.C 是 .

但由于 googlecode 将在今年 8 月消失(这很痛苦,google),这里是文件的正文。

<?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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文