Convert Existing Eclipse Project to Maven Project

Convert Existing Eclipse Project to Maven Project

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.

Convert existing project to a maven project

Ok.. I did eventually shift the project to maven. Here's what I did.

  1. Download the m2e eclipse plugin. It is your best friend!
  2. Create a skeletal maven project. src/main/java is where your code should reside. Move your code into the src/main/java folder. You can continue to use your existing package structure. No need to change that. Even code change isn't required since maven sets src/main/java as the source folder, so you don't need to change the package declarations.
  3. Move your unit tests to src/test/java. Again, no need to change package declarations.
  4. DO NOT copy your libraries & jars.
  5. Check all the compilation errors caused because of the missing jars.
  6. Add dependency of each missing jar to pom.xml. You can get the dependency from the maven repository websites. If you have already indexed the repositories in m2e plugin, then you can simply search of the dependency within the plugin and add it from right there.

In case you have a multi module project, then convert the other modules to maven as described above and add the dependent module to pom.xml of the other module.

If you have custom jars which aren't maven based, you can add them in the src/main/resources folder and then add the jars to build path.

There you go! Easy peasy!

I have a skeletal project setup for Spring + Maven + EC2. You can use that for creating, configuring maven and automating deployment process.

https://github.com/varunachar/maven-release

Converting an existing Java project to Maven in vscode

Here are some steps:

  1. Tell VS Code to use Maven.
  2. Add a pom.xml to your project to spell out the dependencies.
  3. Ask Maven to download the dependencies to your local .m2 repository.
  4. Use the Maven lifecycle to compile/test/package your code.

You'll have no problem as long as all the JARs you need are in Maven Central. If some of them were internal to your organization you'll have to find them and pull them from a private repo or add them to your local .m2.

If this is an old Java project you might want to think about updating. Check the JARs for security vulnerabilities and upgrade them if you can. Hopefully there will be a good Junit suite to prove that upgrades didn't break your code.

One last suggestion: Try to compile and run it on a LTS version of the JDK - 11 or 17. Don't continue with JDK 8 - it's past the end of its support life.

This is easy in IntelliJ. I'd prefer it to VS Code.



Related Topics



Leave a reply



Submit