About
Working with Modules in a plugin
Articles Related
API
The IntelliJ Platform provides a number of Java classes and interfaces you can use to work with modules within the project and core API
Class | Class Type | API / Model |
---|---|---|
ModuleManager | Abstract class | Project |
Module | Interface | Core |
ModuleRootManager | Abstract class | Project |
ModuleRootModel | Interface | Project |
ModuleUtil | Utility | Lang |
ModifiableModuleModel | Interface | Project |
ModifiableRootModel | Interface | Project |
Module Manager: Manage the Module (list of modules, …)
Use the ModuleManager.getModules() method.
Module: Dependencies and classpath of a module
Order entries: To explore the module dependencies, use the OrderEnumerator class.
The following code snippet illustrates how you can get classpath (classes root of all dependencies) for a module:
VirtualFile[] roots = ModuleRootManager.getInstance(module).orderEntries().classes().getRoots();
ModuleRootManager: SDK
Use the ModuleRootManager.getSdk() method. This method returns a value of the Sdk type.
ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);
Sdk SDK = moduleRootManager.getSdk();
String jdkInfo = "Module: " + module.getName() + " SDK: " + SDK.getName() + " SDK version: "
+ SDK.getVersionString() + " SDK home directory: " + SDK.getHomePath();
ModuleRootManager: Module dependency between Module
Use the 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:
ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);
Module[] dependentModules = moduleRootManager.getDependencies();
String[] dependentModulesNames = moduleRootManager.getDependencyModuleNames();
See also: Dependency
Use the ModuleManager.getModuleDependentModules(module) method to 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:
boolean isDependent = ModuleManager.getInstance(project).isModuleDependent(module1,module2);
ModuleUtil: To get a module to which the specified file or PSI element belongs
- File: To get the project module to which the specified file belongs, use the ModuleUtil.findModuleForFile() static method.
java String pathToFile = "C:\\users\\firstName.LastName\\plugins\\myPlugin\src\MyAction.java";
VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByPath(pathToFile);
Module module = ModuleUtil.findModuleForFile(virtualFile,myProject);
String moduleName = module == null ? "Module not found" : module.getName();
- psi To get the project module to which the specified PSI element belongs, use the ModuleUtil.findModuleForPsiElement(psiElement) method.
Library (Order Entry) How do I work with libraries available within a module?
To get the list of libraries available within a module, use OrderEnumerator.forEachLibrary method. IDEA Plugin Dev - Order Entry (SDK, Library, Module dependencies)
// This sample code outputs a list of libraries for the module module.
final List<String> libraryNames = new ArrayList<String>();
ModuleRootManager.getInstance(module).orderEntries().forEachLibrary(new Processor<Library>() {
@Override
public boolean process(Library library) {
libraryNames.add(library.getName());
return true;
}
});
Messages.showInfoMessage(StringUtil.join(libraryNames, "\n"), "Libraries in Module");