IntelliJ IDEA 2018.2 Help

Working with libraries

A library is a collection of compiled code that a module can depend on. In IntelliJ IDEA, libraries can be defined at three levels: global (available for many projects), project (available for all modules within a project), and module (available for one module).

Define a library

After you define a library and add it to module dependencies, the IDE will be supplying its contents to you as you write your code. IntelliJ IDEA will also use the code from the libraries to build and deploy your application.

In IntelliJ IDEA, libraries can be defined in the Project Structure dialog. Go to File | Project Structure, or press Ctrl+Shift+Alt+S.

  • To define a global library, select Global Libraries under the Platform Settings section.

  • To define a project library, select Libraries under the Project Settings section.

    Click artwork studio icons common add, select a library type (Java or Kotlin/JS), and then select files on the disk from which you want to create a library.

  • To define a module library:
    1. Select Modules under the Project Settings section.

    2. Open the Dependencies tab.

    3. Click artwork studio icons common add and select Library.

    4. Click New Library at the bottom of the dialog.

      You can create a new library from files on your computer, or download a library from a repository.

Add a library to module dependencies

Global and project libraries are not available until you add them to module dependencies:

  1. Open the Project Structure dialog (Ctrl+Shift+Alt+S) and select Modules.

  2. Open the Dependencies tab and click artwork studio icons common add.

  3. Select a library you want to add and click Add Selected at the bottom of the dialog.

Download a library from Maven

  1. Press Ctrl+Shift+Alt+S to open the Project Structure dialog, and click Libraries.

  2. Click icons general add svg, and select From Maven.

  3. In the next dialog, specify the library artifact (for example, org.jetbrains:annotations:16.0.2), or enter key words and click artwork studio icons common search.

    You can also specify another library location, and select whether you want to download transitive dependencies, source and JavaDoc files.

    projects libraries

IntelliJ IDEA will download the library from Maven or Nexus public repositories. You can also configure the custom remote repository.

Configure a custom remote repository

You can view the full list of remote repositories and add a custom repository in the settings. Note that IntelliJ IDEA can load libraries from Maven even if you don't use Maven as a build tool for your project.

  1. In the Settings/Preferences dialog (Ctrl+Alt+S), go to Build, Execution, Deployment | Remote Jar Repositories.

  2. Click Add in the corresponding dialog section, and specify the repository URL.

    remote jar

Include specific transitive dependencies

If you want to include only specific transitive dependencies, you can use the library properties editor.

  1. Press Ctrl+Shift+Alt+S to open the Project Structure dialog, and go to Modules | Dependencies.

  2. Select the necessary Maven library and click icons actions edit svg.

  3. In the next dialog, click Edit, and then click Configure next to the Include transitive dependencies option.

  4. Select the dependencies you want to include in the library and click OK.

Exclude library items

IntelliJ IDEA allows you to temporarily exclude library items in order to increase IDE performance. You can exclude folders, archives (e.g. JARs) and folders within archives.

Classes from excluded packages won't be shown in code completion suggestion lists, references to such classes will be shown in the editor as unresolved, etc. However, when you compile or run your code, a library will still be used as a whole, irrespective of whether there are excluded items in that library or not.

Exclude items from a project or a global library

  1. Press Ctrl+Shift+Alt+S to open the Project Structure dialog, and click Libraries.

  2. Click icons modules addExcludedRoot svg, and select the library items that you want to exclude.

Exclude items from a module library

  1. Press Ctrl+Shift+Alt+S to open the Project Structure dialog, and go to Modules | Dependencies.

  2. Click icons modules addExcludedRoot svg, and select the library items that you want to exclude.

Excluded items will be marked with the icons modules excludeRoot icon. To return library items to their original state, remove the excluded items.

Change library level

In IntelliJ IDEA, you can move a project or a module library to a higher level. This is helpful if you want to extend its scope of usage. For example, if you want to use a module library across the project or the entire IDE. To move a library to a higher level, right-click this library in the Project Structure dialog, and select Move to Project Libraries or Move to Global Libraries.

You can also create a copy of a library on a lower level. For example, if you want to add more classes to a project library, but you want to use them in one module only.

Right-click the necessary global library and select Copy to Project Libraries. Then specify if you also want a copy of the library files and where that copy should be created. For a global or project library included in the dependencies of a module, to start creating a copy at the module level, right-click the required library and select Copy to Module Libraries.

Last modified: 20 November 2018

See Also