48. 在父项目中管理子模块项目使用的依赖
在一个项目中,如果某些依赖只是部分子模块项目需要使用的,应该将这些依赖配置在<dependencyManagement>节点中,凡配置在这个节点中的依赖,任何子模块项目中都不会直接拥有,如果某个子模块项目需要使用这些依赖,依然需要使用<dependency>节点来添加!与在子模块项目中直接添加<denpendency>(父级的<dependencyManagement>没有配置某个依赖而子模块项目中直接添加)的区别在于:如果事先使用父级项目的<dependencyManagement>进行了配置,则子模块项目在添加时,不需要指定版本号,直接使用父级项目配置的版号,以便于在父级项目中统一管理依赖的版本!
注意:在父级项目中,添加许多依赖都是不需要指定版本号的,但是,如果将这些依赖配置到<dependencyManagement>中用于指导子模块项目所使用的依赖的版本时,必须显式的指定版本号,否则,子模块项目将不明确需要使用的是哪个版本!
则在父级项目中,关于依赖的管理:
<properties> <!-- Java Version --> <java.version>1.8</java.version> <!-- Dependency Version --> <mysql.version>8.0.20</mysql.version> <mybatis.version>2.1.3</mybatis.version> <mybatis.plus.version>3.3.2</mybatis.plus.version> <druid.version>1.1.23</druid.version> <pagehelper.version>1.2.13</pagehelper.version> <thymeleaf.springsecurity5.version>3.0.4.RELEASE</thymeleaf.springsecurity5.version> <spring.boot.starter.version>2.3.1.RELEASE</spring.boot.starter.version> <lombok.version>1.18.12</lombok.version> </properties> <!-- 直接添加在dependencies节点的中的依赖是每个子模块项目都直接拥有的 --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>${spring.boot.starter.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <!-- 添加在dependencyManagement中的依赖只是为了管理子模块项目使用依赖时的版本 --> <dependencyManagement> <dependencies> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>${pagehelper.version}</version> </dependency> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity5</artifactId> <version>${thymeleaf.springsecurity5.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> <version>${spring.boot.starter.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> <version>${spring.boot.starter.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> <version>${spring.boot.starter.version}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>${druid.version}</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatis.plus.version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> <version>${mysql.version}</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> <version>${lombok.version}</version> </dependency> </dependencies> </dependencyManagement>
由于大量的依赖都已经添加在<dependencyManagement>中了,则straw-generator和straw-portal项目都不会直接拥有这些依赖,则需要在这2个子模块项目中自行添加所需的依赖!
在straw-generator项目中(关于代码生成器的相关依赖由于过于特殊,一定只有当前项目需要使用,所以,对版本的管理方式可以不严格要求):
<dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.3.2</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-extension</artifactId> <version>3.3.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> <version>2.3.1.RELEASE</version> </dependency> </dependencies>
在straw-portal
项目中:
<dependencies> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity5</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> </dependencies>