A library is an archive of compiled code (such as JAR files) that modules depend on.
The IntelliJ Platform supports three types of libraries:
Module Library: the library classes are visible only in this module and the library information is recorded in the module
Project Library: the library classes are visible within the project and the library information is recorded under
.idea/librariesdirectory or in the project
Global Library: the library information is recorded in the
<User Home>/.IntelliJIdea/config/optionsdirectory. Global libraries are similar to project libraries, but are visible for different projects.
For more information about libraries, refer to Libraries.
A particular type of programmatically defined libraries is Predefined Libraries.
Accessing Libraries and Jars
libraries provides functionality for working with project libraries and jars.
Getting a List of Libraries a Module Depends On
To get the list of libraries that a module depends on, use
OrderEnumerator.forEachLibrary as follows.
This sample code outputs a list of libraries that the given module depends on.
Getting a List of All Libraries
To manage the lists of application and project libraries, use
LibraryTable. The list of application-level library tables is accessed by calling
LibraryTablesRegistrar.getInstance().getLibraryTable(), whereas the list of project-level library tables is accessed through
LibraryTablesRegistrar.getInstance().getLibraryTable(). Once you have a
LibraryTable, you can get the libraries in it by calling
To get the list of all module libraries defined in a given module, use the following API:
Getting the Library Content
Library provides the
getUrls() method you can use to get a list of source roots and classes the library includes. To clarify, consider the following code snippet:
Creating a Library
To create a library, perform the following steps:
Get a write action
- Obtain the library table to which you want to add the library. Use one of the following, depending on the library level:
Create the library by calling
Add contents to the library (see below)
For a module-level library, commit the modifiable model returned by
For module-level libraries, you can also use simplified APIs in the
ModuleRootModificationUtil class to add a library with a single API call. You can find an example of using these APIs in the project_model code sample.
Adding Contents or Modifying a Library
To add or change the roots of a library, you need to perform the following steps:
Get a write action
Get a modifiable model for the library, using
Use methods such as
Library.ModifiableModel.addRoot()to perform the necessary changes
Commit the model using
Adding a Library Dependency to a Module
ModuleRootModificationUtil.addDependency(module, library) from under a write action.
Checking Belonging to a Library
ProjectFileIndex interface implements a number of methods you can use to check whether the specified file belongs to the project library classes or library sources. You can use the following methods:
To check if a specified virtual file is a compiled class file useProjectFileIndex.isLibraryClassFile(virtualFile)
To check if a specified virtual file or directory belongs to library classes useProjectFileIndex.isInLibraryClasses(virtualFileorDirectory)
To check if the specified virtual file or directory belongs to library sources useProjectFileIndex.isInLibrarySource(virtualFileorDirectory)
See the project_model to see how the method mentioned above can be applied.
More details on libraries can be found in the plugin_model code sample.