Multi-module projects: what are the benefits?

Solved
Programmeranonyme343 -  
KX Posted messages 19031 Status Moderator -
Hello,
I am interested in Maven, I learned that you can have projects containing multiple modules, but I would like to know what the benefit is of having several? Why not simply create separate packages for each part of the application?
Thank you in advance for your response,
Programmeranonyme343

1 answer

KX Posted messages 19031 Status Moderator 3 020
 
Hello,

With Maven, each deliverable must have its own module.

For example, if you are creating a client/server application, the client should be in one module, and the server in another. Generally, in this case, a third module will be created that will be used by both the client and the server.

To extend the example, you can imagine that instead of having one client, you could have several: a lightweight client (HTML, JS...), a heavy client (Swing, Java FX), a mobile client, etc. Each different deliverable will be in a dedicated module.

As for the server, if we respect the multi-tier architecture of JEE, we could isolate each layer into a dedicated Maven module.
These separations especially make sense concerning the dependencies of each module. For example, Hibernate will be useful for the DAO layer and JAX-RS on the web service layer, but not the other way around.
In the context of a large project with several developers of varying skill levels, the Maven structure helps ensure that the code remains clean. For instance, it would be impossible to make a database request in the middle of a web service code if you have isolated the DAOs in a different module.

An article that might interest you:
https://forums.commentcamarche.net/forum/affich-37598446-implementation-d-une-architecture-multi-tiers-avec-spring
In this code example, the server application is divided into 9 modules (this is a bit extreme, but it can be justified)

That being said, if your application only has a single deliverable, you can very well limit yourself to one module. Maven is a powerful tool that adapts to all types of Java projects, including the simplest ones.
--
Trust does not exclude control.
0
Programmeranonyme343
 
Thank you for your response.
So if we create a multi-module maven project, those that do not depend on any other module will be in separate files, and those that depend on each other will be in a single file?
And if for example moduleA depends on moduleB,
but moduleC also depends on moduleB, will there be two files with one for moduleA+moduleB and the other for moduleC+moduleB?
0
KX Posted messages 19031 Status Moderator 3 020 > Programmeranonyme343
 
Each Maven module must have its own folder, each with a pom.xml at the root of their folder, and as many code files as necessary.

If A depends on B, then a dependency on B must be added in the pom.xml of A.
When Maven builds the project, it will first compile B, and then use it to compile A.
The same goes for C if it depends on B, it just needs to be configured in the pom.xml of C.

Note: at no point do we need to configure the pom.xml of B to indicate that it will be used by A or C.
0
Programmeranonyme343 > KX Posted messages 19031 Status Moderator
 

By the way, I have a question. Where should we put the main class?

0
KX Posted messages 19031 Status Moderator 3 020 > Programmeranonyme343
 
It doesn't change anything regarding the main class, you can put it with the rest of the Java code, in whatever package you want with whatever class name you want.

To start, you could take a look at this article:
https://forums.commentcamarche.net/forum/affich-37593827-debuter-avec-maven

Note: if you have several modules, you can have several main methods, one for each deliverable. However, a module is not required to have a main method, and it is quite common not to have one.
0