Java 将现有的 Eclipse 项目转换为 Maven 项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2449461/
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 07:50:54  来源:igfitidea点击:

Convert Existing Eclipse Project to Maven Project

javaeclipsemaven-2

提问by Tom G

For a project at work, we're considering using the Maven plugin for Eclipse to automate our builds. Right now the procedure is far more complicated than it ought to be, and we're hoping that Maven will simplify things to a one-click build.

对于工作中的项目,我们正在考虑使用 Eclipse 的 Maven 插件来自动化我们的构建。现在这个过程比它应该的复杂得多,我们希望 Maven 将事情简化为一键构建。

My question is, is there a wizard or automatic importer for converting an existing Eclipse Java project to a Maven project, using the Maven plugin? Or should I create a new Maven project and manually copy over all source files, libs, etc.

我的问题是,是否有使用 Maven 插件将现有 Eclipse Java 项目转换为 Maven 项目的向导或自动导入程序?或者我应该创建一个新的 Maven 项目并手动复制所有源文件、库等。

采纳答案by brabster

If you just want to create a default POM and enable m2eclipse features: so I'm assuming you do not currently have an alternative automated build setup you're trying to import, and I'm assuming you're talking about the m2eclipse plugin.

如果您只想创建默认 POM 并启用 m2eclipse 功能:那么我假设您目前没有要导入的替代自动构建设置,并且我假设您正在谈论 m2eclipse 插件。

The m2eclipse plugin provides a right-click option on a project to add this default pom.xml:

m2eclipse 插件在项目上提供了一个右键单击选项来添加这个默认的 pom.xml:

Newer M2E versions

较新的 M2E 版本

Right click on Project -> submenu Configure -> Convert to Maven Project

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

Older M2E versions

较旧的 M2E 版本

Right click on Project -> submenu Maven -> Enable Dependency Management.

右键单击项目 -> 子菜单 Maven -> 启用依赖项管理。

That'll do the necessary to enable the plugin for that project.

这将完成为该项目启用插件所需的工作。



To answer 'is there an automatic importer or wizard?': not that I know of. Using the option above will allow you to enable the m2eclipse plugin for your existing project avoiding the manual copying. You will still need to actually set up the dependencies and other stuff you need to build yourself.

回答'是否有自动导入程序或向导?': 从来没听说过。使用上面的选项将允许您为现有项目启用 m2eclipse 插件,避免手动复制。您仍然需要实际设置依赖项和您自己构建所需的其他内容。

回答by Pascal Thivent

My question is, is there a wizard or automatic importer for converting an existing Eclipse Java project to a Maven project, using the Maven plugin?

我的问题是,是否有使用 Maven 插件将现有 Eclipse Java 项目转换为 Maven 项目的向导或自动导入程序?

As far as I know, there is nothing that will automagically convert an Eclipse project into a Maven project (i.e. modify the layout, create a POM, "generate" and feed it with metadata, detect libraries and their versions to add them to the POM, etc). Eclipse just doesn't have enough metadata to make this possible (this is precisely the point of the POM) and/or to produce a decent result.

据我所知,没有什么可以自动将 Eclipse 项目转换为 Maven 项目(即修改布局、创建 POM、“生成”并为其提供元数据、检测库及其版本以将它们添加到 POM , 等等)。Eclipse 只是没有足够的元数据来实现这一点(这正是 POM 的重点)和/或产生不错的结果。

Or should I create a new Maven project and manually copy over all source files, libs, etc

或者我应该创建一个新的 Maven 项目并手动复制所有源文件、库等

That would be the best option in my opinion. Create a Maven project, copy/move sources, resources, tests, test resources into their respective directories, declaredependencies, etc.

在我看来,那将是最好的选择。创建一个Maven项目,将源、资源、测试、测试资源复制/移动到各自的目录中,声明依赖等。

回答by chengdong

Start from m2e 0.13.0 (if not earlier than), you can convert a Java project to Maven project from the context menu. Here is how:

从 m2e 0.13.0(如果不早于)开始,您可以从上下文菜单中将 Java 项目转换为 Maven 项目。方法如下:

  • Right click the Java project to pop up the context menu
  • Select Configure > Convert to Maven Project
  • 右键单击 Java 项目,弹出上下文菜单
  • 选择配置 > 转换为 Maven 项目

Here is the detailed stepswith screen shots.

这是带有屏幕截图的详细步骤

回答by ShriKant Vashishtha

I was having the same issue and wanted to Mavenise entire eclipse workspace containing around 60 Eclipse projects. Doing so manually required a lot of time and alternate options were not that viable. To solve the issue I finally created a project called eclipse-to-mavenon github. As eclipse doesn't have all necessary information about the dependencies, it does the following:

我遇到了同样的问题,想对包含大约 60 个 Eclipse 项目的整个 eclipse 工作区进行 Mavenise。手动执行此操作需要大量时间,并且替代选项不太可行。为了解决这个问题,我最终在 github 上创建了一个名为eclipse-to-maven的项目。由于 eclipse 没有关于依赖项的所有必要信息,它执行以下操作:

  • Based on <classpathentry/>XML elements in .classpath file, it creates the dependencies on another project, identifies the library jar file and based on its name (for instance jakarta-oro-2.0.8.jar) identifies its version. Currently artifactIdand groupIdare same as I couldn't find something which could return me the Maven groupId of the dependency based on artifactId. Though this is not a perfect solution it provides a good ground to speed up Mavenisation.

  • It moves all source folders according to Maven convention (like src/main/java)

  • As Eclipse projects having names with spaces are difficult to deal on Linux/Unix environment, it renames them as well with names without spaces.

  • Resultant pom.xml files contain the dependencies and basic pom structure. You have to add required Maven plugins manually.

  • 基于<classpathentry/>.classpath 文件中的 XML 元素,它创建对另一个项目的依赖关系,标识库 jar 文件并根据其名称(例如 jakarta-oro-2.0.8.jar)标识其版本。目前artifactIdgroupId我找不到可以返回我基于artifactId. 虽然这不是一个完美的解决方案,但它为加速 Mavenisation 提供了良好的基础。

  • 它根据 Maven 约定移动所有源文件夹(如src/main/java

  • 由于 Eclipse 项目的名称带空格很难在 Linux/Unix 环境中处理,因此它也将它们重命名为不带空格的名称。

  • 结果 pom.xml 文件包含依赖项和基本 pom 结构。您必须手动添加所需的 Maven 插件。

回答by Fred Bricon

Chengdong's answer is correct, you should use Configure>Convert to Maven Project. However, I must add the conversion process has been greatly improved since m2e 0.13.0 : m2e 1.1+ and m2e-wtp 0.16.0+ can now convert the existing eclipse settings into maven plugin configuration.

承东的回答是正确的,你应该使用Configure>Convert to Maven Project. 但是,我必须补充,自 m2e 0.13.0 以来,转换过程得到了很大改进:m2e 1.1+ 和 m2e-wtp 0.16.0+ 现在可以将现有的 eclipse 设置转换为 maven 插件配置

As for the dependency conversion matter, you can try the JBoss Tools (JBT) 4.0 Maven integration feature, which contains an experimental conversion wizard, plugged into m2e's conversion process : http://docs.jboss.org/tools/whatsnew/maven/maven-news-4.0.0.Beta1.html.

至于依赖转换的问题,可以试试JBoss Tools (JBT) 4.0 Maven 集成特性,里面有一个实验性的转换向导,插入到m2e的转换过程中:http: //docs.jboss.org/tools/whatsnew/maven/ maven-news-4.0.0.Beta1.html

It does not pretend to be the ultimate solution (nothing can), be it should greatly help bootstrap your Maven conversion process.

它不会假装是最终的解决方案(没有什么可以),它应该极大地帮助引导您的 Maven 转换过程。

Also, FYI, here are some ideas to enhance m2e's conversion process, refactoring to use a Maven layout will most probably be implemented in the future.

另外,仅供参考,这里有一些增强 m2e 转换过程的想法,重构使用 Maven 布局很可能会在未来实现。

JBT 4.0 (requires Eclipse JavaEE Juno) can be installed from http://download.jboss.org/jbosstools/updates/stable/juno/or from the Eclipse Marketplace

JBT 4.0(需要 Eclipse JavaEE Juno)可以从http://download.jboss.org/jbosstools/updates/stable/juno/或从 Eclipse Marketplace 安装

回答by Omar Faroque Anik

Right click on the Project name > Configure > Convert to Maven Project > click finish. Here you will add some dependencies to download and add your expected jar file.

右键单击项目名称 > 配置 > 转换为 Maven 项目 > 单击完成。在这里,您将添加一些要下载的依赖项并添加您期望的 jar 文件。

This will create an auto-generated pom.xml file. Open that file in xml format in your eclipse editor. After build tag (</build>) add your dependencies which you can copy from maven website and add them there. Now you are good to go. These dependencies will automatically add your required jar files.

这将创建一个自动生成的 pom.xml 文件。在 Eclipse 编辑器中以 xml 格式打开该文件。在构建标签 ( </build>) 之后添加您可以从 Maven 网站复制的依赖项并将它们添加到那里。现在你可以走了。这些依赖项将自动添加您所需的 jar 文件。

回答by moonshang

It's necessary because, more or less, when we import a project from git, it's not a maven project, so the maven dependencies are not in the build path.

这是必要的,因为,或多或少,当我们从 git 导入项目时,它不是 maven 项目,因此 maven 依赖项不在构建路径中。

Here's what I have done to turn a general project to a maven project.

这是我将一般项目转变为 Maven 项目所做的工作。

general project-->java project right click the project, properties->project facets, click "java". This step will turn a general project into java project.

一般项目-->java项目右键项目,属性->项目facets,点击“java”。这一步会将一个普通项目变成java项目。

java project --> maven project right click project, configure-->convert to maven project At this moment, maven dependencies lib are still not in the build path. project properties, build path, add library, add maven dependencies lib

java project --> maven project 右键project,configure-->convert to maven project 此时maven依赖lib还没有在build路径中。项目属性,构建路径,添加库,添加maven依赖lib

And wait a few seconds, when the dependencies are loaded, the project is ready!

并等待几秒钟,当依赖项加载完毕后,项目就准备好了!

回答by Alex

There is a command line program to convert any Java project into a SBT/Maven project.

有一个命令行程序可以将任何 Java 项目转换为 SBT/Maven 项目。

It resolves all jars and tries to figure out the correct version based on SHA checksum, classpath or filename. Then it tries to compile the sources until it finds a working configuration. Custom tasks to execute per dependency configuration can be given too.

它解析所有 jars 并尝试根据 SHA 校验和、类路径或文件名找出正确的版本。然后它会尝试编译源代码,直到找到工作配置。也可以给出每个依赖配置执行的自定义任务。

UniversalResolver 1.0
Usage: UniversalResolver [options]

  -s <srcpath1>,<srcpath2>... | --srcPaths <srcpath1>,<srcpath2>...
        required src paths to include
  -j <jar1>,<jar2>... | --jars <jar1>,<jar2>...
        required jars/jar paths to include
  -t /path/To/Dir | --testDirectory /path/To/Dir
        required directory where test configurations will be stored
  -a <task1>,<task2>... | --sbt-tasks <task1>,<task2>...
        SBT Tasks to be executed. i.e. compile
  -d /path/To/dependencyFile.json | --dependencyFile /path/To/dependencyFile.json
        optional file where the dependency buffer will be stored
  -l | --search
        load and search dependencies from remote repositories
  -g | --generateConfigurations
        generate dependency configurations
  -c <value> | --findByNameCount <value>
        number of dependencies to resolve by class name per jar

https://bitbucket.org/mnyx/universalresolver

https://bitbucket.org/mnyx/universalresolver

回答by another

For converting to Gradle is analogue to Maven:

转换为 Gradle 类似于 Maven:

Right click on Project -> submenu Configure -> Convert to Gradle (STS) Project

右键单击项目 -> 子菜单配置 -> 转换为 Gradle (STS) 项目