基本操作

常用标签

dependencyManagement

在 Maven 多模块管理时,管理依赖关系非常重要,各种依赖包冲突,解决起来相当麻烦,这时候可以使用 dependencyManagement。

说明:使用 dependencyManagement 可以统一管理项目的版本号,确保应用中的每个项目的依赖和版本一致,不用每个模块都弄一个版本号,不利于管理,当需要变更版本号的时候只需要在父类容器里更新,如果某个子类需要另外一个特殊的版本号时,只需要在自己的模块 dependencies 中声明即可。

注意:dependencyManagement 只是声明依赖版本号,并不会引入依赖。

举例:父模块中定义 mysql 依赖版本号,子模块引入 mysql 依赖。

  • 父模块中:

    1. <dependencyManagement>
    2. <dependencies>
    3. <dependency>
    4. <groupId>mysql</groupId>
    5. <artifactId>mysql-connector-java</artifactId>
    6. <version>5.1.44</version>
    7. </dependency>
    8. </dependencies>
    9. </dependencyManagement>
  • 子模块中:

    1. <dependencies>
    2. <dependency>
    3. <groupId>mysql</groupId>
    4. <artifactId>mysql-connector-java</artifactId>
    5. </dependency>
    6. </dependencies>

    删除脚本

    ```shell @echo off rem create by NettQun

rem 这里写你的仓库路径 set REPOSITORY_PATH=D:\Java\maven-repository\maven-aliyun\repository rem 正在搜索… for /f “delims=” %%i in (‘dir /b /s “%REPOSITORY_PATH%*lastUpdated*”‘) do ( del /s /q %%i ) rem 搜索完毕 pause ```

常见错误

maven依赖导入失败问题

删除对应文件夹里的 .lastUpdated 文件。

maven resources compiler:Maven project configuration required for module ‘platform’ isn’t available,Compilation of Maven projects is supported only if external build is started from an IDE.

问题:内网机部署 Eclipse 项目时,Maven 出现上面的错误,所有的依赖都能点进去,不爆红,就是没导入进来。

找到自己的 maven 仓库,全局搜索 _remote.repositories 文件,删除掉即可。