Carol Carol - 1 year ago 94
Java Question

Organizing a Maven project in IntelliJ: Several Modules? Submodules? Is this possible?

I originally asked this question regarding how should I organize my application: layers first or slices first: Multi-Module Web App with Spring and Maven

I'm leaning towards slices first, and thinking that a good way to keep it all neatly would be having:

  • One project for the whole system

  • One IntelliJ module for each "block" I have (example:

I more or less have the idea of how to do it (I'm too new to this tools and frameworks), but I have some doubts:

  1. If everything is split on IntelliJ modules (my "blocks"), I can have a better visibility/separation of concerns between my blocks. Could I do this with simple packaging on a one-IntelliJ-Module-Only configuration? (The project is not that big either, but the idea was to leave room fro growing in the far far future).

  2. If I go for one IntelliJ module = one block, would I have several WEB-INF folders and one pom.xml for each module?

  3. Would I need a "main" module?

  4. Would this allow me to make a part of the web interface fixed (main top horizontal bar, and left side vertical bar) and the rest changing depending how the user browses the application?

Answer Source

As to whether it is possible to organize a maven project into one project with submodules, here are some important considerations when doing it in IntelliJ.

  • Avoid doing any project organization using IntelliJ. Specify the project hierarchy in your pom.xml files.
  • To create an IntelliJ project that reflects your maven project hierarchy, open the parent 'pom.xml' in IntelliJ directly, as if it were an IntelliJ project file. IntelliJ will create a project hierarchy that reflects the maven project hierarchy.
  • Have IntelliJ monitor pom.xml files and watch for changes to the project hierarchy. It will add new projects to the hierarchy for you, and prompt to remove the corresponding IntelliJ project when a maven project is removed from a parent pom.xml. To enable this, search for "Import Maven projects automatically" in IntelliJ Preferences and enable it.
  • Do not go around maven when you need to add library dependencies. Always manage dependencies in pom.xml files.
  • If you avoid touching IntelliJ project configurations and manage them exclusively in your pom.xml files, you can rebuild your IntelliJ projects from the pom.xml files if they get out of sync. Just re-open the parent pom.xml file as a project, and IntelliJ will prompt you, asking if you want to delete the existing IntelliJ projects and recreate them. Tell it to do so, and you will be back in sync. This is sometimes useful if you use source control, and different versions of your source have different projects and subprojects.

For guidance on project structure, IntelliJ can help out with that too. In the Analyze menu, I would suggest enabling the following inspections:

  • Cyclic package dependency (under Dependency Issues)
  • Class independent of its module (under Modularization Issues)
  • Class only used from one other module (under Modularization Issues)
  • Package with disjoint dependency graph (under Packaging Issues)
  • Everything under Maven sections
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download