Mert Y. Ertugrul wrote:HI've read a lot about Java Modules, and now I'm starting to think I might be missing out on a valuable feature of Java.
I'm a big fan of modules. I'm happy you're giving them a shot.
Do you think it is a good idea to create separate modules for each package?
No, for two reasons.
First, modules don't have a one-to-one relationship with packages. A module
may contain exactly one package, and while this is indeed often the case, there could be good reasons for a module to contain more than one package. Blindly creating a separate module per package is not a good idea. Some thought and planning is always required.
Secondly, you are using packages wrongly. A package is a collection of classes that depend strongly on each other. A class might want to access package private members of another class that is in the same package.
Instead, you seem to have divided your classes over packages based on how much the classes resemble each other, not how strongly the classes depend on each other. Why does a
UserAccountController (for example) need to be in the same package as a
ProductCategoryController (another example). Sure, they're both controllers, but neither of them has anything to do with the other.
Instead, you might want to have a package
com.example.shop.user that contains the classes related to users and user accounts, and a package
com.example.shop.product that contains the classes related to products and product categories. Web API controllers and the request and response types could be in
com.example.shop.web.user and
com.example.shop.web.product.
Modules then contain all the packages that an application might want to use together. It's unlikely that you'll want to use
com.example.shop.user in an application, but not
com.example.shop.product. On the other hand, you could put
com.example.shop.user in a different module than
com.example.shop.web.user, because the controllers are only used in a web application, while the model classes might also be used in a client application or administrative tool.
So we could have the following two modules:
Additionally, since I like using Gradle or Maven, do they work well with Java Module feature?
Yes. There's no issue in using these tools together with JPMS modules.