Crazy Search Results in Eclipse for Multiple Module Maven Projects

Setting Resource Filtering right in Eclipse for Multiple Module Maven Projects.

Eclipse Mars improved the handling of Multiple Module Maven Projects by providing hierarchical tree view for them. However, there are still some glitches that can give a hard time for developers. Luckily, many of the issues can be addressed with some configuration.

The Problem

Eclipse handles every Maven Module as a standalone entity, regardless if it contains other Modules or not.

Multiple Modules

In the layout above there are two somewhat independent Maven Projects: the top-module and the sub-module.

This can cause problems when you fire up the Open Resource window and search for a file, because it finds the matching entries for every single Maven Project that contains the file somewhere in it's subdirectories. This result in duplicated search entries as Eclipse finds the file in both Maven Projects. Even worse, for Projects that contain other Modules Eclipse does not set the derived flag on the target directories found in the submodules.

So, in the example project layout the index.html file in the sub-module would appear three times in the Open Resource dialog. (And in Search Results.)

Open Resource Window
  • The first, correct result for the sub-module Project, in it's src/main/resources directory.
  • A slightly wrong one in the top-module Project, in the sub-module/src/main/resources directory.
  • And a totally wrong one also in the top-module Project, in it's sub-module/target/classes directory.

To make things more irritating, selecting the second entry opens the same file in the Editor as selecting the first one, but does not cope with the Link with Editor feature of the Project Explorer.

Target Folders

This is definitely not a good default behaviour. Both NetBeans and IntelliJ IDEA handles this situation much better. In a more complex setup there can be more levels of nested modules and all hell breaks loose.

The Solution

Fortunately the solution is simple.

First, check the "Hide folders of physically nested modules" in the Window > Preferences > Maven section. Yet it's "Experimental", it works fine for me, and I strongly recommend using it.

One caveat is that it does not always work on existing projects. In this case, the projects must be reimported. I suggest doing a reimport even if everything seems to work fine. For me it did not filter the false result from the target directory in some cases, but the reimport fixed that.

The Bonus

There are some other features I found in the journey of getting rid of these quirk.

There is a quick and dirty solution for removing multiple search results for the same physical file. Just open the Resource dialog, and set the Filter Duplicated Resources in the upper right corner.

Even more useful in real life scenarios, that Open Resource Window can filter for directory names too, not just file names like this: *site/index.html. This searches for the index.html file that is in the site directory. This can be handy in a large projects, where there are several files with the same name, but in different directories.

November 3, 2015

Free PDF guide

Sign up to our newsletter and download the "Git Tips and Tricks" guide.

In this article