1. Overview
1.概述
In this tutorial, we’ll see how to import an existing Maven project into Eclipse. For this purpose, we can either use the Eclipse plugin for Maven or the Apache Maven Eclipse plugin.
在本教程中,我们将看到如何将一个现有的Maven项目导入Eclipse。为此,我们可以使用Maven的Eclipse插件或Apache Maven Eclipse插件。
2. Eclipse and Maven Project Setup
2.Eclipse和Maven项目设置
For our example, we will be using the latest version of Eclipse, version 2021-09 (4.21.0), which we obtained from the Eclipse Downloads page.
在我们的例子中,我们将使用最新的Eclipse版本,即2021-09(4.21.0),我们从Eclipse下载页面获得该版本。
2.1. Example Maven Project
2.1.Maven项目实例
For our examples, we’ll use a multi-module Maven project from our GitHub repository. Once we’ve cloned the repository or downloaded the project, our multi-module Maven project’s directory root should look like:
在我们的例子中,我们将使用来自GitHub 仓库的多模块Maven项目。一旦我们克隆了资源库或下载了项目,我们的多模块Maven项目的目录根应该是这样的。
|--multimodulemavenproject
|--daomodule
|--entitymodule
|--mainappmodule
|--userdaomodule
|--pom.xml
|--README.md
2.2. Minor Changes in the Maven Project
2.2.Maven项目的小改动
Our multi-module Maven project is itself a child project. Therefore, to limit the scope of our exercise, we’ll need to make some minor changes in the pom.xml in the multimodulemavenproject directory, which will be our project root. Here, let’s remove the lines that refer to the parent of multimodulemavenproject:
我们的多模块Maven项目本身就是一个子项目。因此,为了限制我们的练习范围,我们需要对multimodulemavenproject目录下的pom.xml做一些小改动,该目录将是我们的项目根。在这里,我们要删除提及multimodulemavenproject的父级的行。
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../</relativePath>
</parent>
With these lines gone, we’re ready to import our Maven project into Eclipse.
完成这几行后,我们就可以把Maven项目导入Eclipse了。
3. Import Using the m2e Eclipse Plugin for Maven
3.使用Maven的m2e Eclipse插件导入
Let’s import the Maven project into Eclipse using the menu path File::Import::Maven::Existing Maven Projects. We can start by clicking on the Import option under the File menu:
让我们使用菜单路径File::Import::Maven::Existing Maven Projects将Maven项目导入Eclipse。我们可以先点击File菜单下的Import选项。
Then, let’s expand the Maven folder, select Existing Maven Projects, and click the Next button:
然后,让我们展开Maven文件夹,选择现有Maven项目,并点击Next按钮。
Finally, let’s provide the path of the root directory of our Maven project and click the Finish button:
最后,让我们提供Maven项目根目录的路径,并点击Finish按钮。
After this step, we should be able to see the Package Explorer view in Eclipse:
在这一步之后,我们应该能在Eclipse中看到Package Explorer视图。
This view might be a bit confusing because we see all the modules separately and not in a hierarchical manner. This is due to the default view in Eclipse, Package Explorer. However, we can easily switch our view to Project Explorer and view the multi-module project in a tree-like structure:
这个视图可能有点令人困惑,因为我们是单独看到所有的模块,而不是以分层的方式。这是由于 Eclipse 中的默认视图,Package Explorer。不过,我们可以轻松地将视图切换到Project Explorer,以树状结构查看多模块项目。
This smooth import of the Maven project was made possible by the Eclipse plugin for Maven, m2e. We didn’t have to add it separately to our Eclipse because it came built-in with the Eclipse installation and can be viewed through the path Help::About Eclipse IDE::Installation Details::Installed Software:
Maven项目的顺利导入得益于Maven的Eclipse插件m2e。我们不必在Eclipse中单独添加它,因为它是内置的与Eclipse安装在一起,可以通过路径Help::About Eclipse IDE::Installed Software查看。
If we have an older version of Eclipse that doesn’t have a built-in m2e plugin, we can always add this plugin using Eclipse Marketplace.
如果我们有一个旧版本的Eclipse,没有内置的m2e插件,我们可以随时使用Eclipse Marketplace添加这个插件。
4. The Apache Maven Eclipse Plugin
4.Apache Maven的Eclipse插件
The Apache Maven Eclipse Plugin can also be used to generate Eclipse IDE files (*.classpath, *.project, *.wtpmodules, and the .settings folder) for use with a project. However, this plugin is now retired by Maven, and the use of Eclipse’s m2e plugin is recommended. More details can be found on the Apache Maven plugin page.
Apache Maven Eclipse插件也可用于生成Eclipse IDE文件(*.classpath、*.project、*.wtpmodules和.settings文件夹),供项目使用。然而,这个插件现已被Maven淘汰,建议使用Eclipse的m2e插件。更多细节可在Apache Maven插件页面找到。
5. Conclusion
5.总结
In this tutorial, we learned about the two ways of importing an existing Maven project into Eclipse. Since the Apache Maven Eclipse Plugin is now retired, we should be using the Eclipse plugin for Maven, m2e, which comes inbuilt with the latest versions of Eclipse.
在本教程中,我们了解了将现有Maven项目导入Eclipse的两种方法。由于Apache Maven Eclipse插件现已退役,我们应该使用Maven的Eclipse插件,m2e,它内置在最新版本的Eclipse中。
All the code samples shown in this tutorial are available over on GitHub.
本教程中显示的所有代码样本都可以在GitHub上找到。