maven多模块使用( pom.xml 中 <modules> 实现子父模块组合)

本文介绍了如何使用Maven管理多模块项目。通过父POM文件可以方便地管理多个子项目,实现统一的构建和依赖管理。文章详细解释了父项目与子项目的关联配置方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到教程。

 

一个多模块项目通过一个父POM 引用一个或多个子模块来定义。父项目,通过以下配置,将子项目关联。

<packaging>pom</packaging>  
<modules>  
    <module>mi-admin</module>  
    <module>mi-admin/modules</module>  
</modules>  

     其中值得注意的是<packaging>pom</packaging>这个父项目不像之前的项目那样创建一个JAR 或者一个WAR,它仅仅是一个引用其它 Maven 项目的POM。pom.xml 中下一部分列出了项目的子模块。这些模块在modules元素中定义,每个 modules 元素对应了一个 simple-parent/ 目录下的子目录。Maven 知道去这些子目录寻找pom.xml 文件,并且,在构建的 simp-parent 的时候,它会将这些子模块包含到要构建的项目中。

     当然,仅仅在父项目,配置子项目是不能够真正实现关联的,因为,这毕竟需要子项目的同意,故!子项目中需要配置:

    <parent>  
        <groupId>org.sonatype.mavenbook.ch06</groupId>  
        <artifactId>simple-parent</artifactId>  
        <version>1.0</version>  
    <!-- 复制 父类定义的这些就行-->
    </parent>  

     现在,通过父pom.xml将2个子项目进行了关联,那么我们需要从 simple-parent 项目运行 mvn clean install 命令,将2个子项目打包,编译为一个项目!

     当 Maven 执行一个带有子模块的项目的时候,Maven 首先载入父POM,然后定位所有的子模块 POM。Maven 然后将所有这些项目的POM 放入到一个称为 Maven 反应堆(Reactor)的东西中,由它负责分析模块之间的依赖关系。这个反应堆处理组件的排序,以确保相互独立的模块能以适当的顺序被编译和安装。

 

另也可以看看此文:https://www.jianshu.com/p/8541dbb961fc

&lt;?xml version="1.0" encoding="UTF-8"?> &lt;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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> &lt;modelVersion>4.0.0&lt;/modelVersion> &lt;groupId>com.example&lt;/groupId> &lt;artifactId>springMybatis&lt;/artifactId> &lt;version>1.0-SNAPSHOT&lt;/version> &lt;name>demo&lt;/name> &lt;packaging>war&lt;/packaging> &lt;properties> &lt;project.build.sourceEncoding>UTF-8&lt;/project.build.sourceEncoding> &lt;java.version>17&lt;/java.version> &lt;maven.compiler.source>17&lt;/maven.compiler.source> &lt;maven.compiler.target>17&lt;/maven.compiler.target> &lt;junit.version>5.9.2&lt;/junit.version> &lt;spring.boot.version>3.1.5&lt;/spring.boot.version> &lt;mybatis.version>3.0.3&lt;/mybatis.version> &lt;/properties> &lt;!-- Maven 示例 --> &lt;parent> &lt;groupId>org.springframework.boot&lt;/groupId> &lt;artifactId>spring-boot-starter-parent&lt;/artifactId> &lt;version>3.1.5&lt;/version> &lt;relativePath/> &lt;!-- lookup parent from repository --> &lt;/parent> &lt;dependencies> &lt;dependency> &lt;groupId>com.fasterxml.jackson.core&lt;/groupId> &lt;artifactId>jackson-databind&lt;/artifactId> &lt;version>2.8.3&lt;/version> &lt;/dependency> &lt;dependency> &lt;groupId>com.example&lt;/groupId> &lt;artifactId>springMybatis&lt;/artifactId> &lt;version>1.0-SNAPSHOT&lt;/version> &lt;/dependency> &lt;/dependencies> &lt;build> &lt;plugins> &lt;!-- Maven WAR Plugin --> &lt;plugin> &lt;groupId>org.apache.maven.plugins&lt;/groupId> &lt;artifactId>maven-war-plugin&lt;/artifactId> &lt;version>3.3.2&lt;/version> &lt;/plugin> &lt;/plugins> &lt;/build> &lt;/project> 请给出pom应该如何写 放在什么位置
最新发布
07-17
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值