Reputation: 35
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.
Upvotes: 1
Views: 4266
Reputation: 11
Create a maven project with packaging as pom and it is your parent project e.g. parent
Create folder as child1 inside parent.
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.
same logic create another project for your war named as child 2.
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 child2You have to make sure that you are always using jdk as your JRE System Library. e.g. jdk 1.8
When you use packaging as war then it may have chance of failOnWebxml issue, then add your build configuration failonwebxml as false.
Always you have to add your maven plugins otherwise you will face problem to generate your war.
Now your multimodule project is ready you can start.
Once you generate your war you may deploy in any server.
Upvotes: 0
Reputation: 14782
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>...
.
Upvotes: 3