mirror of
https://github.com/JetBrains/intellij-sdk-code-samples.git
synced 2025-07-30 18:27:49 +08:00
Fleshed out "Library" page
This commit is contained in:
parent
e302f8b460
commit
11453efa12
@ -18,10 +18,10 @@ Package
|
||||
[libraries](upsource:///platform/projectModel-api/src/com/intellij/openapi/roots/libraries)
|
||||
provides functionality for working with project libraries and jars.
|
||||
|
||||
### How do I get a list of libraries available within a module?
|
||||
### How do I get a list of libraries a module depends on?
|
||||
|
||||
To get the list of libraries, use `OrderEnumerator.forEachLibrary` method.
|
||||
To clarify this, consider the following code snippet that illustrates how to output the list of libraries for the specified module:
|
||||
To get the list of libraries that a module depends on, use `OrderEnumerator.forEachLibrary` method.
|
||||
The following snippet demonstrates how you can do this:
|
||||
|
||||
```java
|
||||
final List<String> libraryNames = new ArrayList<String>();
|
||||
@ -32,7 +32,19 @@ ModuleRootManager.getInstance(module).orderEntries().forEachLibrary(library -> {
|
||||
Messages.showInfoMessage(StringUtil.join(libraryNames, "\n"), "Libraries in Module");
|
||||
```
|
||||
|
||||
This sample code outputs a list of libraries for the `module` module.
|
||||
This sample code outputs a list of libraries that the `module` module depends on.
|
||||
|
||||
### How do I get a list of all libraries?
|
||||
|
||||
To manage the lists of application and project libraries, the [LibraryTable](upsource:///platform/projectModel-api/src/com/intellij/openapi/roots/libraries/LibraryTable.java)
|
||||
class is used. 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(Project)`.
|
||||
Once you have a `LibraryTable`, you can get the libraries in it by calling `LibraryTable.getLibraries()`.
|
||||
|
||||
To get the list of all module libraries defined in a given module, use the following API:
|
||||
```java
|
||||
OrderEntryUtil.getModuleLibraries(ModuleRootManager.getInstance(module));
|
||||
```
|
||||
|
||||
### How do I get the library content?
|
||||
|
||||
@ -53,6 +65,32 @@ Messages.showInfoMessage(roots.toString(), "Library Info");
|
||||
|
||||
In this sample code, `lib` is of the [Library](upsource:///platform/projectModel-api/src/com/intellij/openapi/roots/libraries/Library.java) type.
|
||||
|
||||
### How do I create a library?
|
||||
|
||||
To create a library, you need to perform the following steps:
|
||||
|
||||
* Obtain the library table to which you want to add the library. 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()`.
|
||||
|
||||
For module-level libraries, you can also use simplified APIs in the [ModuleRootModificationUtil](upsource:///platform/projectModel-api/src/com/intellij/openapi/roots/ModuleRootModificationUtil.java)
|
||||
class to add a library with a single API call. You can find an example of using these APIs in the [sample plugin](https://github.com/JetBrains/intellij-sdk-docs/blob/master/code_samples/project_model/src/com/intellij/tutorials/project/model/ModificationAction.java).
|
||||
|
||||
### How do I add contents to a library or modify it?
|
||||
|
||||
To add or change the roots of a library, you need to perform the following steps:
|
||||
|
||||
* Get a **modifiable model** for the library, using `Library.getModifiableModel()`
|
||||
* Use methods such as `Library.ModifiableModel.addRoot()` to perform the necessary changes
|
||||
* Commit the model using `Library.ModifiableModel.commit()`.
|
||||
|
||||
### How do I add a library dependency to a module?
|
||||
|
||||
Use `ModuleRootModificationUtil.addDependency(module, library)`.
|
||||
|
||||
### Checking Belonging to a Library
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user