Library
A library is an archive of compiled code (such as JAR files) that modules depend on.
A particular type of programmatically defined libraries is Predefined Libraries.
Library Types
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 .iml file.
Project Library
The library classes are visible within the project and the library information is recorded under .idea/libraries directory or in the project .ipr file.
Global Library
The library information is recorded in the applicationLibraries.xml file in $USER_HOME$/.IntelliJIdea/config/options directory. Global libraries are similar to project libraries, but are visible for different projects.
Working with Libraries
Package com.intellij.openapi.roots.libraries
provides functionality for working with project libraries and JAR files.
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.getLibraryTable()
, whereas the list of project-level library tables is accessed via LibraryTablesRegistrar.getLibraryTable(Project)
. Get the list of libraries in it via LibraryTable.getLibraries()
.
To get the list of all module libraries defined in a given module, use API from OrderEntryUtil
:
Getting the Library Content
Library
provides the getUrls()
method to get a list of source roots and classes the library includes.
Creating a Library
For module-level libraries, use the simplified APIs from ModuleRootModificationUtil
to add a library with a single API call. An example of using these APIs can be found in the project_model code sample.
Library Creation Steps
Get a write action
Get the library table add the library to. Use one of the following, depending on the library level:
LibraryTablesRegistrar.getInstance().getLibraryTable()
LibraryTablesRegistrar.getInstance().getLibraryTable(Project)
ModuleRootManager.getInstance(module).getModifiableModel().getModuleLibraryTable()
Create the library by calling
LibraryTable.createLibrary()
Add contents to the library (see below)
For a module-level library, commit the modifiable model returned by
ModuleRootManager.getInstance(module).getModifiableModel()
.
Adding Contents or Modifying a Library
Adding/Changing Library Roots
Get a write action
Get a modifiable model for the library, using
Library.getModifiableModel()
Use methods such as
Library.ModifiableModel.addRoot()
to perform the necessary changesCommit the model using
Library.ModifiableModel.commit()
.
Adding a Library Dependency to a Module
Use ModuleRootModificationUtil.addDependency(Module, Library)
from under a write action.
Checking Belonging to a Library
The ProjectFileIndex
interface implements a number of methods that can used to check whether the specified file belongs to the project library classes or library sources.
To check if a specified virtual file is a compiled class file use
ProjectFileIndex.isLibraryClassFile(virtualFile)To check if a specified virtual file or directory belongs to library classes use
ProjectFileIndex.isInLibraryClasses(virtualFileorDirectory)To check if the specified virtual file or directory belongs to library sources use
ProjectFileIndex.isInLibrarySource(virtualFileorDirectory)
See the project_model sample plugin to see how the method mentioned above can be applied.
More details on libraries can be found in the plugin_model code sample.
Predefined Libraries
AdditionalLibraryRootsProvider
registered in com.intellij.additionalLibraryRootsProvider
extension point allows providing synthetic/predefined libraries (SyntheticLibrary
) in a project without exposing them in the model. By default, they're also hidden from UI.