A module is a discrete unit of functionality that can be run, tested, and debugged independently. Modules include such things as source code, build scripts, unit tests, deployment descriptors, etc.
The key components of a module are:
Content roots- the directories where the files belonging to the module (source code, resources, etc.) are stored. Each directory can belong to one and only one module; it's not possible to share a content root between multiple modules.
Source roots- A content root can have multiple source roots underneath it. Source roots can have different types: regular source roots, test source roots, resource roots, etc. In IntelliJ IDEA, source roots are used as roots of the package hierarchy structure. Java classes directly under a source root will be in the root package. Source roots can also be used to implement more fine-grained dependency checks. Code under a regular source root cannot depend on code under a test source root.
Facets- the list of framework-specific configuration entries.
In addition to that, a module can store other settings, such as a module-specific SDK, compile output path settings, etc. Plugins can store additional data associated with a module by creating facets or module-level components.
The IntelliJ Platform provides a number of classes and interfaces you can use to work with modules:
This section discusses how to complete some common tasks related to management of modules.
See Changing the Project Structure for information on modifying project/module structure.
How do I get a list of modules the project includes?
How do I get dependencies and classpath of a module?
Order entries include SDK, libraries and other modules the module uses. With the IntelliJ IDEA UI, you can view order entries for a module on the Dependencies tab of the Project Structure dialog box.
The following code snippet illustrates how you can get classpath (classes root of all dependencies) for a module:
How do I get the SDK the module uses?
ModuleRootManager.getSdk() method. This method returns a value of the
The following code snippet illustrates how you can get detailed information on SDK the specified module uses:
How do I get a list of modules on which this module directly depends?
ModuleRootManager.getDependencies() method to get an array of the
Module type values or the
ModuleRootManager.getDependencyModuleNames() to get an array of module names. To clarify, consider the following code snippet:
How do I get a list of modules that depend on this module?
Note that you can also check whether a module (module1) depends on another specified module (module2) using the
ModuleManager.isModuleDependent() method in the following way:
How do I get a module to which the specified file or PSI element belongs?
To get the project module to which the specified file belongs, use the
To clarify, consider the following code snippet:
To get the project module to which the specified PSI element belongs, use the
Accessing Module Roots
Information about module roots can be accessed via
ModuleRootManager. For example, the following snippet shows how to access the content roots of a module:
Checking Belonging to a Module Source Root
To check if a virtual file or directory belongs to a module source root, use the
ProjectFileIndex.getSourceRootForFile() method. This method returns
null if the file or directory does not belong to any source root of modules in the project.
Receiving Notifications About Module Changes
To receive notifications about module changes (modules being added, removed or renamed), use the message bus and the
If targeting 2019.3 or later, declarative registration is available as well.