如何正确地将 Maven 项目导入到 Eclipse?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16982325/
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
How properly import maven projects to eclipse?
提问by sphinks
I have complex Maven project, which has a parent folder with parent pom.xml
and some subfolders for subprojects, each with its own pom.xml
.
我有一个复杂的 Maven 项目,它有一个带有父级的父文件夹pom.xml
和一些子项目的子文件夹,每个子文件夹都有自己的pom.xml
.
When I checkout the parent folder from a SVN repository 'As maven projects', it correctly appears in the workspace each as separate project.
当我从 SVN 存储库“作为 maven 项目”检出父文件夹时,它正确地出现在工作区中,每个都作为单独的项目。
But now I made a checkout of the parent folder from other branch of project. I want to add this parent folder in the same manner to the current workspace. So I just select Import > Maven project
, get the same dialogs as due checkout from svn, Eclipse finds all pom files with proper hierarchy and ask me give other name to parent project, cause the same name alredy exists (trunc version of project), I give it other name. But after import I get only parent folder as maven project in eclipse, all other subprojects are simple located under parent project as its subfolders.
但是现在我从项目的其他分支签出了父文件夹。我想以相同的方式将此父文件夹添加到当前工作区。所以我只是选择Import > Maven project
,从 svn 获得与到期结账相同的对话框,Eclipse 找到所有具有适当层次结构的 pom 文件,并要求我为父项目提供其他名称,因为相同的名称已经存在(项目的 trunc 版本),我给它其他姓名。但是在导入后,我在 eclipse 中只得到父文件夹作为 maven 项目,所有其他子项目都简单地位于父项目下作为其子文件夹。
So, how can I import such project properlty? I just want all subprojects created as maven projects too.
那么,如何导入这样的项目属性呢?我也只想将所有子项目创建为 Maven 项目。
回答by Mifeet
Eclipse doesn't allow one project to be imported more than once, in your case from trunk and a branch. This articleshows how you can bypass this limitation with a custom maven profile. Basically, the steps are:
Eclipse 不允许多次导入一个项目,在您的情况下是从主干和分支导入。本文展示了如何使用自定义 Maven 配置文件绕过此限制。基本上,步骤是:
Add the following profile to your parent
pom.xml
<profiles> <!-- Specific profile used to append a string to project name --> <profile> <id>append-to-project-name</id> <activation> <property> <name>append.to.project.name</name> </property> </activation> <build> <plugins> <plugin> <artifactId>maven-eclipse-plugin</artifactId> <configuration> <projectNameTemplate> [artifactId]-${append.to.project.name} </projectNameTemplate> </configuration> </plugin> </plugins> </build> </profile> </profiles>
Before importing the project to Eclipse, let maven generate the project settings:
mvn eclipse:eclipse -Dappend.to.project.name=[your-branch-name]
Import the project to Eclipse as an existing project (not a maven project).
将以下配置文件添加到您的父母
pom.xml
<profiles> <!-- Specific profile used to append a string to project name --> <profile> <id>append-to-project-name</id> <activation> <property> <name>append.to.project.name</name> </property> </activation> <build> <plugins> <plugin> <artifactId>maven-eclipse-plugin</artifactId> <configuration> <projectNameTemplate> [artifactId]-${append.to.project.name} </projectNameTemplate> </configuration> </plugin> </plugins> </build> </profile> </profiles>
在将项目导入 Eclipse 之前,让 maven 生成项目设置:
mvn eclipse:eclipse -Dappend.to.project.name=[your-branch-name]
将项目作为现有项目(不是 maven 项目)导入 Eclipse。
This should solve the naming issue.
这应该可以解决命名问题。
As for import of child projects: I also have a parent maven project with child subprojects and use the m2eplugin for Ecplise. When I select Import > Existing Maven Project
, I can check both the parent and children. After import, I have both the parent project and each child imported independently. Screens:
至于子项目的进口:我也有孩子子项目父母Maven项目并使用M2E插件Ecplise。当我选择时Import > Existing Maven Project
,我可以检查父母和孩子。导入后,我将父项目和每个子项目都独立导入。屏幕:
So I hope this combined with the naming solution above should solve your problem.
所以我希望这与上面的命名解决方案相结合应该可以解决您的问题。
回答by Kris
For importing complex (or indeed any) Maven projects from an SCM (be it SVN, Git or any other SCM) I use the following approach:
为了从 SCM(无论是 SVN、Git 还是任何其他 SCM)导入复杂(或任何)Maven 项目,我使用以下方法:
File->Import->Check out Maven Projects from SCM
File->Import->Check out Maven Projects from SCM
This works best if no IDE specific files have been committed to the SCM. It will import all nested projects or you can choose which projects to import. It relies entirely on the Maven settings for determining the nature of the project being created so that, assuming the POM files are correct, you wind up with everything configured just right.
如果没有将特定于 IDE 的文件提交给 SCM,则此方法效果最佳。它将导入所有嵌套项目,或者您可以选择要导入的项目。它完全依赖于 Maven 设置来确定正在创建的项目的性质,因此,假设 POM 文件是正确的,您最终会正确配置所有内容。
You may need to install add-ons so that m2eclipse can access the SCM of your choice.
您可能需要安装附加组件,以便 m2eclipse 可以访问您选择的 SCM。
回答by thSoft
Delete the projects that you had checked out as Maven projects, then check out the parent project simply (not as Maven project), then Import the child modules as Existing Maven projects. Make sure they are listed as child modules in the parent POM.
删除您作为 Maven 项目检出的项目,然后简单地检出父项目(而不是作为 Maven 项目),然后将子模块作为现有 Maven 项目导入。确保它们在父 POM 中被列为子模块。
回答by roger_that
I guess there might be some workspace issue. You might have checked out the project in workspace itself(not sure so pardon for that). What you can do is, try checking out your project in some other drive and then import the parent project. You will again see all POMs aligned in hierarchical way and then i suppose, you won't be asked for entering name for parent project and this will do the trick.
我想可能存在一些工作区问题。您可能已经检查了工作区本身中的项目(不确定所以请原谅)。您可以做的是,尝试在其他驱动器中检出您的项目,然后导入父项目。您将再次看到所有 POM 以分层方式对齐,然后我想,您将不会被要求输入父项目的名称,这将起作用。