一篇解决springBoot+Maven多模块项目

首先:使用maven多模块项目的好处?

1、代码复用

1)比如一个项目的前台服务和后台管理系统,想让他们单独运行,如果划分成两个单独的项目的话,实体类、service、dao等就会产生重复,以前的做法是直接复制过去,但实体类要是改动某些属性的话,两个系统都要改。但如果我们把公共的部分单独提取成一个模块,把前后台系统聚合一块,引用这个公共的模块,就可以避免这个问题。

2、更好的分工

每个人负责一个模块,尽可能的减少版本冲突。

3、减少build的时间

如果项目很大,改动一点就要build整个项目,等待时间就会很长。现在改动那个模块就只build那个模块,减少等待时间

4、装B

多个模块光看着就比单个模块显得高大上,对吧。无形装逼,最为致命。

ok,下面介绍springBoot+Maven多模块项目在idea中的创建、打包、运行过程,其核心是Maven的三个特性:依赖、继承和聚合。只要理解了Maven的这三个特性,其他类型的多模块项目都是一个道理。
来,先看一下项目结构
项目结构 项目结构
parent:pom项目,用来聚合其他子模块和进行总的依赖版本管理。不用单独运行。所以他没有启动类。
common:提取出来的一个公共模块,主要用于被其他模块依赖。不用单独运行。所以他也没有启动类。
module_a:可单独运行的模块。
module_b:可单独运行的模块。
当时想的是,module_a是前台服务,module_b是后台管理系统,我要单独运行,不能因为module_a挂了导致module_b启动不起来。
下面正式开始。。。

一、先把项目和多个模块建立起来

1、先建立一个pom类型父工程

创建pom工程
建立父工程的好处就是1、方便聚合其他子模块,实现一键操作所有子模块。2、聚合后能自动识别子模块间的依赖关系。
举个例子:模块A依赖模块B,对模块A进行打包前要先install模块B,否则就会提示无法处理模块B这个依赖,若是依赖有多个就要先把依赖一个个install,就很烦。用父工程聚合子模块后,就能自动帮你处理这个问题,就很好。若此时模块A还继承自某个父工程,也要先把父工程install后才能对模块A进行打包,否则也会报错。

2、在父工程上右键new Module依次将其他子模块建立出来

new Module
这个地方的Content root决定了子模块和父模块是同一级显示和还是上下级的显示

二、添加依赖关系

先看父工程的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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/>
    </parent>

    <!--父工程聚合子模块,
       1、能对聚合的模块统一进行操作
       2、能自动识别子模块间的依赖关系(即该先install那个)-->
    <modules>
        <module>module_a</module>
        <module>module_b</module>
        <module>common</module>
    </modules>

    <groupId>com.jyf.mavenmulti</groupId>
    <artifactId>parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>parent</name>
    <!--父工程的类型为pom工程-->
    <packaging>pom</packaging>
    <description>maven多模块父工程-用来聚合多个子模块和统一管理依赖版本</description>

    <properties>
        <java.version>1.8</java.version>
        <junit.version>4.12</junit.version>
    </properties>

    <!--父工程被继承后用<dependencyManagement>对所有的依赖版本号进行统一管理-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
            </dependency>
        </dependencies>
        
    </dependencyManagement>

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

然后是common模块的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>

    <!--继承父工程-->
    <parent>
        <groupId>com.jyf.mavenmulti</groupId>
        <artifactId>parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <!--父工程pom.xml文件相对位置-->
        <relativePath>../pom.xml</relativePath>
    </parent>

    <artifactId>common</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>common</name>
    <packaging>jar</packaging>
    <description>公共的被其他模块依赖的模块</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <!--
    common模块中没有加入build标签中的maven插件,因为它的作用只是将公共的部分分离出来,被其他模块引用。
    本身并不需要变成fat.jar(可独立运行的springBoot的jar)
    -->
</project>

然后是module_a的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>

    <!--继承父工程-->
    <parent>
        <groupId>com.jyf.mavenmulti</groupId>
        <artifactId>parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <!--父工程pom.xml文件相对位置-->
        <relativePath>../pom.xml</relativePath>
    </parent>

    <artifactId>module_a</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>module_a</name>
    <packaging>jar</packaging>
    <description>子模块A,可以单独运行</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!--在a模块中引入common模块,能自动识别该common模块,我不清楚这地方为啥能自动识别,此时他也没在本地仓库里啊-->
        <dependency>
            <groupId>com.jyf.mavenmulti</groupId>
            <artifactId>common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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

module_b的pom.xml内容和module_a差不多就不贴了。
ok,到此这个maven多模块项目算是简单的搭建好了。

三、测试一下

common模块中建一个Test类

@Data
public class Test {

    private int id;

    private String name;
}

module_a的controller中向页面返回Test类的一个对象

@Controller
@RequestMapping("/controllerA")
public class ControllerA {

    @ResponseBody
    @RequestMapping("/test")
    public Test test(){
        Test test = new Test();
        test.setId(1);
        test.setName("tom");
        return test;
    }
}

运行模块A
测试

打包

在这里插入图片描述在这里插入图片描述
在这里插入图片描述
ok,打包成功!
因为聚合的存在,我们可以在父工程中实现一键打包所有的子模块,并能自动处理依赖关系
因为模块A和模块B是两个单独运行的模块,所以想运行哪个就单独拿出来运行就行了。

遇到的问题:

1、在自动打包common模块时报错,说找不到主类(main class)?
原因:我在父工程的pom.xml中使用了spring-boot-maven-plugin这个maven插件(它的作用请看下一题),common模块继承了父工程,所有他也有了这个插件,这个插件找不到主类(springBoot的启动类,让我给删了,因为他也不需要单独运行)就报错了。
解决:把父工程pom.xml中的这个插件删掉就行了。父工程又不需要打包,这样common模块就继承不过去了,就没事了。其他两个子模块想使用就单独在自己的pom.xml中引入即可。
2、 这个插件的作用?springBoot的maven插件

spring-boot-maven-plugin这个插件是专门为springBoot项目服务的,
作用是打包时对mvn package打包后的结果进行二次打包,目的是将当前项目所有的依赖也打包进来(会自动的找到启动类作为main class),官方解释此时的jar包叫fat.jar
若不加这个插件,就只打包当前项目中的东西,依赖不会被打包进来。

看完了吧,看完了赶紧试试吧,光看没用,自己动手去实现一下才是硬道理

好的到此本篇文章算是结束了,觉得不错的点个赞在走呗。

发布了10 篇原创文章 · 获赞 1 · 访问量 725

猜你喜欢

转载自blog.csdn.net/qq_40925189/article/details/103842208