Maven父子项目工程依赖原则及问题解决


文章目录

  • Maven父子项目工程依赖原则及问题解决
  • 插件
  • 父子工程继承原则
  • Springboot公共模块处理方案
  • 方案一(推荐):common只放自定义包,father模块放公共,son继承father后,再通过引入common。
  • 方案二:common中既放自定义包,又放公共,son依然继承father(并没有意义),再在son中通过引入common。
  • 方案对比
  • SpringBoot父依赖冲突问题(针对方案二)
  • 特别注意



插件

依赖:运行时开发时都需要用到的jar包,在项目打包时需要把这些依赖也打包进项目里,是项目必须的一部分。

插件:在项目开的发时需要,但是在项目运行时不需要。主要用于减少开发工作量,非项目必须。例如compile插件是用来编译代码的工具,mybatis插件是用来自动生成数据库dao和mapper的工具,没有这些工具自己也可以手敲。

父子工程继承原则

  1. parent的pom.xml设置
  • 父工程需要打包为pom
  • 通过module引入子模块
<project>
    <!--父依赖的父依赖-->
    <parent>
        <groupId>com.liu.grandpa</groupId>
        <artifactId>grandpa</artifactId>
        <version>0.0.1-SNAPSHOT</version>
	</parent>
    
    <!--module信息-->
	<groupId>com.liu.father</groupId>
	<artifactId>father</artifactId>
	<version>0.0.1-SNAPSHOT</version>
    
    <!--打包方式-->
	<packaging>pom</packaging>

    <!--包含哪些子模块-->
	<modules>
		<module>sonA</module>
		<module>sonB</module>
    </modules>
    
    <!--properties信息-->
    <properties>
        <java.version>1.8</java.version>
    </properties>
    
    <!--导入的依赖-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    
    <!--版本控制,以后导入spring-cloud-alibaba-x依赖都被限定-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.7.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    <!--插件-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
  1. son的pom.xml设置
  • 引入parent依赖
<parent>
	<groupId>com.liu.father</groupId>
	<artifactId>father</artifactId>
	<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>sonA</artifactId>
<version>0.0.1-SNAPSHOT</version>
  1. 产生的效果
  • 属性问题:parent模块中属性、属性都可以直接在son中直接使用
  • 导包问题:中的属性son中不能直接使用,需要再通过引入具体包,但是中的版本控制会被沿用
  • 插件问题:不能被继承
  • 祖辈依赖问题:由于son继承father,father继承grandpa,所以son对于grandpa的依赖=son对father的依赖+father对grandpa的依赖
  • 注意:
  • 属性为compile、provided、test均可被继承
  • 如果只是想声明父子关系不想让son引入father依赖,可以只配置father的,不用在子项目中配置

Springboot公共模块处理方案

父工程father,子模块son,公共模块common(各种手写的自定义包)

方案一(推荐):common只放自定义包,father模块放公共,son继承father后,再通过引入common。
方案二:common中既放自定义包,又放公共,son依然继承father(并没有意义),再在son中通过引入common。
方案对比

方案一通过传递依赖,可以将ommon工程中的自定义包和common工程pom文件中属性为compile、provided、test的包全都传递给son

方案二通过实现传递,但依赖的包均为编译好的包,只能使用common工程中的自定义包和common工程pom文件中属性为compile的包(provided和test都不可用)。

SpringBoot父依赖冲突问题(针对方案二)

问题描述:如果common工程作为公共模块整合springboot,需要继承springboot的parent依赖(继承版本号),同时common要做为father模块的一个子模块,这时common就会出现两个parent。

现象出现:son需要通过的方式导入common,但是,但common工程不能出现两个父模块,报红

解决方案:springboot有两种引入版本的方式,可采用方式二通过引入版本管理

方式一:引入spring-boot-starter-parent实现版本管理

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.6.3</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

方式二:引入版本管理

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.6.3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

特别注意

springbott在创建大型工程时,Module不要直接parent依赖springboot,最好建一个my-spring-boot的artifact(module)用于缓冲,并重新梳理项目的dependencies。将一些项目必须的特定版本依赖在my-spring-boot中声明,这样当需要升级springboot版本的时候,可以减少版本依赖问题导致的错误