multi module maven project structure in eclipse - eclipse

I have two modules under a main project
main
|
|
--module-a
|
|
--module-b
I created it using eclipse->new ->Maven project, eclipse->new->maven module.
The structure in the file system looks correct. But in eclipse, it shows module-a, module-b as two new projects in the package explorer.
My main project looks like this.
I have seen few multi module projects where, the modules are present within the main project in package explorer. The main project had
module-a/src/main/java
module-a/src/test/java
module-a/src/resources/java
module-b/src/main/java
module-b/src/test/java
module-b/src/resources/java.
How do I convert my project to look like this ?

Starting from Eclipse 4.5.M5 (that you can already download at http://eclipse.org/downloads ), the Project Explorer view has a parameter to show projects hierarchically, to better handle such case. See https://www.eclipse.org/eclipse/news/4.5/M5/ for details.

You will find if you navigate the actual file system (oe use the 'navigator' view in eclipse) that the sub modules do in fact exist underneath the main parent project folder.
Eclipse is nice enough (clever enough or whatever) to pull them out when you use the 'package explorer' view. I guess this makes it easier to find and navigate your way aournd a highly modular project.
However if you set up your Parent POM correctly you can have a flat structure to your modular project if you so desire (I haven't as yet been able to get this to work, so far I think it is related to the sub module classpaths or something?).
David.

I solved this problem as follows:
Under the "Project Explorer" click on the "View Menu" (little triangle) and select "Filters and Customization..."
Under the "Content" tab tick "Nested Projects"
Finally, Under the "Pre-set filters" tab tick "Nested Projects: hide folders when projects is shown as nested" and "Nested Projects: hide-top-level project if shown as nested".
By doing that I come up with the following structure.
I used a Spring Tool Suite 3: Version: 3.9.11

Basically, Eclipse does not support the Maven way of structuring projects.
By using the M2E it is possible to execute Maven as part of the build, but that way you lose the project management and incremental build capabilities of Eclipse.
You can also simply use the Eclipse layout in the IDE, and build with Maven in the build server, but that way you have to make sure the two build are identical.

I have seen few multi module projects where, the modules are present
within the main project in package explorer.
Thats what do maven eclipse plugin. It just create single project with multiple source directories for each module of maven multimodule project.
If you want to see some hierarchical structure in eclipse - use working sets. Create working set with name of parent, add module-a and module-b to it and visually it will be separated from other projects in eclipse workspace.

You can do this in earlier versions of eclipse as well. Close and Remove the sub-module projects (as they are already present in the parent folder). Now click on Properties on the main project -> Project Facets -> Convert to faceted form. This will detect Java automatically, Click on Apply, OK. Now you can see that these modules are created with Source folders

Related

How to use SVN to build a library

I am trying to follow a tutorial, and I am told to:
1- Get the source code for the Java EMV Reader library from http://code.google.com/p/javaemvreader/ and build it.
2- Drop the resulting jar file in lib/.
3- Import the project in Eclipse and build it.
I right click the java files, and choose run as but don't get an option to run as Java Application. I also can not export the files as a JAR file. I have enclosed an image of what I have
After the first comment, I right clicked on my project, and under Maven, chose the option "configure as Maven" project. ( Thank you so much; this must be one of the fastest resolutions in the world )And I can now run the project. I get the window in my pic2, which I have attached. I don't however know what step 2 of the above instructions means. I don't see a lib/ folder. And the project he is refering to in step 3 is on git. Any ideas on what he means? ![pic2]!1
Eclipse projects have a "type" and that controls what tools are available. You probably created a "Basic Project" which means there are no compilers or other Java tools associated with it. You would want to create a Java Project in Eclipse.
That project does not seem to have Eclipse .project and .classpath files checked into the repository. It does look to be a Maven project however. So you would either want the m2eclipse plugins installed, and check this out as a Maven project, which would handle configuring everything else, or you want to use the Checkout As ... option and use the wizard to create a new Java project to checkout.
These are more Eclipse IDE questions than SVN or Subclipse questions.

Eclipse Maven showing multiple paths for the same file

Is there any setting in Eclipse to show only the Maven project where the file resides?
I have different Maven projects which are modules of a parent Maven project:
projParent
|-projWeb
|-projModel
|-projServices
|-...
If I look for a file using "Open Resource" (using Ctrl + Shift + r) in Eclipse, it appears in many projects (E.g.: in projParent and in projWeb).
Is there any way to show only relevant folder where that file is actually resided in?
Try defining a Working Set that includes only the modules in which you want to search and then use that Working Set in the search.
EDIT
Actually I tried it and it just works perfectly by default in eclipse 4.3 (Kepler), only one result is returned for nested projects.
Anyway, here's how to use working set in open resource (the example is not of a resource in a nested module as in this case Kepler don't show duplicate results; so the example is just of two different resources baring the same name in two different modules):
(1) Hit the ctrl+shift+R keys. I searched for a resource called pom.xml. You can see two results:
(2) Press the little downward-pointing arrow on the top right of the window and choose Select Working Set...:
(3) Check the radio button of Selected Working Sets and check the desired projects (in your case the leaf projects):
(4) And voila, Open Resource returns only one result:
You are probably talking about a kind of duplication between parent project and its sub-modules. In the latest version of Eclipse (from 3.6 maybe and sure from 3.7) there is a new option
Menu Windows >
Item Preferences >
Tree item Maven >
Option : Hide folders of physically nested modules
This would not work on a existing project. You must delete them (logically) and re-import them
I just had the same issue and what worked for me was to select Filter Duplicated Resources in the Open Resource dialog:
Another (unrelated) useful feature is to hide submodule-folders from the parent-project in the package explorer.
This can be done by using the Maven modules filter in the menu of the package explorer:
Tested with Eclipse 2019-06 (4.12.0)

Eclipse file search finds the same file multiple times

In Eclipse I have two maven projects A and B, where A is a parent for B. The directory structure is the following:
A/pom.xml
A/B/pom.xml
A/B/...
Then I use File search (Ctrl+H) to find any file in the project B. The search result window shows the file two times with different relative paths:
A/B/<my_file>
B/<my_file>
So, the same file is shown twice. Obviously, the first search result is reduntant. Is there a way to exclude these duplicate search results?
What I personally do to avoid this is marking each module in the parent project as derived (right-click on the folder > properties > Attributes: Derived).
Then when you perform a file search, uncheck "Consider derived resources" (I don't think it is checked by default) and you won't get the A/B/<my_file>.
The only inconvenience is that you must do this for each module, and each time a new module is added.
In an open resource window (cntrl+shift+R), click on the arrow on top-right and check "Filter Duplicated Resources". This worked for me.
Checking Window -> Preferences -> Maven -> Hide folders of physically nested modules (experimental) worked for me in Mars.1 Release (4.5.1).
After checking it I closed all projects within eclipse, deleted eclipse project files (.project, .classpath and .settings) and reimported my maven projects.
I had similar problem.
My solution for this problem was
Create eclipse "working set" of projects to search
Set this working set as option into search dialog
Search in it
Windows - Preferences - Maven - Hide folders of physically nested modules
I agree with Damon Horrell.
Go to Windows - Preferences - Maven - Hide folders of physically nested modules (make sure the checkbox is checked).
Note: Make sure to re-import the maven project into eclipse to see the changes.
Within the parent project, child ones were already visually hidden in my case, but still found through search and Ctrl-click links.
I solved this by using Resource filters:
Right-click (parent) project > Properties > Resource > Resource Filters
Click Add Filter..., select Applies to -> Folders and enter one child project's folder name, click OK
Repeat 2. for all child project folders
By the end, you should have something like this:
There is an ancient bug which requests Eclipse to support nested projects. Opened 2008, won't happen soon, I guess.
Since Eclipse doesn't support Maven's default nested module layout, you have to import each module individually which doesn't confuse Eclipse but it can confuse you as you can see in the search results. Similar things happen when you start to save launch configs in a module.
There are several possible solutions:
Get used to ignoring half the search results. Takes some discipline but no magic.
Make sure your root project doesn't contain anything important that you might want to open from Eclipse (i.e. move the parent POM to a new module which must be a child of the root project). Now you can close the root project.
Takes some discipline where to put things in your project and it's clumsy when you have to add new modules.
Use the flat multi-module approach. This is an extension to #2, it looks odd and there might be plugins that are offended by that. So if you use a lot of nonstandard plugins, you should check whether they like the new layout.
As a new joiner, still couldn't comment but I'd really like to add something to the best answer. It works! It not only removed the duplicate files for me (one from child module and one from parent module), it also removed the class files from parent module).
But it added the folder to my .gitignore which will cause problem for new files/directories added later. I just reverted the change to .gitignore and it still works in eclipse.
Find this answer to disable auto change of .gitignore
EGit and Eclipse modifies .gitignore file but it should not
You can solve this problem using a flat multi-module approach.
Caveats may apply if you're using the maven-release-plugin with a version control system other than Subversion.
In addition to "Hide folders of physically nested modules" mentioned, you could uncheck parent modules when importing Maven project:

How do I import a multi module Maven project from SVN into Eclipse (Indigo) so that child modules can be built independently?

I have tried posting this question on the Eclipse Maven forums but it seems very quiet there so here goes...
My setup:
-Eclipse Indigo Java EE bundle
-m2e
-Subversive
We have a fairly simple multi module maven project on our repo. It has a parent project/pom and 3 child projects/poms.
Steps I am taking to import:
1) I import the project by using "import->import->->Maven->Check out Maven Projects from SCM".
2) On the next dialogue box I select svn as the SCM URL type and enter the rest of the repo URL. I leave the check boxes checked for "Checkout Head Revision" and "Checkout all Projects" and under "advanced" I leave the check box "Resolve Workspace Conflicts" checked too. I do not enter anything under "Profiles" or "Name Template".
3) On the next dialogue box I leave "Use default Workspace location" checked and leave "Add projects(s) to Working set" unchecked and click "finish".
4) I wait a minute or so for the next dialogue box to appear. It asks me to select maven projects and has a file structure as it should be-parent project/pom and 3 child projects/poms with a checkbox beside all 4 items. I have tried to select either just the parent, just the children or all 4 without success.
5) When I select just the parent and uncheck the children above, the project appears to be imported. The parent appears in the project explorer and the three children appear as sub-folders of the parent with each having a pom.
6) If I right click on the parent, I get a Maven item in the context menu and indeed I can "run as->Maven build, install etc.." ON THE PARENT ONLY. The Child projects have no such Maven context menus nor the ability to "run as->Maven x".
If I try to import all 4 projects in step 4 above, it imports with the same structure as outlined in 5 above but with the children appearing additionally as separate "independent" Maven projects (with errors).
Ultimately I just want to import the multi-module Maven project and be able to deploy it on my Eclipse Tomcat installation, so if I change a file in one of the child projects (called frontend) the change will be deployed quickly and I can check the effect out in a browser.
Thank you very much for any and all help with this issue! I am happy to answer any further questions to help trouble shoot the problem.
My projects occasionally also lose this ability (though they have it right after import). Usually it helps to refresh Maven configuration, refresh/clean projects, or restart Eclipse. Note that it should still be possible to run it the long way, so "Run -> Run Configurations -> Maven".
I'm running Helios SR 2, m2e 1.0.0.20110607-2117.
Pretty sure each module would have to have it's own .project settings that you would need to commit.
Did you install the m2e WTP extension? At least in theory that should be a better option than tinkering with project settings outside of Maven/m2e control.
I probably explained the problem poorly in my initial question. The main problem was that I was not able to deploy the webapp component of the multi-module maven project to Tomcat. I was making a couple of basic errors:
I was importing the parent project without selecting the children, assuming Maven would get them all and that would suffice, but it did not: I had to select the parent and all children when importing as Maven project from svn.
I then failed to convert the webapp part of the project to a faceted type, and further failed to select "dynamic web app". I needed (according to a colleague) to then select version 2.5 to avoid incompatibilities with older servlet containers. (not sure how relevant this is to Tomcat 6, which is what I use).
Once this was done I had the parent project and 3 subfolders and also the 3 child projects in their own right appearing in the project explorer (but on file system they all exist under the parent project, as it should be). I could then right click on the webapp module (the eclipse project explorer project, not the subfolder) and run as Maven project (clean, build etc.).
I could then right click and "run on server". I had an error upon publishing about various files being out of sync. I just refreshed the child projects in eclipse and then they would publish successfully.
Eclipse has so many versions, with so many incompatible plugins etc. that it gets confusing very quickly-lots of the tutorials are highly specific to particular versions etc.
Thanks for helping anyway guys.

What happened to the "Enable Nested Modules" option in m2-eclipse?

I contribute to and implement some open source projects that use a Maven2 build system organized into several sub-modules. I've also been an Eclipse user for several years. With these projects, I have historically leveraged the maven-eclipse plugin so that Eclipse can correctly recognise library dependencies in my project.
It used to be the case that -- when I set up a new workspace or project -- I would have to right-click select "Enable Dependency Management" followed by right-click "Enable Nested Modules." Once I did these things, maven & eclipse would find dependent libraries properly in my .m2 local repo.
But in my most recent install of Eclipse/m2-eclipse -- Eclipse Build 20100218-1602, m2-eclipse 0.10.0.20100209-0800 (sorry, the "About" dialog doesn't provide anything better than that) -- the "Enable Nested Modules" option is missing. Nor does the project seem willing to find my nested modules without it.
After much digging and some trial & error, I got the IDE to recognize my sub-modules by setting...
includeModules=true
In my org.maven.ide.eclipse.pref file manually.
But my question is: what gives? Why did this option disappear? Is there some newer, better way that I should be using m2-eclipse to find nested modules? How are other Maven & Eclipse users dealing with this issue?
The thing here is the option to enable nested modules was moved to the Window >Preferences > Maven. Set the Support multiple Maven modules mapped to single Eclipse Workspace project checkbox, right click your project, navigate to Maven menu item you can find the 'Enable nested modules' option.
Looks like support for this feature was recently removed:
https://issues.sonatype.org/browse/MNGECLIPSE-2291
Why did this option disappear? Is there some newer, better way that I should be using m2-eclipse to find nested modules?
Could this be somehow related to the option below (accessible via Windows > Preferences > Maven):
To be honest, I'm not 100% sure because I don't create my projects under Eclipse, I create them on the command line outside Eclipse and import them as Existing Maven Projects (and this works with nested modules).