How to add library to intellij idea
How to add library to intellij idea
Libraries
A library is a collection of compiled code that you can add to your project. 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).
This information is valid for projects that are built with the native IntelliJ IDEA builder. If you’re using a build tool, such as Maven or Gradle, make all changes using the build file.
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.
You can also create a new library from the JAR files located within a project content root. Select these files in the Project tool window, and then select Add as Library from the context menu.
Define a global library
Click and select one of the following:
Select Java or Kotlin/JS to add a library from the files located on your computer.
Define a project library
Click and select one of the following:
Select Java or Kotlin/JS to add a library from the files located on your computer.
Add a library to module dependencies
Global and project libraries are not available until you add them to module dependencies.
In the dialog that opens, select a project or a global library that you want to add to the module.
Alternatively, click New Library and select how do you want to add a new library: you can add a Java and Kotlin libraries from files on your computer, or download a library from Maven.
Download a library from Maven
In the next dialog, specify the library artifact (for example, org.jetbrains:annotations:16.0.2 ). If you don’t know its exact name, enter the key words and click .
You can also specify another library location, and select whether you want to download transitive dependencies, source files, Javadoc files, or annotations.
IntelliJ IDEA will download the library from Maven or Nexus public repositories. You can also configure a custom remote repository.
Include specific transitive dependencies
If you want to include only specific transitive dependencies, you can use the library properties editor.
Select the necessary Maven library and click .
Change the library level
Move a library to a higher 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, use this procedure if you want to use a module library across the project or the entire IDE.
From the main menu, select File | Project Structure Ctrl+Alt+Shift+S and locate the library that you want to change.
Copy a library to a lower level
You can create a copy of a library on a lower level. For example, use this procedure if you want to add more classes to a project library, but you want to use them in one module only.
From the main menu, select File | Project Structure Ctrl+Alt+Shift+S and locate the library that you want to change.
Exclude library items
IntelliJ IDEA allows you to temporarily exclude library items in order to increase IDE performance. You can exclude folders, archives (for example, 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, and so on. 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
Click and select the library items that you want to exclude.
Exclude items from a module library
Click and select the library items that you want to exclude.
Excluded items will be marked with the icon. To return library items to their original state, remove the excluded items.
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.
Click Add in the corresponding dialog section, and specify the repository URL.
Configure library documentation
You can add library documentation to IntelliJ IDEA so that you can get information about symbols and method signatures right from the editor in the Quick Documentation popup.
You can also configure external documentation by specifying the path to the reference information online. External documentation opens the necessary information in a browser so that you can navigate to related symbols and keep the information for further reference at the same time.
If you’re downloading a library from Maven, select the JavaDoc checkbox to get the library documentation together with the library code.
Specify library documentation paths
To view external library documentation, configure the documentation URL first.
Select the necessary library, click the icon and enter the external documentation URL.
Apply the changes and close the dialog.
Add library documentation to your project
You can add downloaded documentation to your project to be able to access it offline.
Select the library for which you want to add the documentation and click in the right section of the dialog.
Apply the changes and close the dialog.
When the documentation is configured, you can open it in the editor.
Add items to your project
Once you have created a project, you can start adding new items: create directories and packages, add new classes, import resources, and extend your project by adding more modules.
Create new items
Create a new directory
Create a new package
Packages in Java are used for grouping classes that belong to the same category or provide similar functionality, for structuring and organizing large applications with hundreds of classes.
Write package names in lowercase letters. There are some other naming conventions for packages in Java that you should follow.
Create a new empty file
If the extension you have specified is not associated with any of the file types recognized by IntelliJ IDEA, the Register New File Type Association dialog is displayed. In this dialog, you can associate the extension with one of the recognized file types.
Create a new Java class
Follow the Java naming convention as you create new classes.
Together with the file, IntelliJ IDEA automatically generates the class declaration.
This is done by means of file templates. Depending on the type of the file that you create, the IDE inserts initial code and formatting that is expected to be in all files of that type. For more information on how to use and configure templates, refer to File templates.
Create a new module
Modules allow you to combine several technologies and frameworks in one application. In IntelliJ IDEA, you can create several modules in one project and each of them can be responsible for its own framework.
Select the top-level directory in the Project tool window and press Alt+Insert or select New | Module from the context menu.
The New Module wizard opens.
From the list on the left, select a module type. Name the new module.
From the Language list, select the language that you want to use in your application.
If you want to use a language that is not available in IntelliJ IDEA out of the box (for example, Python or PHP), click the button and select the necessary option.
The IDE will open a dialog in which you can select and install the necessary language plugin. After that, you can close the dialog and keep configuring the new module.
Select the build system that you want to use in your project: the native IntelliJ builder, Maven, or Gradle.
For Gradle, you will also need to select a language for the build script: Groovy or Kotlin.
Select a JDK that you want to use from the JDK list. You can use the project SDK or specify a new one.
For more information on modules in IntelliJ IDEA, refer to Modules.
Import items
Import files
You can import files to your project using any of the following ways:
Drag the file from your system file manager to the necessary node in the Project tool window.
Manually move the file to the project folder in your system file manager.
Example: Import an image
Copy the file in the file manager and then paste in to the folder with resource files in the IDE Project tool window.
In the class in which you want to use the image, place the caret at the necessary line and press Ctrl+V to paste the path to the image.
Run the class to make sure that the image is inserted correctly.
Import an existing module
By doing so, you are attaching another module to the project without physically moving any files. If you don’t need the modules to be located in one folder, the module import is finished, and you can start working with the project normally.
If you want the modules in the same folder, in the Project tool window, drag the imported module to the top-level directory. In this case, the contents of the imported module will be physically transferred to your project’s folder.
For more information on modules in IntelliJ IDEA, refer to Modules.
Как добавить библиотеку (.jar файл) в проект Intellij IDEA
Добавляем внешний jar в проект Intellij IDEA
Если вкратце, то вам нужно добавить необходимую библиотеку в модуле зависимостей структуры проекта. А теперь подробнее:
File > Project Structure…
Project Settings > Modules > Dependencies > знак ‘+’ > JARs or directories…
И еще раз ОК
Выбор jar > OK > OK
4. Найти только что добавленный jar файл можно в открывающемся списке External Libraries вашего проекта Intellij IDEA.
Стоит заметить, что данные скриншоты подходят для версий Intellij IDEA 14 и 15, в других версиях среды разработки элементы и пункты могут отличаться.
Или смотрите видео-версию урока
Library
A library is an archive of compiled code (such as JAR files) that modules depend on.
The IntelliJ Platform supports three types of libraries:
For more information about libraries, refer to Libraries.
A particular type of programmatically defined libraries is Predefined Libraries.
Accessing Libraries and Jars
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 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 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:
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 LibraryTable.createLibrary()
Add contents to the library (see below)
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 modifiable model for the library, using Library.getModifiableModel()
Use methods such as Library.ModifiableModel.addRoot() to perform the necessary changes
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 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 use
To check if a specified virtual file or directory belongs to library classes use
To check if the specified virtual file or directory belongs to library sources use
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.
Predefined Libraries
AdditionalLibraryRootsProvider Allows providing synthetic/predefined libraries ( SyntheticLibrary ) in a project without exposing them in the model. By default, they’re also hidden from UI.
Modules
In IntelliJ IDEA, a module is an essential part of any project – it’s created automatically together with a project. Projects can contain multiple modules – you can add new modules, group them, and unload the modules you don’t need at the moment.
Modules allow you to combine several technologies and frameworks in one application. In IntelliJ IDEA, you can create several modules for a project and each of them can be responsible for its own framework. For more information, refer to Add frameworks (facets).
For more information on how modules are used in projects, refer to Configure projects.
IntelliJ IDEA modules vs Java modules
For more information on Java 9 support in IntelliJ IDEA refer to the Support for Java 9 Modules in IntelliJ IDEA 2017.1 and Java 9 and IntelliJ IDEA blog posts.
Projects with multiple modules
IntelliJ IDEA allows you to have many modules in one project, and they shouldn’t be just Java. You can have one module for a Java application and another module for a Ruby on Rails application or for any other supported technology.
An application that consists of a client side and a server side is a good example a two-module project.
Add a new module to your project
Select the top-level directory in the Project tool window and press Alt+Insert or select New | Module from the context menu.
The New Module wizard opens.
From the list on the left, select a module type. Name the new module.
From the Language list, select the language that you want to use in your application.
If you want to use a language that is not available in IntelliJ IDEA out of the box (for example, Python or PHP), click the button and select the necessary option.
The IDE will open a dialog in which you can select and install the necessary language plugin. After that, you can close the dialog and keep configuring the new module.
Select the build system that you want to use in your project: the native IntelliJ builder, Maven, or Gradle.
For Gradle, you will also need to select a language for the build script: Groovy or Kotlin.
Select a JDK that you want to use from the JDK list. You can use the project SDK or specify a new one.
Import an existing module
By doing so, you are attaching another module to the project without physically moving any files. If you don’t need the modules to be located in one folder, the module import is finished, and you can start working with the project normally.
If you want the modules in the same folder, in the Project tool window, drag the imported module to the top-level directory. In this case, the contents of the imported module will be physically transferred to your project’s folder.
For information on how to attach a Maven or Gradle project to your current project, refer to Link a Gradle project to an IntelliJ IDEA project and Link and unlink a Maven project.
Import a module from existing sources
Use these steps to import a project as a module if the project comes from an external model or if you want to create a module from the existing source code that is not necessarily an exported project.
In the dialog that opens, select Create module from existing sources if you want to create a new module from the existing source code.
Group modules
In IntelliJ IDEA, you can logically group modules. If you have a large project with multiple modules, grouping will make it easier to navigate through your project. Module groups can be nested: a group can contain other subgroups.
Create a new module group (deprecated)
In earlier versions (2017.2 and earlier), IntelliJ IDEA used explicit groups for joining modules together. If you’ve configured manual module groups, you will be able to continue working with them in later versions of the IDE. Alternatively, you can convert module groups and use qualified names instead.
In the Project tool window ( Alt+1 ), select the modules that you want to group.
You can also do so on the Modules page of the Project Structure dialog ( Ctrl+Alt+Shift+S ).
The new group is now created and is marked with the icon.
Select Outside Any Group to exclude the selected module from the group, To This Group to add the module to the group, or To New Subgroup to create a new group in another group.
Convert module groups to qualified names (deprecated)
In the next dialog, review the new module names and adjust them if necessary.
Apply the changes and close the dialog.
Group modules by fully qualified names
IntelliJ IDEA 2017.3 and later uses fully qualified names to group modules. For example, if you want to group all CDI modules, add the cdi prefix to their names.
Specify a prefix and apply the changes.
To view all modules on the same level in the Project Structure dialog, use the Flatten Modules context menu option.
Module groups won’t be visible in the Project tool window ( Alt+1 ) if the Flatten Modules option is enabled there. You can disable it via the button in the tool window header.