Maven聚合项目(微服务项目)创建流程,以及pom详解

一、创建流程

1、首先创建springboot项目作为父项目

只留下pom.xml 文件,删除src目录及其他无用文件

 2、创建子项目

子项目可以是maven项目,也可以是springboot项目

3、父子项目关联

 4、父项目中依赖管理

<?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>
<!--    继承的springboot版本-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

<!--    引入子模块 -->
    <modules>
        <module>system</module>
        <module>gateway</module>
        <module>common</module>
    </modules>

    <groupId>com.matter</groupId>
    <artifactId>record</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>syh_matter_record_server</name>
    <description>syh_matter_record_server</description>
    <packaging>pom</packaging>

    <!-- 作用:公共依赖引入
         使用场景:基本所有子模块都用到的依赖-->
    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <!-- jdbc依赖包  -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>

    <!-- 版本管理 -->
    <properties>
        <java.version>1.8</java.version>
        <spring.cloud.version>2021.0.1</spring.cloud.version>
        <spring.cloud.alibaba.version>2021.0.1.0</spring.cloud.alibaba.version>
        <mybatis.plus.version>3.4.3.4</mybatis.plus.version>
        <fastjson2.version>2.0.9</fastjson2.version>
        <pagehelper.version>1.4.3</pagehelper.version>
    </properties>

    <!-- 作用:依赖版本生命,不做依赖引入。使用dependencyManagement统一管理项目的版本号,确保应用的各个项目的依赖和版本一致,
               不用每个模块项目都弄一个版本号,不利于管理。 -->
    <dependencyManagement>
        <dependencies>
            <!-- spring-loud -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring.cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!-- 阿里微服务 -->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring.cloud.alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!-- mybatis-plus -->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>${mybatis.plus.version}</version>
            </dependency>

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

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

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

</project>
 depedencyManagement标签介绍

在maven的聚合工程中,父模块的pom文件中,使用dependencyManagement统一管理项目的版本号,确保应用的各个项目的依赖和版本一致,不用每个模块项目都弄一个版本号,不利于管理。

dependencyManagement里只是声明依赖,并不自动实现引入,需要子项目声明需要用的依赖。

  1. 如果不在子项目中声明依赖,是不会从父项目中继承下来的;
  2. 如果在子项目中写了该依赖项,但是没有指定具体版本,这时候才会从父项目中继承相应依赖,以及相应的version和scope;
  3. 如果在子项目中写了该依赖项,并且项目中指定了版本号,那么会使用子项目中指定的版本,不再使用父项目指定的依赖版本号。

父项目使用depedencyManagement声明依赖,子工程引入依赖不需要声明版本

5、公共模块使用

第一步:创建公共模块 common

第二步:父子关联

第三步:其他模块使用公共模块 common

dependencies标签引入公共模块 common,然后就可以调用方法了

二、遇到的问题以及解决方法

1、公共模块被其他模块引入后使用,使用正常,打包报错 程序包 com.xxx.common.utils不存在或者xxx找不到符号

原因:SpringBoot工程打包编译时,会生成两种jar包,一种是普通的jar,另一种是可执行jar。默认情况下,这两种jar的名称相同,在不做配置的情况下,普通的jar先生成,可执行jar后生成,造成可执行jar会覆盖普通的jar。而service工程无法依赖common-utils工程的可执行jar,所以编译失败:程序包不存在。
解决:在common-utils工程中,pom.xml文件中加入配置即可:

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

        </plugins>
 </build>

2、Maven多模块项目无法加载其他模块(公共模块)下的Bean

比如公共模块中有redis序列化配置,以及redis工具类需要需要注入

 在需要使用的模块中的启动类添加 @ComponentScan注解

// 值为一个数组,表示扫描的路径,递归扫描到最后
@ComponentScan(basePackages = {"com.example"})

 这里看到我们两个模块刚好前缀一样,所以只配置一个路径即可

 

 

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Maven聚合项目是指将多个子项目组织在一起,形成一个大的项目。通过聚合项目,可以方便地管理和构建多个相关的子项目。 以下是使用Maven搭建聚合项目的步骤: 1. 创建项目:在IDEA中创建一个新的Maven项目,作为父项目。可以使用以下命令创建一个空的Maven项目: ```shell mvn archetype:generate -DgroupId=com.example -DartifactId=parent-project -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false ``` 2. 创建项目:在父项目创建多个子项目。可以使用以下命令在父项目创建项目: ```shell mvn archetype:generate -DgroupId=com.example -DartifactId=child-project1 -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false mvn archetype:generate -DgroupId=com.example -DartifactId=child-project2 -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false ``` 3. 配置父项目pom.xml文件:在父项目pom.xml文件中添加子项目的模块信息。在`<modules>`标签中添加子项目的artifactId,例如: ```xml <modules> <module>child-project1</module> <module>child-project2</module> </modules> ``` 4. 构建聚合项目:在父项目的根目录下执行以下命令,构建聚合项目: ```shell mvn clean install ``` 5. 运行子项目:可以在子项目的目录下执行相应的命令,例如: ```shell cd child-project1 mvn clean package ``` 通过以上步骤,你就可以成功搭建一个Maven聚合项目了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

S Y H

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

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

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

打赏作者

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

抵扣说明:

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

余额充值