1

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.

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
thorald_
  • 35
  • 5
  • What do you mean by „_so that it can be used as a program?_“? Builds (equal or higher than with the `package` phase) create JARs and JARs (usually) contain executable code, i.e. programs. For `phase` and other items of Maven see [_Maven: Lifecycle vs. Phase vs. Plugin vs. Goal_](https://stackoverflow.com/a/30953905/1744774). – Gerold Broser Aug 21 '21 at 22:50
  • 1
    @Gerold Broser Oh yeah, a poor choice of words from my side. But basically I meant what you are saying here. An (executable) JAR that I can use to run my code. There are more questions which come with this poorly worded question, which I probably will ask here in due time, if I don't find the answers myself, but they would be a little much for a chat :) – thorald_ Aug 23 '21 at 20:39

2 Answers2

2

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>....

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
  • 1
    That cleared some things up for me, thank you^^. But one question arose which I think still belongs here. I think I understand the Idea and the difference between an aggregator and a parent, but if I am right, my project structure doesn't seem too good anymore. Because I have three aggregators and in my understanding they can't contain information about dependencies. Which would force me to eventually declare the same dependencies multiple times. So would it make sense to make `Frontend` and `Backend` to parents, so I just have two places for dependencies needed more than once? – thorald_ Aug 23 '21 at 20:49
  • @thorald_ Aggregator projects aggregate (sub-)modules, so that all modules can be built altogether by just buildung the aggregator. Parents inherit POM declarations to its children. Projects that are both an aggregator AND a parent (see the ¹ in my answer) can, and usually should, contain ``. That's the whole point of parents: Don't declare the same things multiple times, declare them once on a higher level. In my answer `frontend` and `backend` _are_ parents since `window` and `database` (+ `***`) declare them as ``, i.e. all the three aggregators are also parents. – Gerold Broser Aug 24 '21 at 01:52
  • that's what I hoped for. I want to do just that: `"Don't declare the same things multiple times, declare them once on a higher level"`, but yesterday I read somewhere that an aggregator can't be a parent at the same time, which confused me. But if it is like that, that's perfect – thorald_ Aug 24 '21 at 14:31
0
  1. Create a maven project with packaging as pom and it is your parent project e.g. parent

  2. Create folder as child1 inside parent.

  3. create a maven project and named it as child1 when you click on new maven project for child1 uncheck default workspace location and point to child1 folder which you are created in point 2 and now you can give the packaging name as jar.

  4. same logic create another project for your war named as child 2.

  5. now you have created 3 projects one is parent and another are child1 and child 2. now go to the parent pom.xml mentioned the modules.

    child1 child2
  6. You have to make sure that you are always using jdk as your JRE System Library. e.g. jdk 1.8

  7. When you use packaging as war then it may have chance of failOnWebxml issue, then add your build configuration failonwebxml as false.

  8. Always you have to add your maven plugins otherwise you will face problem to generate your war.

  9. Now your multimodule project is ready you can start.

  10. Once you generate your war you may deploy in any server.