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

时间:2023-01-17 12:57:18

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项目并手工复制所有源文件、libs等等。

9 个解决方案

#1


130  

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

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

Older M2E versions

旧的M2E的版本

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

右键单击Project ->子菜单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插件,避免手工复制。您仍然需要实际设置依赖项和其他您需要构建自己的东西。

#2


283  

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开始(如果不早于m2e),可以从上下文菜单将Java项目转换为Maven项目。这里是:

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

Here is the detailed steps with screen shots.

以下是屏幕截图的详细步骤。

#3


11  

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-maven on github. As eclipse doesn't have all necessary information about the dependencies, it does the following:

我也遇到了同样的问题,希望对包含大约60个eclipse项目的整个eclipse工作区进行裁剪。手工操作需要大量的时间,而且备选方案也不是那么可行。为了解决这个问题,我最终在github上创建了一个名为eclipseto -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 artifactId and groupId are 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.

    基于.classpath文件中的 XML元素,它在另一个项目上创建依赖项,标识库jar文件,并基于其名称(例如jakarta-oro-2.0.8.jar)标识其版本。目前artifactId和groupId都和我找不到什么东西可以根据artifactId返回依赖的Maven groupId一样。虽然这不是一个完美的解决方案,但它提供了一个良好的基础来加速造影术。

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

    它根据Maven约定移动所有源文件夹(如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.

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

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

    合成砰的一声。xml文件包含依赖项和基本的pom结构。您必须手动添加所需的Maven插件。

#4


7  

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项目>单击finish。在这里,您将添加一些依赖项来下载并添加预期的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格式打开该文件。构建标记()之后,添加您可以从maven网站复制的依赖项,并在其中添加它们。现在你可以走了。这些依赖项将自动添加所需的jar文件。

#5


4  

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 .

程东的答案是正确的,您应该使用配置>转换为Maven项目。但是,我必须补充的是,自从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工具(JBT) 4.0 Maven集成特性(其中包含一个实验性的转换向导)插入到m2e的转换过程中:http://docs.jboss.org/tools/whatsnew/maven-news-4.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市场安装

#6


4  

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项目右键点击项目,属性——>项目facet,点击“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项目—> maven项目右击项目、configure—>转换为maven项目此时,maven依赖项库仍不在构建路径中。项目属性,构建路径,添加库,添加maven依赖*

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

再等几秒钟,当加载依赖项时,项目就准备好了!

#7


1  

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项目,并手动复制所有源文件、libs等。

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

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

#8


1  

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.

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

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

#9


0  

For converting to Gradle is analogue to Maven:

对于转换到Gradle,类似于Maven:

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

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

#1


130  

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

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

Older M2E versions

旧的M2E的版本

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

右键单击Project ->子菜单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插件,避免手工复制。您仍然需要实际设置依赖项和其他您需要构建自己的东西。

#2


283  

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开始(如果不早于m2e),可以从上下文菜单将Java项目转换为Maven项目。这里是:

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

Here is the detailed steps with screen shots.

以下是屏幕截图的详细步骤。

#3


11  

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-maven on github. As eclipse doesn't have all necessary information about the dependencies, it does the following:

我也遇到了同样的问题,希望对包含大约60个eclipse项目的整个eclipse工作区进行裁剪。手工操作需要大量的时间,而且备选方案也不是那么可行。为了解决这个问题,我最终在github上创建了一个名为eclipseto -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 artifactId and groupId are 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.

    基于.classpath文件中的 XML元素,它在另一个项目上创建依赖项,标识库jar文件,并基于其名称(例如jakarta-oro-2.0.8.jar)标识其版本。目前artifactId和groupId都和我找不到什么东西可以根据artifactId返回依赖的Maven groupId一样。虽然这不是一个完美的解决方案,但它提供了一个良好的基础来加速造影术。

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

    它根据Maven约定移动所有源文件夹(如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.

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

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

    合成砰的一声。xml文件包含依赖项和基本的pom结构。您必须手动添加所需的Maven插件。

#4


7  

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项目>单击finish。在这里,您将添加一些依赖项来下载并添加预期的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格式打开该文件。构建标记()之后,添加您可以从maven网站复制的依赖项,并在其中添加它们。现在你可以走了。这些依赖项将自动添加所需的jar文件。

#5


4  

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 .

程东的答案是正确的,您应该使用配置>转换为Maven项目。但是,我必须补充的是,自从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工具(JBT) 4.0 Maven集成特性(其中包含一个实验性的转换向导)插入到m2e的转换过程中:http://docs.jboss.org/tools/whatsnew/maven-news-4.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市场安装

#6


4  

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项目右键点击项目,属性——>项目facet,点击“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项目—> maven项目右击项目、configure—>转换为maven项目此时,maven依赖项库仍不在构建路径中。项目属性,构建路径,添加库,添加maven依赖*

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

再等几秒钟,当加载依赖项时,项目就准备好了!

#7


1  

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项目,并手动复制所有源文件、libs等。

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

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

#8


1  

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.

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

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

#9


0  

For converting to Gradle is analogue to Maven:

对于转换到Gradle,类似于Maven:

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

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