# 模块依赖项

> For the complete documentation index, see [llms.txt](https://www.jetbrains.com.cn/help/idea/llms.txt).

模块可以依赖于 SDK、JAR 文件（库）或项目内的其他模块。 当您编译或运行代码时，模块依赖列表会用于为编译器或 JVM 形成  classpath 。

> **Note:**
> 此信息适用于使用原生 IntelliJ IDEA 构建器构建的项目。 如果您正在使用构建工具，例如 [Maven](maven-support.html) 或 [Gradle](gradle.html)，请使用构建文件进行所有更改。

Procedure: 添加新依赖项

1. 在主菜单中，转到  `文件 | 项目结构`  `Ctrl+Alt+Shift+S` (Windows), `⌘ ;` (macOS), `⌘ ;` (IntelliJ IDEA Classic (macOS)), `⌘ ⇧ Comma` (macOS System Shortcuts), `Ctrl+Alt+Shift+S` (XWin), `Ctrl+Alt+Shift+S` (GNOME), `Ctrl+Alt+Shift+S` (KDE), `Ctrl+Alt+Shift+S` (Emacs), `Ctrl+Alt+Shift+S` (Sublime Text), `⌘ ;` (Sublime Text (macOS)), `Ctrl+Alt+Shift+S` (Visual Studio), `⌘ ;` (Visual Studio (macOS)), `Ctrl+Alt+Shift+S` (NetBeans), `Ctrl+Alt+Shift+S` (Eclipse), `⌘ ;` (Eclipse (macOS)) 并点击  `模块 | 依赖项` 。

2. 点击 !["添加"按钮](https://resources.jetbrains.com.cn/help/img/idea/2026.3/app.expui.general.add.svg) `Alt+Insert` (Windows), `⌥ Insert` (macOS), `⌥ Insert` (IntelliJ IDEA Classic (macOS)), `⌥ Insert` (macOS System Shortcuts), `Alt+Insert` (XWin), `Alt+Insert` (GNOME), `Alt+Insert` (KDE), `Alt+Insert` (Emacs), `Alt+Insert` (Sublime Text), `⌥ Insert` (Sublime Text (macOS)), `Alt+Insert` (Visual Studio), `⌥ Insert` (Visual Studio (macOS)), `Alt+Insert` (NetBeans), `Alt+Insert` (Eclipse), `⌥ Insert` (Eclipse (macOS)) 并选择依赖类型：

* JAR 文件或目录 ：选择计算机文件中的 Java 存档或目录。

* 库 ：选择一个现有的库或创建一个新的库，然后将其添加到依赖项列表中。

* 模块依赖性 ：在项目中选择另一个模块。

![添加新依赖](https://resources.jetbrains.com.cn/help/img/idea/2026.3/add-dependency.png)

Procedure: 移除依赖

在删除依赖项之前，请确保该依赖项未在项目中的其他模块中使用。 为此，请选择所需的依赖项，然后按 `Alt+F7` (Windows), `⌥ F7` (macOS), `⌥ F7` (IntelliJ IDEA Classic (macOS)), `⌃ U` (macOS System Shortcuts), `Alt+F7` (XWin), `Alt+Shift+7` (GNOME), `Alt+Shift+7` (KDE), `Alt+F7` (Emacs), `Alt+F7` (Sublime Text), `⌥ F7` (Sublime Text (macOS)), `Shift+F12` (Visual Studio), `⇧ F12` (Visual Studio (macOS)), `Alt+F7` (NetBeans), `Ctrl+G` (Eclipse), `⌘ ⇧ G` (Eclipse (macOS))。 您还可以使用上下文菜单的   查找用法   选项。

* 选择您要移除的依赖项，然后点击 !["移除"按钮](https://resources.jetbrains.com.cn/help/img/idea/2026.3/app.expui.general.remove.svg)或按 `Alt+Delete` (Windows), `⌘ ⌦` (macOS), `⌘ ⌦` (IntelliJ IDEA Classic (macOS)), `⌘ ⌦` (macOS System Shortcuts), `Alt+Delete` (XWin), `Alt+Delete` (GNOME), `Alt+Delete` (KDE), `Alt+Delete` (Emacs), `Alt+Delete` (Sublime Text), `⌘ ⌦` (Sublime Text (macOS)), `Ctrl+R, D` (Visual Studio), `⌘ R, D` (Visual Studio (macOS)), `Alt+Delete` (NetBeans), `Alt+Delete` (Eclipse), `Alt+Delete` (Eclipse (macOS))。

Procedure: 分析依赖性

如果您想检查项目中依赖项是否仍然存在，并找到其确切的使用情况，可以运行依赖分析：

1. 在主菜单中，转到  `文件 | 项目结构`  `Ctrl+Alt+Shift+S` (Windows), `⌘ ;` (macOS), `⌘ ;` (IntelliJ IDEA Classic (macOS)), `⌘ ⇧ Comma` (macOS System Shortcuts), `Ctrl+Alt+Shift+S` (XWin), `Ctrl+Alt+Shift+S` (GNOME), `Ctrl+Alt+Shift+S` (KDE), `Ctrl+Alt+Shift+S` (Emacs), `Ctrl+Alt+Shift+S` (Sublime Text), `⌘ ;` (Sublime Text (macOS)), `Ctrl+Alt+Shift+S` (Visual Studio), `⌘ ;` (Visual Studio (macOS)), `Ctrl+Alt+Shift+S` (NetBeans), `Ctrl+Alt+Shift+S` (Eclipse), `⌘ ;` (Eclipse (macOS)) 并点击  `模块 | 依赖项` 。

2. 右键点击所需的依赖项并选择   分析此依赖项  。

您可以在不关闭对话框的情况下逐个分析依赖项。 每次分析的结果都会在   依赖查看器  工具窗口的单独选项卡中打开。 在您分析完所有必要的依赖项后，您可以关闭   项目结构  对话框并查看结果。

如果 IntelliJ IDEA 在项目中未找到依赖项的使用情况，您将会被提示删除该依赖项。

!["Dependency Viewer 工具窗口中依赖性分析的结果"](https://resources.jetbrains.com.cn/help/img/idea/2026.3/dependencies-analysed.png)

> **Note:**
> 有关 [Maven 依赖项](work-with-maven-dependencies.html)或 [Gradle 依赖项](work-with-gradle-dependency-diagram.html)的更多信息，请参阅其相应部分。

## 配置依赖范围

Procedure: 指定依赖范围

指定依赖范围可以让您控制在构建的哪个步骤中使用依赖。 类路径在编译源代码、编译测试源代码、运行已编译源代码和运行测试时可能会有所不同。

1. 在主菜单中，转到  `文件 | 项目结构`  `Ctrl+Alt+Shift+S` (Windows), `⌘ ;` (macOS), `⌘ ;` (IntelliJ IDEA Classic (macOS)), `⌘ ⇧ Comma` (macOS System Shortcuts), `Ctrl+Alt+Shift+S` (XWin), `Ctrl+Alt+Shift+S` (GNOME), `Ctrl+Alt+Shift+S` (KDE), `Ctrl+Alt+Shift+S` (Emacs), `Ctrl+Alt+Shift+S` (Sublime Text), `⌘ ;` (Sublime Text (macOS)), `Ctrl+Alt+Shift+S` (Visual Studio), `⌘ ;` (Visual Studio (macOS)), `Ctrl+Alt+Shift+S` (NetBeans), `Ctrl+Alt+Shift+S` (Eclipse), `⌘ ;` (Eclipse (macOS)) 并点击  `模块 | 依赖项` 。

2. 从   范围  列中的列表中选择必要的范围：

* 编译 ：构建、测试和运行项目所需（默认范围）。

* 测试 ：编译和运行单元测试所需。

* 运行时 ：包含在您的源代码和测试源代码的类路径中，但仅在运行阶段。

* 已提供 ：用于构建和测试项目。

3. 选项   导出   允许您控制依赖于此模块的模块的编译类路径：标记的条目将包含在依赖模块的编译类路径中。

![配置依赖范围](https://resources.jetbrains.com.cn/help/img/idea/2026.3/module-dependencies.png)

IntelliJ IDEA 与其他构建工具（例如 Gradle 和 Maven）不同，会以不同的方式处理测试源的依赖关系。

如果您的模块（假设是模块 A）依赖于另一个模块（模块 B），IntelliJ IDEA 假设 A 中的测试源不仅依赖于 B 的源，还依赖于它自己的测试源。 因此，B 的测试源文件也包含在相应的类路径中。

下表总结了可能存在的依赖范围的类路径信息。

|  范围  |  源文件，在编译时  |  来源，当运行  |  编译测试时  |  测试，运行时  |
|  编译  |  +  |  +  |  +  |  +  |
|  测试  |  -  |  -  |  +  |  +  |
|  运行时  |  -  |  +  |  -  |  +  |
|  已提供  |  +  |  -  |  +  |  +  |

## 整理依赖项

依赖项的顺序很重要，因为 IntelliJ IDEA 会按照列表中指定的顺序处理它们。

在编译期间，依赖项的顺序决定了编译器（javac）查找类以解析相应引用的顺序。 在运行时，此列表定义了 JVM 搜索类的顺序。

要排序依赖项，请选择  `文件 | 项目结构`  `Ctrl+Alt+Shift+S` (Windows), `⌘ ;` (macOS), `⌘ ;` (IntelliJ IDEA Classic (macOS)), `⌘ ⇧ Comma` (macOS System Shortcuts), `Ctrl+Alt+Shift+S` (XWin), `Ctrl+Alt+Shift+S` (GNOME), `Ctrl+Alt+Shift+S` (KDE), `Ctrl+Alt+Shift+S` (Emacs), `Ctrl+Alt+Shift+S` (Sublime Text), `⌘ ;` (Sublime Text (macOS)), `Ctrl+Alt+Shift+S` (Visual Studio), `⌘ ;` (Visual Studio (macOS)), `Ctrl+Alt+Shift+S` (NetBeans), `Ctrl+Alt+Shift+S` (Eclipse), `⌘ ;` (Eclipse (macOS)) 并点击  `模块 | 依赖项`

您可以按照名称和范围排序依赖项。 您还可以使用 ![上移按钮](https://resources.jetbrains.com.cn/help/img/idea/2026.3/app.expui.general.moveUp.svg) 和 ![下移按钮](https://resources.jetbrains.com.cn/help/img/idea/2026.3/app.expui.general.moveDown.svg) 在列表中上下移动项目。

![排序依赖项](https://resources.jetbrains.com.cn/help/img/idea/2026.3/sort-dependencies.png)

