【Maven】进阶

1. 聚合

为了防止某个模块(dao)更新了,重新编译了,导致和其他模块不兼容,需要用一个root来管理其他几个模块

在这里插入图片描述

    <groupId>com.heima</groupId>
    <artifactId>ssm</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>pom</packaging>
    <!-- 管理的工程列表 -->
    <modules>
        <module>ssm_pojo</module>
        <module>ssm_dao</module>
        <module>ssm_service</module>
        <module>ssm_controller</module>
    </modules>

在这里插入图片描述

构建顺序按照依赖顺序

2. 继承

在这里插入图片描述

不同模块依赖的库版本不同,很容易导致不兼容的问题

在这里插入图片描述

我们让父工程统一管理子工程依赖的版本即可,子工程继承父工程的版本

父工程的pom.xml

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.itheima</groupId>
            <artifactId>ssm_pojo</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.itheima</groupId>
            <artifactId>ssm_dao</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.itheima</groupId>
            <artifactId>ssm_service</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.itheima</groupId>
            <artifactId>ssm_controller</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.2.13.RELEASE</version>
        </dependency>
    </dependencies>
</dependencyManagement>

子工程的pom.xml

<parent>
    <groupId>com.itheima</groupId>
    <artifactId>ssm</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--父工程的pom-->
    <relativePath>../ssm/pom.xml</relativePath>
</parent>

<groupId>com.itheima</groupId>      <!-- 子工程的组织,由于写了父工程,可省略 -->
<artifactId>ssm_pojo</artifactId>
<version>1.0-SNAPSHOT</version>     <!-- 子工程的版本,由于写了父工程,尽量和父工程一致,可省略 -->

在这里插入图片描述

父工程写了依赖的版本号,子工程就不用写了,直接引用即可(如果不引用,子工程就不会依赖对应的包)。由于子工程都是继承父工程的依赖,可以减少版本冲突

继承与聚合

  1. 作用

    • 聚合需要配置modules标签,用于快速构建项目,root进行build,所有的工程都需要build
    • 继承需要配置parent标签,用于简化配置,父工程配置过,子工程直接继承
  2. 相同点

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

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

3. 属性变量定义与使用

在这里插入图片描述
写版本的时候不再写具体的版本号,而是直接写变量

父工程ssm的pom.xml

<groupId>com.itheima</groupId>
<artifactId>ssm</artifactId>
<version>1.0-SNAPSHOT</version>
    
<properties>
     <spring.version>5.1.9.RELEASE</spring.version>
     <junit.version>4.12</junit.version>
<properties/>

<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>
        <dependency>
            <groupId>com.itheima</groupId>
            <artifactId>ssm_controller</artifactId>
            <version>${version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>

4. 版本管理

SNAPSHOT(快照版本)

  • 项目开发过程中,为方便团队成员合作,解决模块间相互依赖和时时更新的问题,开发者对每个模块进行构建的时候,输出的临时性版本叫快照版本,即测试阶段版本
  • 快照版本会随着开发的进展不断更新

RELEASE(发布版本)

  • 项目开发到进入阶段里程碑后,向团队外部发布较为稳定的版本,这种版本所对应的构件文件是稳定的,即便进行功能的后续开发,也不会改变当前发布版本内容,这种版本称为发布版本

子工程的pom.xml

<parent>
    <groupId>com.itheima</groupId>
    <artifactId>ssm</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--父工程的pom-->
    <relativePath>../ssm/pom.xml</relativePath>
</parent>

<groupId>com.itheima</groupId>
<artifactId>ssm_pojo</artifactId>
<version>2.1.13-RELEASE</version>     <!-- 自定义子工程版本 -->

build之后,仓库中就会生成对应的包

5. 资源配置

pom.xml中配置的属性,其实是可以在配置文件中通过${}的方式使用的

在这里插入图片描述

我们希望xml中统一管理配置信息,我们先在ssm的pom.xml中自定义属性变量

在这里插入图片描述
然后在ssm_dao的.properties中使用

在这里插入图片描述

ssm的pom.xml中配置资源文件路径,表示资源文件使用pom.xml中的变量

在这里插入图片描述

在这里插入图片描述

../ssm_dao该层${project.basedir}

在这里插入图片描述

在这里插入图片描述

filtering设置为true,表示该目录需要参与build,该目录下可以使用xml中配置的变量

6. 多环境配置

不同环境加载不同配置,不要随意更改配置
在这里插入图片描述
ssm的pom.xml中多环境配置如下,当然也不能少配置资源文件的路径信息

<profiles>
    <!-- 定义生产环境-->
    <profile>
        <id>release_env</id>
        <properties>
            <jdbc.url>jdbc:mysql://192.168.100.200:3306/ssm_db</jdbc.url>
        <properties/>
    </profile>

    <!-- 定义开发环境-->
    <profile>
        <id>dep_env</id>
        <properties>
        	<jdbc.url>jdbc:mysql://192.168.100.201:3306/ssm_db</jdbc.url>
        <properties/>
        <!-- 设置默认环境 -->
        <activation>
	        <activeByDefault>true</activeByDefault>
	    </activation>
    </profile>
</profiles>

<resources>
    <resource>
        <directory>${project.basedir}/src/main/resources</directory>
        <filtering>true</filtering>
    </resource>
</resources>

添加install参数

在这里插入图片描述

我们现在执行install,ssm_dao的properties中的url就是jdbc:mysql://192.168.100.201:3306/ssm_db

7. 跳过测试(了解)

由于声明周期都是插件在执行,我们配置test相关插件即可

跳过全部测试环节
在这里插入图片描述

跳过指定测试环节,include标签写测试文件名
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

bugcoder-9905

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

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

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

打赏作者

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

抵扣说明:

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

余额充值