Import Maven multi-module project into Eclipse - java

How do I properly import a Maven multi-module project into Eclipse? When I import a new Maven project and select the parent pom, I can see the submodules under the parent pom which are visible in the package explorer and project explorer. However, they are presented as a deep directory structure, as if Eclipse doesn't know they are Java projects.
When I expand the submodule project, I don't see the Java package shortcuts as I would have if I opened a single Maven project. I only see it as a deep directory structure. How do I import a multimodule Maven project and open the submodule project to take advantage of Eclipse Java project features?

Install M2E Plugin from Market if it is not already installed
From Eclipse IDE choose "File" and "Import" from toolbar. That should
lead you to a pop up.
Type "Existing Maven Project", or navigate to this under
Maven settings.
Then select the main pom project from the import menu, and be sure
to select entire tree of projects under it.
Before approaching this clean the IDE from the currently wrong imported project, deleting its Maven root pom project and every submodule, but leave them on the disk obviously.
I don't recommend the "Convert" options as sometimes that doesn't lead to clean results, especially with big and messy projects.

First, import the parent project. Then, right click on it at the package/project explorer, go to Configure and then click the Configure and Detected Nested Projects..., on the window that will appear click Finish and you're gold.
Also worth mentioning, Package Explorer won't show it nested, use Project Explorer instead.
Have a nice day.

For everyone who still have the same issue, follow these steps:
Open Import Existing Maven Project wizard
Select the Parent POM.xml only and click Finish
Foreach child project, Right Click on the project folder, then click on Import and select his POM.xml file
Hope it helps ! Good Luck !

If you are using git or any version control. Clone the repository first in local.
Steps already explain by #Black Jack. Adding image to support

Step 1
Navigate to project folder.
Open command prompt (or shell)
Run mvn eclipse:eclipse
Step 2
Open eclipse IDE
Open project from File > Open Projects From File System...
Select folder of project containing parent pom.xml file
Step 3
Run Project > Clean from Menu
Step 4
Run File> Restrat From Menu
Solved my problem in Eclipse 2019-06(4.12.0) and 2018-12(4.10.0)
Important trick
When you start to search for files in 'open resource' (for example using Ctrl + Shift + R) eclipse will show file from two address. for example MySampleFile will be available under:
parent-module/sub-module/src/main/java/.../MySampleFile and
sub-module/src/main/java/.../MySampleFile
Use second one. Opening file with first one will not activate IDE features on that file.

To add a few on top of #BlackJacks answer, ensure that the parent pom in the root of the directory that contains sub-projects contains a modules section:
<modules>
<module>module1</module>
<module>module2</module>
<module>module3</module>
</modules>
So, after the import, unlike IntelliJ, Eclipse opens the sub-modules as separate projects, apart from the main root project.
Hope this helps!

Follow the steps
Choose file menu
Click on import
Select project right click then Convert --> Convert to maven project
Click on import then type or select Existing Maven Project
Now your moduled project in you workbench. If you have any query we can discuss.

Related

How do I import gradle projects with with EGit in Eclipse? (libGDX)

I am quiet new to git but for now I have set up a repository on Bitbucket. The repository contains my libGdx Project which is gradle project containing 3 sub-projects (Android, Desktop and Core).
So now it is my aim to clone this to eclipse using EGit. Everything works until i have to choose a wizard for the Import of the projects.
After i added the repo i use Import > git to Import a Project from the added repository
Here i have to choose the wizard
If i choose "Import existing Projects" the program tells me that no Projects are found.
After that i tried the "Use the New Project wizard" but it confuses me and actually i want to Import a gradle Project and not create a new one.
The last Option "Import as General Project" gives me just Folders and files, so eclipse doesn't notice the existing Projects. Like i said i'm quiet new to git so i appreciate every help ! :)
Thanks :)
The wizard intends to create the project settings files for Eclipse (.project and .settings/).
It seens like you don't have them in your git repository (I don't think you should). That why Eclipse asks you to create a new "project".
the best way is "Import -> Gradle project" (you need to have gradle plugin installed in eclipse first)
But is you don't want to use gradle plugin you need first to generate eclipse file. To do so, type "gradle eclipse" in your working directory (see https://docs.gradle.org/current/userguide/eclipse_plugin.html) and then import existing project into your eclipse workspace.
Another thing, it seems that your workspace is in the same folder as sources (.metadata folder), you should create your eclipse workspace in another directory.

Import Java project from GitHub to Eclipse

I'm trying to import a project on GitHub into eclipse because there are a few things I want to add to it. However whenever I add it I get a ton of errors and problems with it...
The GitHub project is https://github.com/dmulloy2/SwornGuns
I've tried adding it to eclipse and it didn't work. I then tried making a project in eclipse and then put the files in, that didn't work. I also read online that going to Configure -> Convert to Maven Project would work, and then importing the spigot.jar file that this source depends on. However that didn't work and just gives me a ton of import errors.
The errors I am getting:
That project is a maven project. A simple way is clone that project into your machine, and import it as a maven project, with File - Import - Maven - Existing Maven Project.
Eclipse will help you resolve the pom file and configure project and download necessary libs.
First, make sure you are using Luna, the most recent version.
In eclipse, File > Import > Git > Projects from Git > Existing Local Repository.
In the Import Projects from Git screen, click add, then goto the location of the cloned repository. Click open, finish, then next. Select Java > Java Project, and click next again.
At this point, there are three options.
Import existing projects
Use the New Project Wizard
Import as General Project
Pick "Use the New Project Wizard". Then, uncheck "Use Default location", and click browse. Select a folder (You probably want the location you cloned the repo to), click "Open", and give the Project a name. Click next, and make sure that the src/bin folders are setup correctly, and click finish.
Whew!
Of course, the Import from Git option was only added in Luna, so update eclipse if necessary.

How do I open a Java project (that uses maven) in Eclipse? (Do I use M2E or maven-eclipse-plugin)

I'm trying to open this project in Eclipse but not sure how.
I followed the build instructions and not sure how I tell Eclipse to use Maven as the source control (if required) and then open the Java project so I can step through it.
I discovered that there are two Maven plugins for Eclipse (M2E and maven-eclipse-plugin) and not sure which one to choose, since they are incompatible with each other and I need to use.
From the OS command line, in the directory of the pom.xml file:
mvn eclipse:clean eclipse:eclipse
This will create the various eclipse project files. Then in eclipse, in the "Project" pane, right-click > "Import existing project" and navigate to the directory - you'll find it has recognised there's now a project to import.
This should work:
Right click in the Package Explorer, choose Import.
From the context-menu of the Package Explorer choose import.
In the now open panel search for Existing Maven projects. The rest is pretty much self explaining.

How to build and run Maven projects after importing into Eclipse IDE

I am learning building a Java project in Eclipse using Maven. I created a Java project HelloWorld from
“maven-archetype-quickstart” template in a folder D:/maven_projects. Then to convert the Maven project to support Eclipse IDE, I navigated into the project folder and issued the commands:
mvn eclipse:eclipse and mvn package .
Then I imported the project in Eclipse and did the necessary Eclipse configurations like setting the Maven local repository in Eclipse classpath. Now the project in D:/EclipseWorkspace folder. I ran the project successfully in Eclipse printing "helloworld".
Now if I want to go on develop the project and for that reason want to add new dependencies in pom.xml in Eclipse, then the new jars are not added in classpath when I run the project.
So my question is after importing a Maven project into Eclipse how can I add more and more dependencies in pom.xml, then build and run the project? What is the recommended and efficient way to do this?
I would recommend you don't use the m2eclipse command line tools (i.e. mvn eclipse:eclipse) and instead use the built-in Maven support, known as m2e.
Delete your project from Eclipse, then run mvn eclipse:clean on your project to remove the m2eclipse project data. Finally, with a modern version of Eclipse, just do "Import > Maven > Existing project into workspace..." and select your pom.xml.
M2e will automatically manage your dependencies and download them as required. It also supports Maven builds through a new "Run as Maven build..." interface. It's rather nifty.
1.Update project
Right Click on your project maven > update project
2.Build project
Right Click on your project again. run as > Maven build
If you have not created a “Run configuration” yet, it will open a new configuration with some auto filled values.
You can change the name. "Base directory" will be a auto filled value for you. Keep it as it is. Give maven command to ”Goals” fields.
i.e, “clean install” for building purpose
Click apply
Click run.
3.Run project on tomcat
Right Click on your project again. run as > Run-Configuration.
It will open Run-Configuration window for you.
Right Click on “Maven Build” from the right side column and Select “New”.
It will open a blank configuration for you.
Change the name as you want. For the base directory field you can choose values using 3 buttons(workspace,FileSystem,Variables). You can also copy and paste the auto generated value from previously created Run-configuration. Give the Goals as “tomcat:run”. Click apply. Click run.
If you want to get more clear idea with snapshots use the following link.
Build and Run Maven project in Eclipse
(I hope this answer will help someone come after the topic of the question)
Dependencies can be updated by using "Maven --> Update Project.." in Eclipse using m2e plugin, after pom.xml file modification.
Just install the m2e plugin for Eclipse. Then a new command in Eclipse's Import statement will be added called "Import existing maven projects".
answer 1
Right click on your project in eclipse
go to maven -> Update Project
answer 2
simply press Alt+F5
after updating your pom.xml. This will build your project again and download all jar files
Right Click on your project
Go to Maven>Update Project
Check the Force Update of Snapshots/Releases Checkbox
Click Ok
That's all. You can see progression of build in left below corner.
When you add dependency in pom.xml , do a maven clean , and then maven build , it will add the jars into you project.
You can search dependency artifacts at http://mvnrepository.com/
And if it doesn't add jars it should give you errors which will mean that it is not able to fetch the jar, that could be due to broken repository or connection problems.
Well sometimes if it is one or two jars, better download them and add to build path , but with a lot of dependencies use maven.
If you are getting this error :in cucumber Exception
Cucumber Exception: java.lang.ClassNotFoundException:
cucumber.io.ResourceLoader :
then add following jar file to your pom.xml
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-core</artifactId>
<version>1.1.8</version>
</dependency>
Run the maven build once and should be gone

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.

Categories