maven多模块项目构建_已有的多个项目整合

本文介绍了如何在Maven中将现有项目整合为多模块项目,包括新建父模块、导入子模块、配置父POM文件以统一管理依赖和版本,以及子模块如何引用父模块的依赖。

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

在开发中,我们经常要使用 maven 进行多模块依赖管理,这样可以进行更方便的依赖管理。那么接下来就演示如何将已有的多个项目整合为一个 maven 多模块项目。

构建项目

新建父模块

新建一个 maven 项目

在这里插入图片描述

在这里插入图片描述

将父模块的 src 源码包删除掉

在这里插入图片描述

将已有的项目导入作为子模块

先将已有的项目(2 个项目:son-01,son-02)拷贝到父模块的目录下,截图如下:idea 并没有把拷贝进来的 2 个项目识别为 module。

在这里插入图片描述

选择已有的项目文件作为模块导入【File - New - Module from Existing Sources…】

在这里插入图片描述

选择父模块目录下先前拷贝进来的项目 son-01

在这里插入图片描述

按下图选择后点击 Finish

在这里插入图片描述

可以看到 son-01 带上了蓝色标识,说明已成为 module;其他的项目重复刚才的操作即可

在这里插入图片描述

配置

父模块的 pom.xml 配置

  1. 改打包方式为 pom(非常重要)

  2. 指定父模块:可选的,指定的父模块会被项目的所有子模块所继承。如 在下面的 pom.xml 中,父模块继承了 spring-boot-starter-parent,项目的子模块都会继承该模块(子模块中也就不需要再指定继承 spring-boot-starter-parent),这样便于统一配置 Spring Boot 的版本,避免依赖冲突。

  3. 声明属性:子模块的 pom.xml 中可以直接使用这些属性

  4. 指定模块列表:指定子模块有哪些

  5. 统一依赖管理:声明依赖的版本,当子模块中引入依赖时没有指定版本,默认使用的就是此处声明的版本

    特别注意:统一依赖管理只是声明了依赖的版本,子模块中使用依赖时仍需配置引入

  6. 定义插件配置:子模块会继承这些配置

父模块的 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>

    <!--2.指定父模块-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.15</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.example</groupId>
    <artifactId>parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging> <!--1.改打包方式为 pom-->

    <!--3.声明属性-->
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <!--4.指定模块列表-->
    <modules>
        <module>son-01</module>
        <module>son-02</module>
    </modules>

    <!-- 5.统一管理子模块的依赖版本 -->
    <dependencyManagement>
        <dependencies>
            <!--自定义的一个依赖包-->
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>my-sdk</artifactId>
                <version>0.0.1</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!--6.定义插件配置-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

子模块的 pom.xml 配置

  1. 指定父模块

  2. maven 坐标:groupId、version 和 父模块相同,可以省略。

  3. 引入依赖:如果想要使用父模块中声明的依赖版本,在引入依赖时省略版本即可,当然可以重新声明将其覆盖

子模块的 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!--1.指定父模块-->
    <parent>
        <groupId>com.example</groupId>
        <artifactId>parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <!--2.maven 坐标:省略了 groupId、version-->
    <artifactId>son-01</artifactId>

    <!--3.引入依赖-->
    <dependencies>
        <!--自定义的一个依赖包-->
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>my-sdk</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>

        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

如果有帮助的话,可以点个赞支持一下嘛🙏

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值