I don't fully understand the combination of JPMS and maven. In multiple places, the two claim to be independent and orthogonal to each other, yet both offer concepts for dependency and encapsulation.
Our Setup
We currently have a code base consisting of multiple maven projects that are referencing each other by using maven dependencies. We don't use maven modules.
We now wish to extract some functionality of one of the projects, which may be eventually even released as a public library. For this, we want to see if using JPMS modules would give us benefits. From what I've read so far (see here and there), strong arguments include better encapsulation, and I can understand that using the JPMS export statements etc. enable hiding classes that are not meant for the user of the library.
We extracted the affected code and placed it in a separate maven project, added a module-info.java, and only exported the relevant class.
What confuses me
My confusion now starts when we wanted to use this library in our legacy code, where we have extracted the library from. This code does not use JPMS. From what I've understood, we could just add the new library maven project as dependency, but this would not grant us the benefits of JPMS, as all classes of the library then could be used by the consuming application right?
The confusion grows as I've also tried to add the library as module using a maven-compiler-plugin configuration as described here. This works, and it seems to give me the benefits of the JPMS:
- the library is no longer required as maven dependency in the
pom - I can only access the intended classes from the library
However, adding the module to the main application is cumbersome, as we have to modify the compiler and runtime arguments in many places. I understand that ideally, our main application would also use JPMS, and we could add the dependency in the
module-info.java, but this is no option at the moment. Is this the way that the JPMS is intended to work, so that the dependencies no longer have to be stated in the pom?
I have the feeling that I'm missing a bigger point, so I'd welcome a fundamental explanation of the issue at hand.