IntelliJ IDEA 2019.2 Help

Module dependencies

Modules can depend on SDKs, JAR files (libraries) or other modules within a project. When you compile or run your code, the list of module dependencies is used to form the classpath for the compiler or the JVM.

Add a new dependency

  1. From the main menu, select File | Project Structure Ctrl+Shift+Alt+S and click Modules | Dependencies.

  2. Click the Add button Alt+Insert and select a dependency type:

    • JARs or directories: select a Java archive or a directory from files on your computer.

    • Library: select an existing library or create a new one and then add it to the list of dependencies.

    • Module Dependency: select another module in the project.

    Adding a new dependency

Remove a dependency

  • To remove a dependency, select it and then click the Remove button or press Alt+Delete.

Before removing a dependency, make sure that it is not used in other modules in the project. To do so, select the necessary dependency and press Alt+F7. You can also use the Find Usages option of the context menu.

Specify a dependency scope

Specifying a dependency scope allows you to control at which step of the build the dependency should be used.

  1. From the main menu, select File | Project Structure Ctrl+Shift+Alt+S and click Modules | Dependencies.

  2. Select the necessary scope from the list in the Scope column:

    • Compile: required to build, test, and run a project (the default scope).

    • Test: required to compile and run unit tests.

    • Runtime: art of the classpath to test and run a project.

    • Provided: used for building and testing a project.

IntelliJ IDEA processes dependencies for test sources differently from other build tools (for example, Gradle and Maven).

If your module (say, module A) depends on another module (module B), IntelliJ IDEA assumes that the test sources in A depend not only on the sources in B but also on its own test sources. Consequently, the test sources of B are also included in the corresponding classpaths.

Analyze dependencies

If you want to check whether a dependency still exists in your project, and find its exact usages, you can run dependency analysis:

  1. From the main menu, select File | Project Structure Ctrl+Shift+Alt+S and click Modules | Dependencies.

  2. Right-click the necessary dependency and select Analyze This Dependency.

You can analyze several dependencies one by one without closing the dialog. The result of each analysis will be opened in a separate tab of the Dependency Viewer tool window. After you analyze all necessary dependencies, you can close the Project Structure dialog and view the results.

If IntelliJ IDEA finds no dependency usages in the project, you will be prompted to remove this dependency.

Results of the dependencies analysis in the Dependency Viewer tool window

Sort dependencies

The order of dependencies is important as IntelliJ IDEA will process them in the same order as they are specified in the list.

During compilation, the order of dependencies defines the order in which the compiler (javac) looks for classes to resolve the corresponding references. At runtime, this list defines the order in which the JVM searches for the classes.

You can sort dependencies by their names and scopes. You can also use the Move up button and the Move down button to move the items up and down the list.

Last modified: 17 October 2019