maven之父子工程&版本控制&案例实战,及拓展groupId和artifactId的含义

12 篇文章 0 订阅
2 篇文章 0 订阅

<parent>标签

用于父子工程项目,什么是父子工程?

顾名思义,maven父子项目是一个有一个父项目,父项目下面又有很多子项目的maven工程,当然,子项目下面还可以添加子项目,从而形成一个树形的结构。那么,这么做有什么好处呢?有以下两点:

  • 依赖传递
  • 打包方便

父工程package 需要指定pom
子工程需要声明parent,否则无法使用父pom的依赖

<dependency> 标签

导入依赖
和dependencyManagement 一起出现,指定的版本优先级更高,通常出现在父pom

<parent> & <dependency>区别

通常parent用于做结构管理,声明子工程继承父工程的
A依赖B C继承B

A = B的依赖 + B自定义class
C = B的依赖
image

<dependencyManagement> 标签

版本控制,只声明版本,不进行依赖导入

<modules> 标签

执定子工程,如果不指定,不能识别子工程

版本控制优先级

子项目的版本优先更高
子项目 dependency
子项目 dependencyManagement
父项目 dependency
父项目 dependencyManagement

如果出现同一个pom,多个不同版本的相同依赖,最下方优先级最高

实战案例:

例如我们有如下项目目录
在这里插入图片描述

最外层的pom.xml即为父工程,一般用于控制子工程版本

父pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	
    <groupId>cn.wolfcode</groupId>
    <artifactId>caro2o</artifactId>
    <version>3.8.3</version>

    <name>caro2o</name>
    <url>http://www.ruoyi.vip</url>
    <description>e店邦o2o平台管理系统</description>
    
    <properties>
        <caro2o.version>3.8.3</caro2o.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
        <druid.version>1.2.11</druid.version>
        <bitwalker.version>1.21</bitwalker.version>
        <swagger.version>3.0.0</swagger.version>
        <kaptcha.version>2.3.2</kaptcha.version>
        <mybatis-spring-boot.version>2.2.2</mybatis-spring-boot.version>
        <pagehelper.boot.version>1.4.1</pagehelper.boot.version>
        <fastjson.version>2.0.8</fastjson.version>
        <oshi.version>6.1.6</oshi.version>
        <commons.io.version>2.11.0</commons.io.version>
        <commons.fileupload.version>1.4</commons.fileupload.version>
        <commons.collections.version>3.2.2</commons.collections.version>
        <poi.version>4.1.2</poi.version>
        <velocity.version>2.3</velocity.version>
        <jwt.version>0.9.1</jwt.version>
    </properties>
	
    <!-- 依赖声明 -->
    <dependencyManagement>
        <dependencies>

            <!-- SpringBoot的依赖配置-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.5.14</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!-- 阿里数据库连接池 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
                <version>${druid.version}</version>
            </dependency>

            <!-- 解析客户端操作系统、浏览器等 -->
            <dependency>
                <groupId>eu.bitwalker</groupId>
                <artifactId>UserAgentUtils</artifactId>
                <version>${bitwalker.version}</version>
            </dependency>

            <!-- SpringBoot集成mybatis框架 -->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>${mybatis-spring-boot.version}</version>
            </dependency>

            <!-- pagehelper 分页插件 -->
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper-spring-boot-starter</artifactId>
                <version>${pagehelper.boot.version}</version>
            </dependency>

            <!-- 获取系统信息 -->
            <dependency>
                <groupId>com.github.oshi</groupId>
                <artifactId>oshi-core</artifactId>
                <version>${oshi.version}</version>
            </dependency>

            <!-- Swagger3依赖 -->
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-boot-starter</artifactId>
                <version>${swagger.version}</version>
                <exclusions>
                    <exclusion>
                        <groupId>io.swagger</groupId>
                        <artifactId>swagger-models</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>

            <!-- io常用工具类 -->
            <dependency>
                <groupId>commons-io</groupId>
                <artifactId>commons-io</artifactId>
                <version>${commons.io.version}</version>
            </dependency>

            <!-- 文件上传工具类 -->
            <dependency>
                <groupId>commons-fileupload</groupId>
                <artifactId>commons-fileupload</artifactId>
                <version>${commons.fileupload.version}</version>
            </dependency>

            <!-- excel工具 -->
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>${poi.version}</version>
            </dependency>

            <!-- velocity代码生成使用模板 -->
            <dependency>
                <groupId>org.apache.velocity</groupId>
                <artifactId>velocity-engine-core</artifactId>
                <version>${velocity.version}</version>
            </dependency>

            <!-- collections工具类 -->
            <dependency>
                <groupId>commons-collections</groupId>
                <artifactId>commons-collections</artifactId>
                <version>${commons.collections.version}</version>
            </dependency>

            <!-- 阿里JSON解析器 -->
            <dependency>
                <groupId>com.alibaba.fastjson2</groupId>
                <artifactId>fastjson2</artifactId>
                <version>${fastjson.version}</version>
            </dependency>

            <!-- Token生成与解析-->
            <dependency>
                <groupId>io.jsonwebtoken</groupId>
                <artifactId>jjwt</artifactId>
                <version>${jwt.version}</version>
            </dependency>

            <!-- 验证码 -->
            <dependency>
                <groupId>com.github.penggle</groupId>
                <artifactId>kaptcha</artifactId>
                <version>${kaptcha.version}</version>
            </dependency>

            <!-- 定时任务-->
            <dependency>
                <groupId>cn.wolfcode</groupId>
                <artifactId>caro2o-quartz</artifactId>
                <version>${caro2o.version}</version>
            </dependency>

            <!-- 代码生成-->
            <dependency>
                <groupId>cn.wolfcode</groupId>
                <artifactId>caro2o-generator</artifactId>
                <version>${caro2o.version}</version>
            </dependency>

            <!-- 核心模块-->
            <dependency>
                <groupId>cn.wolfcode</groupId>
                <artifactId>caro2o-framework</artifactId>
                <version>${caro2o.version}</version>
            </dependency>

            <!-- 系统模块-->
            <dependency>
                <groupId>cn.wolfcode</groupId>
                <artifactId>caro2o-system</artifactId>
                <version>${caro2o.version}</version>
            </dependency>

            <!-- 通用工具-->
            <dependency>
                <groupId>cn.wolfcode</groupId>
                <artifactId>caro2o-common</artifactId>
                <version>${caro2o.version}</version>
            </dependency>

            <!-- 业务模块-->
            <dependency>
                <groupId>cn.wolfcode</groupId>
                <artifactId>caro2o-business</artifactId>
                <version>${caro2o.version}</version>
            </dependency>

        </dependencies>
    </dependencyManagement>

    <modules>
        <module>caro2o-admin</module>
        <module>caro2o-framework</module>
        <module>caro2o-system</module>
        <module>caro2o-quartz</module>
        <module>caro2o-generator</module>
        <module>caro2o-common</module>
        <module>caro2o-business</module>
    </modules>
    <packaging>pom</packaging>


    <dependencies>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>public</id>
            <name>aliyun nexus</name>
            <url>https://maven.aliyun.com/repository/public</url>
            <releases>
                <enabled>true</enabled>
            </releases>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>public</id>
            <name>aliyun nexus</name>
            <url>https://maven.aliyun.com/repository/public</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

</project>

子项目迭代时一般是以相同版本迭代,所以用<caro2o.version>3.8.3</caro2o.version>来统一控制版本

写在<dependencyManagement>标签中的依赖为子项目可选择加载的依赖,若某子项目要加载该依赖则在自己的pom.xml中的dependencies里添加即可,不用再写版本号version,因为都交由父项目统一管理了,而这就避免了不同子项目引用相同依赖时因版本不同而产生冲突的问题。

写在<dependencies>标签中的依赖为子项目必须加载的依赖,下略。


服务启动入口子项目caro2o-admin的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>caro2o</artifactId>
        <groupId>cn.wolfcode</groupId>
        <version>3.8.3</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <packaging>jar</packaging>
    <artifactId>caro2o-admin</artifactId>

    <description>
        web服务入口
    </description>

    <dependencies>

        <!-- spring-boot-devtools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional> <!-- 表示依赖不会传递 -->
        </dependency>

        <!-- swagger3-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
        </dependency>

        <!-- 防止进入swagger页面报类型转换错误,排除3.0.0中的引用,手动增加1.6.2版本 -->
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-models</artifactId>
            <version>1.6.2</version>
        </dependency>

         <!-- Mysql驱动包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <!-- 核心模块-->
        <dependency>
            <groupId>cn.wolfcode</groupId>
            <artifactId>caro2o-framework</artifactId>
        </dependency>

        <!-- 定时任务-->
        <dependency>
            <groupId>cn.wolfcode</groupId>
            <artifactId>caro2o-quartz</artifactId>
        </dependency>

        <!-- 代码生成-->
        <dependency>
            <groupId>cn.wolfcode</groupId>
            <artifactId>caro2o-generator</artifactId>
        </dependency>
        <!-- 业务模块-->
        <dependency>
            <groupId>cn.wolfcode</groupId>
            <artifactId>caro2o-business</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.1.1.RELEASE</version>
                <configuration>
                    <fork>true</fork> <!-- 如果没有该配置,devtools不会生效 -->
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>   
                <groupId>org.apache.maven.plugins</groupId>   
                <artifactId>maven-war-plugin</artifactId>   
                <version>3.1.0</version>   
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <warName>${project.artifactId}</warName>
                </configuration>   
           </plugin>   
        </plugins>
        <finalName>${project.artifactId}</finalName>
    </build>

</project>

注意:

在这里插入图片描述

比如该用若依框架搭建的demo项目,我要新增一个名为caro2o-business的业务子项目,则需要将该业务子项目作为模块依赖添加到服务启动入口子项目caro2o-admin的pom.xml里,否则服务启动后admin没加载到business模块内的代码,就会出现404或500等一系列错误。

在这里插入图片描述

然后在业务子项目的pom.xml里添加工具子项目等不同模块的依赖,如:

在这里插入图片描述
因为父项目已经控制版本号了,所以这里可以不用再写version。在图中的情况,若整个项目整体迭代而在父项目的pom.xml中升级更改了统一版本号,则会有报错的可能,因为Maven中首先取的是子项目自己pom.xml里的依赖版本号,就会导致引用caro2o-system时和别的子项目版本出现冲突。


拓展:

maven项目的groupId和artifactId什么意思?

首先要知道groupId和artifactId的存在是为了定位到你的项目,所以它们充当着坐标的角色。
groupId:group意思为组,所以理解为是组织Id,也是公司Id,通常它的写法与公司域名类似。一般分三段,即“域.公司名称.子项目”,域指org、com、cn
等,类似计算机网络里学过的,cn指china,com商业,org非盈利组织。
artifactId:即一个具体的项目,命名方式自己决定。

举个某马大型商城项目结构为例,该项目采用了微服务架构,所以使用了聚合方式,即一个大项目下几个小项目(这样是为了便于版本管理):

在这里插入图片描述
例如:
< groupId>com.leyou.parent< /groupId>,其中com为商业标识,leyou为这个乐优商城项目的公司名称,parent是项目名称,但是因为这个项目采用了聚合方式,下面还有许多项目,这个作为最顶层容器,所以起名parent。 < artifactId>leyou</ artifactId >代表这个项目的总名称为leyou。
其下面的子项目命名为 < groupId>com.leyou.user</ groupId>,前两段域和公司名都相同,只是项目那儿放的这个具体的子项目名称。
< artifactId>leyou-user< /artifactId>使用的leyou-user表示该项目是leyou项目下的user子项目,这种方式结构清晰易懂。
采用这种命名后,项目内部结构通常要相对应。
例如:
在这里插入图片描述

这个项目是大项目下每个项目作为一个单独的服务,对于那些可能被其他服务调用的服务,可以再拆分为两个,例如,这里子项目leyou-item因为为被其他服务调用,所以又才分为了interface和service两个子项目,interface对外提供服务调用,service作具体的业务操作。
在这里插入图片描述

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值