📌 目录:

  1. Maven 引入外部依赖概述
  2. 如何在 pom.xml 中添加依赖
  3. 依赖的作用范围
  4. Maven 依赖管理
  5. 使用 Maven 中央仓库
  6. 参考资料

1. Maven 引入外部依赖概述

在 Maven 项目中,引入外部依赖是构建过程中最常见的需求之一。Maven 通过 pom.xml 文件中的 <dependencies> 节点来管理外部依赖。使用外部依赖时,Maven 会自动从中央仓库或其他仓库下载所需的 JAR 文件并将其添加到项目的构建路径中。

外部依赖的好处:

  • 自动管理依赖:Maven 自动下载和管理项目的所有依赖,减少手动管理 JAR 文件的麻烦。
  • 版本控制:可以方便地管理依赖的版本,确保项目的一致性和可维护性。
  • 提高开发效率:通过中央仓库,可以轻松引入开源项目或第三方库。

2. 如何在 pom.xml 中添加依赖

步骤一:查找依赖信息

每个 Maven 依赖都包含三个关键元素:

  • groupId:定义了项目或库的组(如公司名或组织名)。
  • artifactId:定义了库的名称。
  • version:定义了该库的版本。

你可以在 Maven 中央仓库、第三方仓库或项目的文档中找到这些信息。一个典型的依赖声明如下:

<dependencies>
    <!-- 添加 JUnit 依赖 -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
</dependencies>

步骤二:添加到 pom.xml

pom.xml 文件中的 <dependencies> 节点下添加外部依赖,例如上面的 JUnit 依赖:

<dependencies>
    <!-- JUnit 测试框架依赖 -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>

    <!-- 其他外部依赖 -->
</dependencies>


3. 依赖的作用范围

Maven 支持多种作用范围(scope),不同的作用范围会影响依赖的使用方式和生命周期。常见的作用范围有:

1. compile(默认作用范围)

  • 适用于编译、测试和运行阶段,任何阶段都可以使用该依赖。
  • 默认的作用范围,如果不指定 scope,则会使用该作用范围。
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>

2. test

  • 仅在测试时使用,项目编译时包含该依赖,但不会在打包时包含。
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>

3. provided

  • 适用于容器提供的库(如 Servlet API)。在编译和测试时可用,但不会打包到最终的 JAR 或 WAR 文件中。
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version>
    <scope>provided</scope>
</dependency>

4. runtime

  • 仅在运行时需要的依赖,编译时不会使用,但在运行时需要。
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.2.19</version>
    <scope>runtime</scope>
</dependency>

5. system

  • 适用于本地的 JAR 文件依赖。此依赖需要指定本地文件路径。
<dependency>
    <groupId>com.example</groupId>
    <artifactId>example-lib</artifactId>
    <version>1.0</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/libs/example-lib-1.0.jar</systemPath>
</dependency>

6. import

  • 用于在 pom.xml 中导入 BOM(Bill of Materials)依赖,这样可以导入一组版本约定的依赖。
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-framework-bom</artifactId>
            <version>5.3.9</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>
    </dependencies>
</dependencyManagement>


4. Maven 依赖管理

依赖传递

Maven 支持依赖传递(Transitive Dependency)。这意味着,如果你的项目依赖 A,而 A 又依赖 B,Maven 会自动将 B 也引入到项目中。

依赖冲突

当不同的依赖版本冲突时,Maven 会采用最近版本的依赖。例如,如果项目 A 依赖 commons-lang3:3.12,项目 B 依赖 commons-lang3:3.11,而项目 C 依赖项目 A 和项目 B,Maven 会自动选择其中一个版本。

你可以通过 dependencyManagement 来明确指定冲突依赖的版本。

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12</version>
        </dependency>
    </dependencies>
</dependencyManagement>

排除依赖

如果你不需要某个传递依赖,可以通过 <exclusions> 排除它。

<dependency>
    <groupId>com.example</groupId>
    <artifactId>example-lib</artifactId>
    <version>1.0</version>
    <exclusions>
        <exclusion>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>


5. 使用 Maven 中央仓库

Maven 提供了一个中央仓库(Central Repository),可以方便地下载公开的开源库。大部分常用的依赖库都托管在中央仓库中,因此你无需手动下载和管理 JAR 文件。

如何查找依赖

你可以在 Maven 官方中央仓库网站(Maven Central)中查找需要的依赖。只需输入依赖的名称、版本等信息,即可找到对应的 groupIdartifactIdversion,然后将它们添加到 pom.xml 文件中。


6. 参考资料

🔗 出站链接

🔗 站内链接


通过 Maven 引入外部依赖,可以显著简化项目开发的依赖管理工作,并提高开发效率。如果有任何问题或需要进一步的帮助,请随时告诉我! 😊