IntelliJ IDEA 2017.1 Help

Working with module dependencies

What are module dependencies?

Module dependencies are the things that a module uses and, consequently, depends on.

The module dependencies may include:

  • An SDK which, if present in module dependencies, is referred to as the module SDK.
  • Libraries: collections of archives (JARs) and directories with class files, sources and documentation.
  • Other modules belonging to the same project.

When you compile or run your code, the module dependencies are used to form the classpath for the compiler or JVM. (Native Library Locations, if any, are added to java.library.path rather than the classpath.)

Where do I manage module dependencies?

Most of the tasks related to working with module dependencies are performed in the Project Structure dialog (File | Project Structure): select Modules, select the module of interest, and then select Dependencies.


Use the following icons and context menu commands:


Adding dependencies

Next to the list of dependencies, click /help/img/idea/2017.1/add.png and select:

  • JARs or directories. In the dialog that opens, select the files and folders to be added to the dependencies. (For each selected file and folder, an unnamed module library is created.)
  • Library. In the dialog that opens, select one or more libraries and click Add Selected. (You can also create a new library: New Library and then add it to the dependencies: Add Selected).
  • Module Dependency. In the dialog that opens, select the modules to be added to the dependencies.

See also, Adding a global or project library to module dependencies.

Sorting the list of dependencies

You can sort the dependencies by their names (the central column) and scopes by clicking the cells in the header row.

  • First click. The list is sorted by the corresponding column in the ascending order. The sorting marker appears in the cell: /help/img/idea/2017.1/sortMarkerAsc.png.
  • Second click. The order changes to descending; the sorting marker changes to /help/img/idea/2017.1/sortMarkerDesc.png.
  • Third click. The initial unsorted state is restored.

When the list is sorted, you cannot change the order of dependencies.

Order of dependencies

The module dependencies are processed in the order they appear in the corresponding list.

At compile time, the order of items in the list defines the order in which the compiler (javac) looks for the classes to resolve the corresponding references. At runtime, this list defines the order in which the JVM searches for the classes to be loaded.

Using library patches. If you want to use patches to certain libraries, these patches should be placed in the list before the main libraries. Otherwise, new versions of the classes (contained in the patches) will be ignored.

Classpath, bootclasspath and the JDK position. For compilation, the positions of items in the list in relation to the module JDK may be important:

  • The items that appear after the JDK are translated into the module classpath entries. Most of the libraries that you normally deal with should go there.
  • The items that appear before the JDK correspond to the module bootclasspath entries.

    For compilation to succeed, certain libraries (e.g. ones having to do with the JDK itself and its extensions) must be included in the module bootclasspath. So, such libraries must appear in the list before the JDK.

    The use of such libraries, however, may be necessary in a very limited number of special cases. Thus, for most of your modules, the JDK will (and should) be the first item in the list of dependencies.

Controlling compilation and runtime classpaths for sources and tests

You can specify whether a dependency should be included in the classpath 1) when compiling the module sources 2) when compiling the module test sources 3) when running the compiled sources 4) when running the tests. You do that by selecting one of the following options for the dependency scope (the Scope column on the Dependencies tab):

  • Compile. The dependency is included in the classpath for your sources and test sources at the compilation and run phases.
  • Test. The dependency is included in the classpath only when compiling and running your test sources.
  • Runtime. The dependency is included in the classpath only when running your sources and test sources.
  • Provided. For your sources, the dependency is included in the classpath only at the compilation phase. This is useful when there is a container (e.g. a web container) that provides the corresponding dependency at runtime.

    For your test sources, the dependency is included in the classpath both at the build and run phases.

    Application server libraries, normally, are included in the dependency lists with the scope Provided.

Processing dependencies for test sources. Note that IntelliJ IDEA is a bit different from some other build tools (e.g. Gradle and Maven) in the way it processes dependencies for test sources.

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 test sources. Consequently, the test sources of B are also included in corresponding classpaths.

Exporting dependencies to compilation classpaths of other modules

Say, you have a module (module A) that depends on another module (module B).

At runtime, all the dependencies of B are included in the classpath of A.

As for the compilation classpath of A, you have two choices for each of the dependencies of B. (For illustrating, let's assume that B has a library L in its dependencies.)

  • L is included in the classpath of A if, in the dependencies of B, it has the Export option on.
  • L is not included in the classpath of A if the L's Export option is off.

To turn the Export option on and off, use the check boxes in the Export column on the Dependencies tab.

See Also

Last modified: 18 July 2017