Java 如何配置 Eclipse 构建路径以使用 Maven 依赖项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2037188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:51:41  来源:igfitidea点击:

How to configure Eclipse build path to use Maven dependencies?

javaeclipsemaven-2idem2eclipse

提问by James Goodwin

I would like to take advantage of the features that Maven provides for managing dependencies in a project. My brief understanding of how Maven works is that it will aquire the JARs needed and then build the project with these libraries.

我想利用 Maven 提供的用于管理项目中的依赖项的功能。我对 Maven 工作原理的简要理解是,它将获取所需的 JAR,然后使用这些库构建项目。

Currently I have a simple POM file set up as a test:

目前我有一个简单的 POM 文件设置为测试:

<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.jamesgoodwin.test</groupId>
  <artifactId>com.jamesgoodwin.test</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
  </build>
  <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>3.0.0.RELEASE</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
  </dependencies>
</project>

To manage dependencies, I usually add the project or JAR to the build path and then i'll be able to build my project..

为了管理依赖项,我通常将项目或 JAR 添加到构建路径中,然后我就可以构建我的项目了。

But when using M2Eclipsethe dependencies are not added automatically to the build path. Is there any configuration to let Eclipse know that the Maven is managing the dependencies?

但是当使用M2Eclipse 时,依赖项不会自动添加到构建路径中。是否有任何配置可以让 Eclipse 知道 Maven 正在管理依赖项?

采纳答案by Buhb

If you right-click on your project, there should be an option under "maven" to "enable dependency management". That's it.

如果您右键单击您的项目,“maven”下应该有一个选项来“启用依赖管理”。就是这样。

回答by davek

if you execute

如果你执行

mvn eclipse:clean

followed by

其次是

mvn eclipse:eclipse

if will prepare the eclipse .classpathfile for you. That is, these commands are run against maven from the command line i.e. outside of eclipse.

if 将为您准备 eclipse.classpath文件。也就是说,这些命令是从命令行(即 eclipse 之外)针对 maven 运行的。

回答by Juha Syrj?l?

Maybe you could look into maven-eclipse-plugininstead of M2Eclipse.

也许您可以查看maven-eclipse-plugin而不是 M2Eclipse。

There you basically add maven-eclipse-plugin configuration to your pom.xml and then execute mvn eclipse:eclipsewhich will generate the required .projectand .classpathfiles for Eclipse. Then you'll have the correct build path in Eclipse.

在那里,你基本上添加Maven的Eclipse的插件配置到你的pom.xml,然后执行mvn eclipse:eclipse将产生需要的.project.classpathEclipse的文件。然后您将在 Eclipse 中拥有正确的构建路径。

回答by EJB

When m2eclipse is installed properly, it should add dependencies automatically. However, you should generate the eclipse project files by entering:

当 m2eclipse 安装正确后,它应该会自动添加依赖项。但是,您应该通过输入以下内容来生成 eclipse 项目文件:

mvn eclipse:m2eclipse

or, alternatively if you don't use m2eclipse:

或者,如果您不使用 m2eclipse:

mvn eclipse:eclipse

回答by Pascal Thivent

I'm assuming you are using m2eclipse as you mentioned it. However it is not clear whether you created your project under Eclipse or not so I'll try to cover all cases.

我假设你正在使用你提到的 m2eclipse。但是,不清楚您是否在 Eclipse 下创建了项目,因此我将尝试涵盖所有情况。

  1. If you created a "Java" project under Eclipse (Ctrl+N > Java Project), then right-clickthe project in the Package Explorerview and go to Maven > Enable Dependency Management(depending on the initial project structure, you may have modify it to match the maven's one, for example by adding src/javato the source folders on the build path).

  2. If you created a "Maven Project" under Eclipse (Ctrl+N > Maven Project), then it should be already "Maven ready".

  3. If you created a Maven project outside Eclipse (manually or with an archetype), then simply importit in Eclipse (right-clickthe Package Explorerview and select Import... > Maven Projects) and it will be "Maven ready".

  1. 如果您在 Eclipse 下创建了一个“Java”项目(Ctrl+N > Java 项目),然后在Package Explorer视图中右键单击该项目 并转到Maven > Enable Dependency Management(根据初始项目结构,您可能已经修改它与 Maven 的匹配,例如通过添加到构建路径上的源文件夹)。src/java

  2. 如果您在 Eclipse 下创建了一个“Maven 项目”(Ctrl+N > Maven 项目),那么它应该已经“Maven 准备好了”。

  3. 如果你创建的Eclipse(手动或使用原型),然后简单地之外的Maven项目导入它在Eclipse(右键单击包资源管理器视图,并选择导入...> Maven项目),这将是“Maven的准备”。

Now, to add a dependency, either right-clickthe project and select Maven > Add Dependency) or edit the pom manually.

现在,要添加依赖项,请右键单击该项目并选择Maven > Add Dependency)或手动编辑 pom。

PS: avoid using the maven-eclipse-pluginif you are using m2eclipse. There is absolutely no need for it, it will be confusing, it will generate some mess. No, really, don't use it unless you really know what you are doing.

PS:如果您使用的是 m2eclipse,请避免使用maven-eclipse-plugin。绝对不需要它,它会令人困惑,它会产生一些混乱。不,真的,除非您真的知道自己在做什么,否则不要使用它。

回答by Mearde

I had a slight variation that caused some issues - multiple sub projects within one project. In this case I needed to go into each individual folder that contained a POM, execute the mvn eclipse:eclipse command and then manually copy/merge the classpath entries into my project classpath file.

我有一个轻微的变化,导致了一些问题——一个项目中有多个子项目。在这种情况下,我需要进入包含 POM 的每个单独文件夹,执行 mvn eclipse:eclipse 命令,然后手动将类路径条目复制/合并到我的项目类路径文件中。

回答by DevJav

Right click on the project Configure > convert to Maven project

右键单击项目配置>转换为Maven项目

Then you can see all the Maven related Menu for you project.

然后你可以看到你项目的所有 Maven 相关菜单。

回答by madhu_karnati

I did like this..

我就是喜欢这个。。

Right click on the project--> configure-->convert to maven project. Right click on the project-->maven-->add dependencies.

右键项目-->配置-->转换为maven项目。右键项目-->maven-->添加依赖。

回答by borjab

I did not found the maven or configure menus but found the following button that solved my problem:

我没有找到 maven 或配置菜单,但找到了解决我的问题的以下按钮:

enter image description here

enter image description here

回答by Thomas Beer

In Eclipse (Mars) go to:

在 Eclipse (Mars) 中,转到:

Project Properties --> Maven. Ensure that "Resolve dependencies from workspace projects" is disabled.

项目属性 --> Maven。确保禁用“解决工作区项目的依赖关系” 。