快速创建父子工程[新]

前言

Maven工程的继承与聚合特性使得项目的依赖管理与统一打包更加的简便,为了对Maven工程的继承与聚合的特性进行介绍说明及父工程进行统一依赖管理的方法进行操作演示,下面首先创建一个Maven工程作为父工程。

一、父工程

1、创建父工程

在这里插入图片描述
生成之后
在这里插入图片描述

2、移除无用文件

删掉src文件
剩下变成这样
在这里插入图片描述

3、查看 pom.xml

父工程 multi-module 的 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.linfinite</groupId>
    <artifactId>multi-module</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

</project>

二、子工程-启动模块

1、创建子工程启动模块

选中父工程
在这里插入图片描述
随后在其中创建一个子工程,并在创建的过程中 Parent 选择刚才所创建的工程为父工程,子工程的 GroupID 与父工程一定要保持一致
在这里插入图片描述
创建好了之后

2、查看文件及 pom.xml

2.1、查看文件

在这里插入图片描述

2.2、查看 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.linfinite</groupId>
    <artifactId>multi-module</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <modules>
        <module>multi-module-boot</module>
    </modules>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

</project>

multi-module-boot 子的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>
    <parent>
        <groupId>cn.linfinite</groupId>
        <artifactId>multi-module</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>multi-module-boot</artifactId>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

</project>

3、修改pom.xml

添加依赖
但是这个启动模块没有启动这个功能,必须添加依赖

3.1、添加 multi-module 父工程依赖

在 multi-module 父工程的 pom.xml 文件中的 properties标签 下插入 spring-boot.version

	<spring-boot.version>3.4.4</spring-boot.version>

在 dependencyManagement标签 下引入 spring-boot.version

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>${spring-boot.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

父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.linfinite</groupId>
    <artifactId>multi-module</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <modules>
        <module>multi-module-boot</module>
    </modules>

    <!--依赖版本号控制-->
    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <spring-boot.version>3.4.4</spring-boot.version>
    </properties>

    <!--依赖管理,依赖版本号传递-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>${spring-boot.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

3.2、添加 multi-module-boot 子工程依赖

在 multi-module-boot 子工程的 pom.xml 文件中引入这个依赖,版本不需要填写因为父工程已经控制版本了,dependencyManagement标签 就是传递版本号的

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

子工程pom.xlm

<?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>
    <parent>
        <groupId>cn.linfinite</groupId>
        <artifactId>multi-module</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>multi-module-boot</artifactId>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

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

</project>

4、创建启动类

package cn.linfinite;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MultiMoudeApplication {
    public static void main(String[] args) {
        SpringApplication.run(MultiMoudeApplication.class, args);
    }
}

在这里插入图片描述

三、子工程-控制层模块

1、创建子工程控制层模块

在这里插入图片描述
创建好之后

2、查看文件及 pom.xml

2.1、查看文件

文件跟启动模块文件一样,懒得截图了。

2.2、查看 pom.xlm

multi-module 父工程的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.linfinite</groupId>
    <artifactId>multi-module</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <modules>
        <module>multi-module-boot</module>
        <module>multi-module-web</module>
    </modules>

    <!--依赖版本号控制-->
    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <spring-boot.version>3.4.4</spring-boot.version>
    </properties>

    <!--依赖管理,依赖版本号传递-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>${spring-boot.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

multi-module-web 子工程的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>
    <parent>
        <groupId>cn.linfinite</groupId>
        <artifactId>multi-module</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>multi-module-web</artifactId>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

</project>

3、修改pom.xml

确保 Controller 控制层模块被正确打包,并被启动模块依赖,不然创建的controller没有办法用postman调用到。

3.1、添加 multi-module-boot 子工程依赖

添加 multi-module-boot 依赖

        <dependency>
            <groupId>cn.linfinite</groupId>
            <artifactId>multi-module-web</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
3.1.1、添加之后

multi-module-boot依赖

<?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>
    <parent>
        <groupId>cn.linfinite</groupId>
        <artifactId>multi-module</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>multi-module-boot</artifactId>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>cn.linfinite</groupId>
            <artifactId>multi-module-web</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>

3.2、添加 multi-module-web 子工程依赖

添加 multi-module-web 依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
3.2.1、添加之后
<?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>
    <parent>
        <groupId>cn.linfinite</groupId>
        <artifactId>multi-module</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>multi-module-web</artifactId>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

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

</project>

3、创建class测试

自己看着创建就好了,略
在这里插入图片描述

四、依赖管理

解决有很多通用依赖,能不能只引入一个,子模块都可以调用很多个依赖
需要创建一个公共pom.xml,专门引入一些通用依赖即可。

your-parent/
├── pom.xml          ← 管理 lombok 版本
├── common/          <-- 所有公共依赖/工具/配置类都放这里
│   └── pom.xml
├── boot-module/
│   ├── pom.xml      ← 引入 lombok,不写版本
│   └── Application.java
├── controller-module/
│   ├── pom.xml      ← 引入 lombok,不写版本
│   └── UserController.java

1、提前准备

在 multi-module 父工程下创建common包,再创建 pom.xml 文件。

<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>

    <parent>
        <groupId>cn.linfinite</groupId>
        <artifactId>multi-module</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>common</artifactId>
    <packaging>jar</packaging>

    <!--依赖版本号控制-->
    <properties>
        <lombok.version>1.18.34</lombok.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
        </dependency>
    </dependencies>

</project>

在这里插入图片描述

2、 multi-module 父工程添加依赖

在 modules标签 中添加依赖

    <modules>
        <module>common</module>
    </modules>

2.1、添加后

<?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.linfinite</groupId>
    <artifactId>multi-module</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <modules>
        <module>common</module>
        <module>multi-module-boot</module>
        <module>multi-module-web</module>
    </modules>

    <!--依赖版本号控制-->
    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <spring-boot.version>3.4.4</spring-boot.version>
    </properties>

    <!--依赖管理,依赖版本号传递-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>${spring-boot.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

3、子工程中按需引入common

在 multi-module-web 子工程中添加依赖

        <dependency>
            <groupId>cn.linfinite</groupId>
            <artifactId>common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

例子:
multi-module-web 子工程修改后的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>
    <parent>
        <groupId>cn.linfinite</groupId>
        <artifactId>multi-module</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>multi-module-web</artifactId>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>cn.linfinite</groupId>
            <artifactId>common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

我引入了lombok,可以自己在web下创建一个实体类用一下注解试一下能否成功引入

五、项目打包

1、需要了解

当前项目结构

multi-module/
├── common/               ← 公共依赖(工具类、实体类)
├── multi-module-web/     ← Controller、Service
└── multi-module-boot/    ← 启动类(main 方法)

启动类写在 multi-module-boot 中:
它依赖了 multi-module-web
multi-module-web 又依赖了 common

所以 传递依赖链 是:

multi-module-boot
    └── multi-module-web
            └── common

当你打包 multi-module-boot 成一个可执行 jar,它会自动将所有依赖打进去(fat jar)。

打包命令

mvn clean package -pl multi-module-boot -am

-pl:指定只打这个模块(project list)
-am:自动构建它依赖的模块(also make)

最终会生成:

java -jar multi-module-boot/target/multi-module-boot-1.0-SNAPSHOT.jar

2、实际操作

我的项目结构已经非常标准了。既然我只想打一个可运行的包(Spring Boot Fat Jar),那只需要对 multi-module-boot 模块做一件事:
✅ 加上 Spring Boot 的打包插件。

2.1、直接 multi-module-boot 启动工程添加依赖

在 multi-module-boot/pom.xml 中,添加下面这段 配置:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

这样就可以了。

或者

2.2、父工程管理版本,启动工程添加依赖

可选(推荐):版本统一放到父模块
你已经在父模块 multi-module/pom.xml 的 中设置了:

<spring-boot.version>3.4.4</spring-boot.version>

那你还可以把插件版本管理放进父模块里的 ,这样子模块不写版本也能统一用:

<!-- multi-module/pom.xml -->
<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

然后子模块里只需要使用,不用再写版本:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

✅ 最终你只需要:
在 multi-module-boot/pom.xml 加上 (如上)
然后执行打包命令:

mvn clean package -pl multi-module-boot -am

-am 会自动打好 common 和 multi-module-web,然后生成一个包含所有依赖的 fat jar。

🎉 结果
最终生成一个可执行包:

multi-module-boot/target/multi-module-boot-1.0-SNAPSHOT.jar

运行项目:

java -jar multi-module-boot/target/multi-module-boot-1.0-SNAPSHOT.jar

注意:只打包启动模块即可,以为启动模块打包会把下面所依赖的全部都会一起打包

mvn clean install -pl multi-module-boot -am

代码参考地址:仓库地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Java诗人DK

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值