Creating a custom aggregator module

Notice that we have two modules in the address book viewer application that provides a view of the address book. The packt.addressbook module shows a simple list of contacts in command line. The packt.addressbook.ui module shows the address book contacts and details in UI form. Both these modules happen to use the two utility modules to get the list of contacts (packt.contact) and to sort them (sort.util). Here, we have just two modules, so it's not that big of a deal to add the requires descriptor for both of these modules in two places. But imagine if there were many more libraries and many more consumers! You'd be duplicating the list multiple times.

To avoid that, let's create an aggregator module that bundles and re-exports the packt.contact and sort.util modules. We can then have the packt.addressbook and packt.addressbook.ui modules depend on the aggregator module directly.

Let's call the aggregator module packt.addressbook.lib. This module acts as the library for all addressbook modules. Create a directory with the name of the module in the src folder and add the following code in its module descriptor:

    module packt.addressbook.lib {   
      requires transitive packt.contact; 
      requires transitive packt.sortutil; 
    } 

This is actually the only file that this module would need. It doesn't provide any APIs of its own. It just has the module descriptor that requires transitive all the modules that it wants to re-export. Here, we choose for it to re-export the two custom utility modules we've created. We have the option here to add requires transitive on some of the platform modules as well, like java.logging. But we'll just stick with our custom modules for now.

The next step is to go to the consumer modules and change the direct dependencies to the aggregator module instead.

Here's the module descriptors for the two address book modules:

    module packt.addressbook {   
      requires java.logging; 
      requires packt.addressbook.lib; 
    }
module packt.addressbook.ui { exports packt.addressbook.ui; requires java.logging; requires javafx.base; requires javafx.controls; requires javafx.graphics; requires packt.addressbook.lib; }

Compile and execute the two modules, and you should still see the output as before. Here's the updated dependency graph of our address book application now, excluding the platform modules. Notice that the transitive dependencies are illustrated with a dotted arrow to convey that while the dependency is not direct, it's still there!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.79.84