Creating the Application Directory Structure
Typically, the code for a modular application is organized in a directory structure, similar to the one shown in Figure 19.9. The modules are created in the src directory and compiled into the mods directory. The compiled modules are bundled into modular JARs and placed in the mlib directory. The different JDK tools can thus operate on the relevant directories.
Figure 19.9 Application Directory Structure
The application root directory for the adviceApp application bears the same name. It is also the working directory in which all commands are issued to the JDK tools. Under the adviceApp directory, the following command creates the directories shown in Figure 19.9:
>
mkdir mlib mods src
Creating an Exploded Module Directory
The model module does not require any user-defined modules—that is, it does not depend on any user-defined module, and can therefore be implemented first. Its module declaration is straightforward, as it exports its only package, com.passion.model (shown in Figure 19.10). The directory layout for the model module is created manually under the src directory as shown in Figure 19.10, and referred to as the exploded module.
Figure 19.10 Exploded Module Structure for the model Module
Referring to Figure 19.10, (1) shows that the module name and that of the module root directory must be the same, and (2) shows that the module-info.java file containing the module declaration must reside immediately under the module root directory.
Packages were discussed in §6.3, p. 326. Any package (or subpackage) that a module contains is mapped to a corresponding directory hierarchy under the module root directory. In Figure 19.10, (3) shows how the package com.passion.model of the module is laid out under the module root directory, with the directory hierarchy com/passion/model mirroring its package name. It also shows that the package has only one source file, AdviceModel.java. If the package had any other source files, they would also be placed in the same location.
A package is included in a module by virtue of its location in the module root directory. The organization of the module root directory, as in Figure 19.10, defines what constitutes a module. Similarly, any other packages in the module are mapped to their directory structure under the module root directory.
Figure 19.11 shows the module graph of the adviceApp application together with the module declaration of each module that is in the module graph. Each module declaration is in a source file named module-info.java, and is placed in its corresponding exploded module directory, as shown in Figure 19.12.
Figure 19.11 Module Graph and Module Declarations
Figure 19.12 Compiling Exploded Modules Containing Source Code
Figure 19.12(a) shows the contents of the src directory where each module is mapped to its corresponding exploded module. Each module has one package that contains one source file. This package is mapped to its directory hierarchy under the root directory of its module.
Leave a Reply