Working with Modules in a plugin
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 |
Use the ModuleManager.getModules() method.
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();
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();
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);
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();
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");