ChatGPT解决这个技术问题 Extra ChatGPT

Convert Existing Eclipse Project to Maven Project

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.

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.

Does your existing build use an Ant script?
You should probably switch accepted answer

R
Ripon Al Wasim

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:

Right click the Java project to pop up the context menu

Select Configure > Convert to Maven Project

Here is the detailed steps with screen shots.


This still does not create the directory structures and add dependencies in pom.
does m2e still not create the maven directory structure and add dependencies in pom ?
My solution (@ShriKant Vashishtha) mentioned below solves all problems described here. Creates Maven structure, removes spaces in folder names, creates pom.xml and moves source folders in Maven structure
Works perfect and flawless on Windows 10 Eclipse Mars at the time.
It Works perfect to me
B
BeeOnRope

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.

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

Newer M2E versions

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

Older M2E versions

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

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.


Although it enables M2Eclipse, it's not really a wizard not does it import an existing Eclipse project. Rather, it gives the user an option of selecting dependencies, etc., which may be difficult if the user doesn't have much experience with Maven.
I interpreted the question as 'do I need to create a new project and manually copy stuff around to use the m2eclipse plugin?', I'll clarify.
The maven menu item is not available for me from the right click menu (latest m2eclipse), chengdong's answer works.
On the current version of Eclipse (4.2) and m2eclipse the menu item is Convert / Convert to Maven Project.
@Jesper menu item is "Configure / Convert to Maven Project"
S
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-maven on github. As eclipse doesn't have all necessary information about the dependencies, it does the following:

Based on 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.

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.


Interesting ! Is it ported to Luna ? Would the project still be an eclipse project (valid .classpath and .project files) ? Could you add to your answer how to use your plugin to mavenize a simple JavaSE project - and what would be the actual changes in directory structure ?
Sorry but I don't know what Luna is. It will not be valid eclipse project anymore as project directory structure gets changed. However creating modified eclipse configurations using Maven is easy. This is not a plugin but a tool. How to run it is mentioned already in README of project. The directory structure gets converted in Maven convention. So all sources go to src/main/java for instance.
Luna is the latest version of eclipse !!
M
Mr_and_Mrs_D

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.

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.


Will this create the maven folder structure ?
yes, It will create the maven folder structure based on appropriate mapped archetype template and minimum configuration. You got to add each of your project dependencies(from .classpath) into POM.xml yourself manually one at a time.
F
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 .

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.

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

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.

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


m
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.

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

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

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

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


A
Alex

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

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.

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


This looks like a very attractive option. But, as it is currently packaged, it is not very accessible. It is not apparent how to package and deploy the source.code. Most Java developers are not Scala developers, nor is SBT a widely used tool within the community.
C
Community

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?

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.

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

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.


Why is this answer higher than the +199 answer? I almost didn't see that one
a
another

For converting to Gradle is analogue to Maven:

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


The answer is not relevant to the question
I found this question by looking for Gradle, many times it is helpful to add answers for other O.S. rather than the asked O.S. because it does not exist a question for individual case. Regarding Gradle, I would not create a new question to answer what I consider helpful, rather I post this. Thanks & Regards.