Search code examples
mavenbuildstructure

Maven build multi-module project


I am recently learning more and more in the field of Maven projects and I want to use it for an idea I wanted to tackle for some time. This question is concerning, how to effectively build a Maven project, with the structure I will use.

My take on the structure would be as follows:

- MyProject
  |
  |_Frontend
  | |
  | |_Window
  | | |_pom.xml
  | |...
  | |_pom.xml
  |
  |_Backend
  | |
  | |_Database
  | | |_pom.xml
  | |...
  | |_pom.xml
  |
  |_pom.xml

Where MyProject would be the parent to Frontend and Backend. For all of which packaging would be set to pom. Finally in Frontend and Backend will be the projects which contain the code, here for example a Window project and a Database project (in the actual there of course will be more than just one project in Frontend and Backend, this is just for visualization). Projects from Frontend could be dependent on projects from Backend.

I would set Frontend and Backend as <module>s in the MyProject parent POM and all projects in Frontend as <module>s in the Frontend POM, same deal for Backend. Then I would add the <dependencies> for the Backend projects inside the <dependencyManagement> element of Frontend's POM, so I can add the needed dependencies in the Frontend projects separately. (Main will be, for this example, in the Window Project)

So my question is: How would one build that so that it can be used as a program?

I am relatively new to Maven so I am not as well versed in the whole build idea. I get its benefits, hence I want to use it, but I am not sure how to achieve what I want in a project like this. I searched the net for answers, but I probably phrased it too poorly because I couldn't find the answers I needed. So if there is an article or an answer out there, which applies here, it's okay too just link it, but if someone here will take their time to give me a more in depth explanation for this specific problem, I wouldn't complain^^.

So thanks for any answers.


Solution

  • The structure you thought of looks resonable. I'm adding some details to your chart:

    +- myProject 
       +- pom.xml  ... <packaging>pom, aggregator (<modules>) of frontend & backend
       |
       +- frontend
       |  +- pom.xml  ... <parent>myProject, <packaging>pom, aggregator (<modules>) of window & ***
       |  |  
       |  +- window
       |  |  +- pom.xml  ... <parent>frontend
       |  |
       |  +- ***
       |     +- pom.xml  ... <parent>frontend
       |
       +- backend
          +- pom.xml  ... <parent>myProject, <packaging>pom, aggregator (<modules>) of database & ***
          |
          +- database
          |  +- pom.xml  ... <parent>backend
          |
          +- ***
             +- pom.xml  ... <parent>backend
    

    Just to clarify, regarding „MyProject would be the parent [...] packaging would be set to pom.“, see the POM Reference, Inheritance v. Aggregation and this answer:

    Long story short, there are the following relationships:

    • Aggregator¹ (<packaging>pom) → (sub-)modules ... for, well, aggregation of (sub-)modules
    • Parent project (<packaging>pom¹|jar|war|...) ← child project ... for inheritance (of POM configuration from the parent to the child)

    So, <packaging>pom is not required due to MyProject being a parent but since it is an aggregator. In fact, a parent project doesn't know anything about its childs. Only the child projects declare <parent>....