学无先后,达者为师

网站首页 编程语言 正文

【Maven】多模块构建项目的维护

作者:骑着蜗牛ひ追导弹' 更新时间: 2022-07-16 编程语言

文章目录

  • 多模块构建维护
    • 一、聚合
    • 二、继承
    • 三、聚合与继承
    • 四、属性
      • 4.1 介绍
      • 4.2 常见的属性及其调用方式
        • 1、自定义属性
        • 2、内置属性
        • 3、setting属性
        • 4、java系统属性
        • 5、环境变量属性


多模块构建维护

在这里插入图片描述

可以看到通过上一章的讲解,我们将一个ssm项目分模块管理,形成了pojo、dao、service、controller四个模块,并且逐层递进形成依赖关系,假设此时dao层模块出现了改动,我们该如何进一步管理呢?

一、聚合

聚合用于快速构建maven工程,一次性构建多个项目、模块

操作起来很简单:

  • 创建一个空模块,打包类型定义为pom
  • 定义当前模块进行构建操作时关联其他的模块(绑定)

注:参与合操作的模块最终执行顺序与模块间的依赖关系有关,与配置顺序无关

首先构建一个maven的项目,只留pom.xml文件:

image-20220714121422615

添加项目的管理配置:

<?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>com.zyx</groupId>
    <artifactId>maven_ssm</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!-- 定义该工程用于构建项目管理 -->
    <packaging>pom</packaging>

    <!--  管理的工程列表  -->
    <modules>
        <!--  具体的工程  -->
        <module>../maven_ssm_pojo</module>
        <module>../maven_ssm_dao</module>
        <module>../maven_ssm_service</module>
        <module>../maven_ssm_controller</module>
    </modules>

</project>

对项目进行整体的编译,可以看到结果显示的是所有的模块选项:

image-20220714123805165

install一下:

在这里插入图片描述

可以看到对应的文件夹下添加了jar包文件:

image-20220714124359888

返回顶部


二、继承

模块间依赖关系的维护

image-20220714125132217

实际开发过程中,由于每个模块一个负责人,所以会导致每个人使用的相同依赖出现不同版本,可能会引发版本冲突问题。

如上图所示,假设service、dao模块都用到了spring-context依赖,但是一个5.1.9,另一个5.2.0,就有可能引起版本的问题。此时我们可以通过继承实现子工程中沿用父工程(ssm)的配置,统一版本。

首先在父工程中声明其子工程的所需要的所有坐标,并配置声明依赖管理:

<!--声明此处进行依赖管理-->
<dependencyManagement>
    <!--具体的依赖-->
    <dependencies>
        <!--添加自己的工程模块依赖-->
        <dependency>
            <groupId>com.zyx</groupId>
            <artifactId>maven_ssm_pojo</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>com.zyx</groupId>
            <artifactId>maven_ssm_dao</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>com.zyx</groupId>
            <artifactId>maven_ssm_service</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <!--spring环境-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>

        <!--mybatis环境-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.3</version>
        </dependency>
        <!--mysql环境-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <!--spring整合jdbc-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <!--spring整合mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.3</version>
        </dependency>
        <!--druid连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.16</version>
        </dependency>
        <!--分页插件坐标-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.2</version>
        </dependency>

        <!-- spring mvc环境 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <!--jackson相关坐标3个-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.0</version>
        </dependency>
        <!--servlet环境-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

        <!--其他组件-->
        <!--junit单元测试-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <!--spring整合junit-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
    </dependencies>

</dependencyManagement>

<build>
    <pluginManagement>
        <!--设置插件-->
        <plugins>
            <!--具体的插件配置-->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <port>80</port>
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

子工程中对应声明使用父工程的依赖配置,同时将子工程所有的版本信息去除:

<!--定义该工程的父工程-->
<parent>
    <groupId>com.zyx</groupId>
    <artifactId>maven_ssm</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--填写父工程的pom文件-->
    <relativePath>../maven_ssm/pom.xml</relativePath>
</parent>

<dependencies>
    <!--spring --> 
    <dependency> 
        <groupId>org.springframework</groupId> 
        <artifactId>spring-context</artifactId>
    </dependency>
</dependencies>

可继承的资源:

image-20220714132403568

返回顶部


三、聚合与继承

作用

  • 聚合用于快速构建项目
  • 继承用于快速配置

相同点

  • 聚合与继承的pom.xm文件打包方式均为pom,可以将两种关系制作到同一个pom文件中
  • 聚合与继承均属于设计型模块,并无实际的模块内容

不同点

  • 聚合是在当前模块中配置关系,聚合可以感知到参与聚合的模块有哪些
  • 继承是在子模块中配置关系,父模块无法感知哪些子模块继承了自己

返回顶部


四、属性

4.1 介绍

版本统一的重要性

image-20220714133234441

虽然我们可以在父项目中进行依赖资源的统一管理,但是不免我们有时会出错,如上图所示,上下配置的资源版本不一致,这种情况下我们可以增添属性信息避免人为配置的出错。

<!--定义自定义属性-->
<properties>
    <spring.version>5.1.9.RELEASE</spring.version>
    <junit.version>4.12</junit.version>
</properties>

<!--声明此处进行依赖管理-->
<dependencyManagement>
    <!--具体的依赖-->
    <dependencies> 
        <!--其他组件-->
        <!--junit单元测试-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
        </dependency>
        <!--spring整合junit-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>

对于我们继承的项目依赖,版本同样可以统一,因为使用的是maven的项目对象版本,在最开始就声明了<version>1.0-SNAPSHOT</version>,所以后面直接使用${version}调用就行了。

<?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>com.itheima</groupId>
    <artifactId>ssm</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!-- 定义该工程用于构建项目管理 -->
    <packaging>pom</packaging>
    
     <!--声明此处进行依赖管理-->
    <dependencyManagement>
        <!--具体的依赖-->
        <dependencies>
            <!--添加自己的工程模块依赖-->
            <dependency>
                <groupId>com.itheima</groupId>
                <artifactId>ssm_pojo</artifactId>
                <version>${version}</version>
            </dependency>

            <dependency>
                <groupId>com.itheima</groupId>
                <artifactId>ssm_dao</artifactId>
                <version>${version}</version>
            </dependency>

            <dependency>
                <groupId>com.itheima</groupId>
                <artifactId>ssm_service</artifactId>
                <version>${version}</version>
            </dependency>
    </dependencies>
</dependencyManagement>

返回顶部


4.2 常见的属性及其调用方式

1、自定义属性

image-20220714135104648


2、内置属性

image-20220714135122507


3、setting属性

在这里插入图片描述


4、java系统属性

image-20220714135345918

在这里插入图片描述


5、环境变量属性

image-20220714135409207

在这里插入图片描述

返回顶部


原文链接:https://blog.csdn.net/qq_45797116/article/details/125783265

栏目分类
最近更新