What is the difference between "Module Dependencies" and "Libraries" in IntelliJ IDEA?

21,526

Solution 1

Module dependencies are classes, archives, libraries and resources that your module files references. While a library is a set of class files stored in an archive or directory.

Export check means if checked then this library will be implicitly added to the other module that references this one.

To create a .jar file you need create an artifact. Artifact is a placeholder of the building output. There's predefined templates for creating .jar, .war, .ear archives. You can choose jar to build a jar artifact. By default it's defined empty and you need to define content of the artifact. You can drag-n-drop compiled output to it but don't do it with library archives. Because libraries in this case will be packaged inside the .jar file and you will be required to create a separate classloader to load them before your application start. Instead you change the artifact type to Other and drag .jar and dependent libraries into output root. This way library archives will be copied along with created .jar. You also need to create a MANIFEST.MF and specify Class-Path there for dependent libraries. All files will be stored in the directory you specify for building the artifact. You can build it using Build Artifact menu.

Solution 2

If your project contains multiple modules, "module dependency" defines dependencies between these modules, but libraries are compiled classes (usually jar files, optionaly containing theirs sources and javadocs) that are used by your module.

Each module can have its own libraries and artifacts (for example a result jar file), and can depend on other modules without circular dependency.

Solution 3

"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)."

  • Global library is set via Project Structure\Platform Settings\Global Libraries
  • Project library is set via Project Structure\Project Settings\Libraries
  • Module library is set via Project Structure\Project Settings\Modules\Dependencies

Solution 4

Module Dependencies tab can contain Libraries, Export means that a library from the module will be also available to another module that depends on this module.

Final jar with all the dependencies can be created using Artifacts.

Share:
21,526
moorara
Author by

moorara

Updated on April 26, 2020

Comments

  • moorara
    moorara about 4 years

    What is the difference between "Module Dependencies" and "Libraries" in IntelliJ IDEA when you want to add a .jar library to your project? Also, What is the "Export" check box when you are adding your .jar library to the "Module Dependencies" in IntelliJ IDEA?

    In each of these ways, how are the classes and code inside the included .jar library integrated into your final project (code) when creating the newly generated .jar file?

  • Eran H.
    Eran H. almost 9 years
    This post helped me alot , but you need to update it. IntelliJ today has an option to automatically copy all the jar files AND link your Manifest to the jar files in the Class-Path . To do so: Choose JAR and not Other when creating an artifact, then in "JAR files from libraries" choose "copy to the output directory and link via manifest"
  • alife
    alife almost 4 years
    This answer, and ONLY this answer, properly addresses the question. +1
  • Johnson
    Johnson almost 4 years
    This answer doesn't address the Export function. But I agree, from the question title leading me here, this answer offers the knowledge I was looking for. While the other answers consider the general meaning of the terms "module dependencies" and "library", this answer considers their meaning in the UI of IntelliJ (Project Structure > Modules > Dependencies vs. Project Structure > Libraries).