Convert Existing Eclipse Project to Maven Project - java

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.

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.

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.

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

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.

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

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!

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

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.

For converting to Gradle is analogue to Maven:
Right click on Project -> submenu Configure -> Convert to Gradle (STS) Project

Related

Eclipse: reference local code instead of cached gradle dependency

I have two gradle java projects imported into Eclipse, one being a dependency of the other.
I would like for Eclipse to use the local dependency code, instead of the compiled dependency in the gradle cache folders, so I can modify and debug both projects simultaneously.
How do I force Eclipse to use a local dependency code?
The feature you are referring to is known as Composite Builds:
Importing into the IDE
One of the most useful features of composite builds is IDE integration. By applying the idea or eclipse plugin to your build, it is possible to generate a single IDEA or Eclipse project that permits all builds in the composite to be developed together.
In addition to these Gradle plugins, recent versions of IntelliJ IDEA and Eclipse Buildship support direct import of a composite build.
Importing a composite build permits sources from separate Gradle builds to be easily developed together. For every included build, each sub-project is included as an IDEA Module or Eclipse Project. Source dependencies are configured, providing cross-build navigation and refactoring.
The most simple way of achieving this is to use includeBuild in your settings.gradle.
rootProject.name = 'my-composite'
includeBuild 'my-app'
includeBuild 'my-utils'
With that in place, there's no need to configure the build path manually in Eclipse.
Gosh this was a trivial one and it took me way too long to figure it out.
Solved the issue by creating a file named external-projects.properties in the root folder of the parent project with the relative or full path to the dependency project folder.
To be precise, for me the folders tree ends up like this:
git-folder/
|__parent-project/
|__external-projects.properties
|__build.gradle.kts
|__rest (src/main, etc)
|__dependency-project/
|__build.gradle.kts
|__rest (src/main, etc)
And the contents of the external-projects.properties file:
dependency-project = ../dependency-project
The dependency project must also be in the build path of the parent project (done by "Right-Click project > Build Path > Configure Build Path" and adding it in the "Projects" tab).
After that, a "Right Click > Gradle > Refresh Gradle Project" did the trick.

Missing Java Build Path for Maven project

So I recently imported a project from Github into Eclipse and converted it to a Maven Project. However, when I try to add Libraries, the "Java Build Path" menu is missing from Properties. What's the issue and how can I fix it?
Generally you don't have to manipulate your build path entry manually when your project has the Maven nature, because that is already whay Maven is intended to do: "Manage your project dependencies" which means grabbing the needed versions, saving them into your local repository then use those downloaded artifacts in your project classpath when you run one of the mvn goals (depending on the dependencies scopes).

How to force Eclipse to see java project as java project?

I have downloaded a 3rd party project, which consists of multiple files, including Java ones.
General structure is as follows:
<topfolder>
pom.xml
<subfolder1>
pom.xml
src
main
java
<normalclasspath>
resources
site
apt
index.apt
test
java
<normalclasspath>
Eclipse imports this project normally, but is unable to index its Java content. For example, I can't browse from a variable to its definition, and so on.
Also I can't set my own Build Path since it says No action available.
Of course, I can refactor folder structure myself to suite eclipse needs, but are there any automation means for this?
UPDATE
Yes, this is a Maven project and Eclipse already knows that it has Maven nature. The only option now is to disable it
But this is not a question. The question is how to add JAVA NATURE, so that Eclipse knows consistence of classpath and be able to navigate to class definitions and so on.
UPDATE 2
#75inchpianist's answer about facets helped partially. It was not available to select Java facet immediately, but required turning on facets at all first. Then Java facet was already there.
Now I see, that Eclipse interprets Java, but Maven interpretation is not full. Namely, no Maven dependencies interpreted (no Maven Dependencies node in Package Explorer).
The attempt to add it fails:
Right click on the project and select "Properties"
Within "Project Facets" make sure that Java is selected!
As this is a maven project (because of the pom.xml) you need to install a maven plugin for eclipse.
Next you have to right click on the project and choose "Configure->Convert to Maven Project".
Now you should have your normal java structure.
Assuming that Eclipse recognizes the project as Java project yu can do the following:
If you don't want to install a maven plugin into Eclipse you can make Eclipse recognize the source correctly by adding every src/main/java folder as Source-Folder to your Build-Path.
This is better than modifying the folder structure as you can update the sources without problem later.
Add following to your .project file and refresh the project.
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>

Adding POM Dependency in Eclipse build path

I have a project with a number of POM files. I have done a clean install and eclipse:eclipse. I would like to import the project as a normal project rather than Maven project. When I imported the project as normal project , I was not able to see the Maven dependencies present in POM in eclipse's build path. Is importing the project as Maven project only way of doing it? When I import the project as Maven project I see a number projects in eclipse workspace (one for each POM). However I would like to view this as a single project in eclipse workspace.
UPDATE: The classpath files of the child projects contain the dependent jar files , however it is not added to build path when the main project is added. Only the entries in classpath file of the main project is being added.
Given that you refer to "Maven project", are you using an eclipse plugin for Maven like M2Eclipse?
Assuming you are using M2Eclipse, the Maven Dependencies Library is automatically enabled for a Maven project. For a normal Java project, importing the library would force you to configure the project as a Maven project (On the context menu for the project, select Configure -> Convert to Maven Project).
Having said that, while you import your Maven project, you will notice an "Advanced" option (towards the lower section of the "Import Maven Projects" dialog). When you expand this option, there will be a checkbox to "Resolve Workspace Projects" - this is checked by default and is hence responsible for importing the child modules as separate projects. If you uncheck this, it will import only the parent module as the project and all children modules will be in sb folders under the parent module.
IMO, resolving the projects provides greater clarity, but then again this is very much a personal choice.
Hope that helps.
Eclipse doesn't support nested projects, and Maven kinda forces you to. So you can't really push everything into one project.
Doing a cursory google search led me to this, which might be helpful: http://warpedjavaguy.wordpress.com/2011/08/08/how-i-defeated-the-maven-release-plugin-in-a-flat-structured-multi-module-project/
You might also check out the following two online books from Sonatype:
Maven: The Complete Reference and Developing with Eclipse and Maven
Personally, I create Working Sets for each multi-module project and add all the individual projects to it. This enables me to see them all grouped together, this might be to your liking.
http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.platform.doc.user%2Fconcepts%2Fcworkset.htm
Once you have created the working set you can enable/disable them in the menu in Project Explorer and configure the project explorer to display the working set as the root element in the tree rather than the project.

In eclipse, maven dependencies are overriding project dependencies

I have a "master project" that uses several "modules". The modules are in the pom.xml, but I also have the code for the modules as projects in Eclipse. I defined the modules as project dependencies both in "Properties|Project References" and in "Properties|Java Build Path". I also made sure all of the Eclipse projects are at the top of "Order and Export" in Java Build Path.
The master project is not recognizing changes to the modules within eclipse. When I press F3 to see the code, it opens java source from my .m2 directory. It's obviously ignoring my Eclipse projects. Am I missing some setting? What should I do?
You don't need to add the dependent projects into the Project references (at least when using m2eclipse-plugin), just add them as dependencies on your masters' pom.xml. I haven't had the need to touch the Build path since I've started using Maven in Eclipse.
Make sure you have enabled the Workspace resolution of the plugin by right clicking on project root -> Maven -> Enable Workspace Resolution. If it says "Disable Workspace Resolution", it is already enabled. You may also need to run "Update project configuration" under the same menu.
This way when you run your master-project from within Eclipse, any changes made to the other projects will be "visible" to the master (and you can actually use hot swap during runtime even on the other projects).
If you're using Maven, you pretty much want to commit to Maven. Even if you do configure Eclipse with Eclipse-specific dependencies, anytime you update your project configuration using Maven, it will do a complete rewrite of your configuration.
I strongly recommend converting any of your other project build dependencies into Maven-enabled projects that can be used as Maven dependencies.
In your specific case (through using the Maven modules), this means ensuring that each of your modules are imported into Eclipse as m2e-enabled projects. m2e will then automatically wire in the dependencies between modules and other dependent projects.
Im scratching my head as to why you would need your modules defined as dependencies on your top level project. Are you trying to avoid having to define the (cross) dependencies at a module level? In any case, as per other suggestions, first of all put your dependencies in the correct places in your POM, then either run
mvn eclipse:clean eclipse:eclipse
to update your Eclipse settings, or better still, make sure m2eclipse is installed and import your maven projects directly into the IDE.

Categories