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

时间:2021-10-31 00:02:15

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>

Usually when managing dependencies for a project I would add the project or JAR to the project build path and then be able to build my project..

通常,当管理项目的依赖关系时,我会将项目或JAR添加到项目构建路径中,然后才能构建我的项目。

But when using M2Eclipse the dependencies do are not automatically added to the build path. Is there somewhere I need to configure this to allow Eclipse to know that Maven is managing the dependencies?

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

18 个解决方案

#1


103  

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

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

#2


59  

if you execute

如果你执行

mvn eclipse:clean

followed by

紧随其后的是

mvn eclipse:eclipse

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

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

#3


28  

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-click the project in the Package Explorer view 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/java to the source folders on the build path).

    如果你创建了一个“Java”项目在Eclipse(Ctrl + N > Java项目),然后在Package Explorer视图中右键单击项目,去Maven >使依赖关系管理(根据项目的初始结构,你可以修改它与Maven的,例如通过添加src / Java构建路径上的源文件夹)。

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

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

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

    如果您在Eclipse外部创建了一个Maven项目(手动或使用原型),那么只需在Eclipse中导入它(右键单击Package Explorer视图并选择import……> Maven项目),它将是“Maven就绪”。

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

现在,要添加依赖项,可以右键单击项目并选择Maven >添加依赖项),或者手动编辑pom。

PS: avoid using the maven-eclipse-plugin if 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.

注意:如果使用m2eclipse,请避免使用maven-eclipse插件。这是完全没有必要的,它会令人困惑,它会产生一些混乱。不,真的,不要用它,除非你真的知道你在做什么。

#4


14  

Right click on the project Configure > convert to Maven project

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

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

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

#5


10  

Add this to .classpath file ..

将此添加到.classpath文件中。

<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
    <attributes>
        <attribute name="maven.pomderived" value="true"/>
    </attributes>
</classpathentry>

Thx

谢谢

#6


9  

I did like this..

我确实喜欢这个. .

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

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

#7


5  

In Eclipse (Mars) go to:

在Eclipse(火星)中:

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

项目属性- - > Maven。确保“消除来自工作空间项目的依赖关系”被禁用。

#8


4  

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

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

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

#9


2  

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

#10


1  

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命令,然后手动将类路径条目复制/合并到我的项目类路径文件中。

#11


1  

Adding my answers for a couple of reasons:

添加我的答案有几个原因:

  • Somehow none of the answers listed directly resolved my problem.
  • 不知何故,列出的答案中没有一个直接解决了我的问题。
  • I couldn't find "Enable dependency management" under Maven. I'm using Eclipse 4.4.2 build on Wed, 4 Feb 2015.
  • 我在Maven下找不到“启用依赖项管理”。我在2015年2月4日周三使用Eclipse 4.4.2构建。

What helped me was another option under Maven called as "Update Project" and then when I click it this window opens which has a checkbox that says "Force update of Snapshot/Releases". The real purpose of this checkbox is different I know but somehow it resolved the dependencies issue. 如何配置Eclipse构建路径以使用Maven依赖项?

帮助我的是Maven下的另一个名为“更新项目”的选项,当我点击这个窗口时,它会打开一个复选框,上面写着“强制更新快照/发布”。我知道这个复选框的真正目的不同,但是它以某种方式解决了依赖关系问题。

#12


0  

Maybe you could look into maven-eclipse-plugin instead of M2Eclipse.

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

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

在那里你基本上添加了maven-eclipse-plugin配置到你的pom中。然后执行mvn eclipse:eclipse,它将为eclipse生成所需的.project和.classpath文件。然后您将在Eclipse中获得正确的构建路径。

#13


0  

For newer Eclipse versions (>=Mars) right click on project > configure > convert to Maven project

对于更新的Eclipse版本(>=Mars),右键单击项目>,配置>转换为Maven项目

#14


0  

Make sure your POM follows the naming convention, and is named in lowercase lettering as pom.xml and NOT POM.xml.

确保您的POM遵循命名约定,并以小写字母作为POM命名。xml而不是POM.xml。

In my case all was right, but Eclipse still complained when trying to Right-click and Update project configuration - told me that the POM could not be read. Changed the name to lowercase - pom.xml from POM.xml - and it worked.

在我的例子中,一切都是正确的,但是Eclipse仍然抱怨当尝试右键单击并更新项目配置时——告诉我POM无法读取。将名称更改为小写- pom。从POM xml。xml——而且它起作用了。

#15


0  

I met this issue too. When I add dependencies in the pom.xml, I checked in the local folder /Users/xyz/.m2/ and the jars are already downloaded there, but cann't added the the buildpath of the eclipse.

我也遇到过这个问题。当我在pom中添加依赖项时。xml,我检查了本地文件夹/用户/xyz/。m2/ jar已经在那里下载了,但是不能添加eclipse的构建路径。

My eclipse Version: Mars.2 Release (4.5.2)

right click project > Maven > Enable Workspace Resolution

And this solved my issue.

我的eclipse版本:Mars.2 Release(4.5.2)右击项目> Maven >支持工作区解析,这解决了我的问题。

#16


0  

Typically, you can copy the .classpath file from a working project since there isn’t anything project specific here assuming you’re structured as a standard Maven project.

通常,您可以从一个正在工作的项目中复制.classpath文件,因为这里没有任何特定的项目,假设您的结构是一个标准的Maven项目。

#17


0  

This worked for me in Eclipse Oxygen (4.7.0):

这在Eclipse Oxygen频道(4.7.0)中对我有效:

Right click Project -> Maven -> Select Maven Profiles... then check the Repository Proxy box, press OK.

右键单击项目-> Maven ->选择Maven配置文件…然后勾选存储库代理框,按OK。

#18


0  

None of that solved my problem. but what I did was if click on the pom.xml, there is a tab at the bottom named dependencies. in this tab it is split into 2 section, one called dependencies and one called dependency management. select every thing in the dependency section and click add to be under the dependency management control. close and reopen your project.

这些都不能解决我的问题。但是我做的是点击pom。xml,底部有一个名为dependencies的选项卡。在此选项卡中,它被分为两个部分,一个称为依赖项,一个称为依赖项管理。选择依赖项部分中的每一项并单击add to be under the dependency management control。关闭并重新打开项目。

#1


103  

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

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

#2


59  

if you execute

如果你执行

mvn eclipse:clean

followed by

紧随其后的是

mvn eclipse:eclipse

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

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

#3


28  

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-click the project in the Package Explorer view 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/java to the source folders on the build path).

    如果你创建了一个“Java”项目在Eclipse(Ctrl + N > Java项目),然后在Package Explorer视图中右键单击项目,去Maven >使依赖关系管理(根据项目的初始结构,你可以修改它与Maven的,例如通过添加src / Java构建路径上的源文件夹)。

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

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

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

    如果您在Eclipse外部创建了一个Maven项目(手动或使用原型),那么只需在Eclipse中导入它(右键单击Package Explorer视图并选择import……> Maven项目),它将是“Maven就绪”。

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

现在,要添加依赖项,可以右键单击项目并选择Maven >添加依赖项),或者手动编辑pom。

PS: avoid using the maven-eclipse-plugin if 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.

注意:如果使用m2eclipse,请避免使用maven-eclipse插件。这是完全没有必要的,它会令人困惑,它会产生一些混乱。不,真的,不要用它,除非你真的知道你在做什么。

#4


14  

Right click on the project Configure > convert to Maven project

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

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

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

#5


10  

Add this to .classpath file ..

将此添加到.classpath文件中。

<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
    <attributes>
        <attribute name="maven.pomderived" value="true"/>
    </attributes>
</classpathentry>

Thx

谢谢

#6


9  

I did like this..

我确实喜欢这个. .

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

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

#7


5  

In Eclipse (Mars) go to:

在Eclipse(火星)中:

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

项目属性- - > Maven。确保“消除来自工作空间项目的依赖关系”被禁用。

#8


4  

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

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

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

#9


2  

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

#10


1  

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命令,然后手动将类路径条目复制/合并到我的项目类路径文件中。

#11


1  

Adding my answers for a couple of reasons:

添加我的答案有几个原因:

  • Somehow none of the answers listed directly resolved my problem.
  • 不知何故,列出的答案中没有一个直接解决了我的问题。
  • I couldn't find "Enable dependency management" under Maven. I'm using Eclipse 4.4.2 build on Wed, 4 Feb 2015.
  • 我在Maven下找不到“启用依赖项管理”。我在2015年2月4日周三使用Eclipse 4.4.2构建。

What helped me was another option under Maven called as "Update Project" and then when I click it this window opens which has a checkbox that says "Force update of Snapshot/Releases". The real purpose of this checkbox is different I know but somehow it resolved the dependencies issue. 如何配置Eclipse构建路径以使用Maven依赖项?

帮助我的是Maven下的另一个名为“更新项目”的选项,当我点击这个窗口时,它会打开一个复选框,上面写着“强制更新快照/发布”。我知道这个复选框的真正目的不同,但是它以某种方式解决了依赖关系问题。

#12


0  

Maybe you could look into maven-eclipse-plugin instead of M2Eclipse.

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

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

在那里你基本上添加了maven-eclipse-plugin配置到你的pom中。然后执行mvn eclipse:eclipse,它将为eclipse生成所需的.project和.classpath文件。然后您将在Eclipse中获得正确的构建路径。

#13


0  

For newer Eclipse versions (>=Mars) right click on project > configure > convert to Maven project

对于更新的Eclipse版本(>=Mars),右键单击项目>,配置>转换为Maven项目

#14


0  

Make sure your POM follows the naming convention, and is named in lowercase lettering as pom.xml and NOT POM.xml.

确保您的POM遵循命名约定,并以小写字母作为POM命名。xml而不是POM.xml。

In my case all was right, but Eclipse still complained when trying to Right-click and Update project configuration - told me that the POM could not be read. Changed the name to lowercase - pom.xml from POM.xml - and it worked.

在我的例子中,一切都是正确的,但是Eclipse仍然抱怨当尝试右键单击并更新项目配置时——告诉我POM无法读取。将名称更改为小写- pom。从POM xml。xml——而且它起作用了。

#15


0  

I met this issue too. When I add dependencies in the pom.xml, I checked in the local folder /Users/xyz/.m2/ and the jars are already downloaded there, but cann't added the the buildpath of the eclipse.

我也遇到过这个问题。当我在pom中添加依赖项时。xml,我检查了本地文件夹/用户/xyz/。m2/ jar已经在那里下载了,但是不能添加eclipse的构建路径。

My eclipse Version: Mars.2 Release (4.5.2)

right click project > Maven > Enable Workspace Resolution

And this solved my issue.

我的eclipse版本:Mars.2 Release(4.5.2)右击项目> Maven >支持工作区解析,这解决了我的问题。

#16


0  

Typically, you can copy the .classpath file from a working project since there isn’t anything project specific here assuming you’re structured as a standard Maven project.

通常,您可以从一个正在工作的项目中复制.classpath文件,因为这里没有任何特定的项目,假设您的结构是一个标准的Maven项目。

#17


0  

This worked for me in Eclipse Oxygen (4.7.0):

这在Eclipse Oxygen频道(4.7.0)中对我有效:

Right click Project -> Maven -> Select Maven Profiles... then check the Repository Proxy box, press OK.

右键单击项目-> Maven ->选择Maven配置文件…然后勾选存储库代理框,按OK。

#18


0  

None of that solved my problem. but what I did was if click on the pom.xml, there is a tab at the bottom named dependencies. in this tab it is split into 2 section, one called dependencies and one called dependency management. select every thing in the dependency section and click add to be under the dependency management control. close and reopen your project.

这些都不能解决我的问题。但是我做的是点击pom。xml,底部有一个名为dependencies的选项卡。在此选项卡中,它被分为两个部分,一个称为依赖项,一个称为依赖项管理。选择依赖项部分中的每一项并单击add to be under the dependency management control。关闭并重新打开项目。