【Maven】进阶
  pgSCHb2FgBf2 2023年12月07日 19 0



文章目录

  • 1. 聚合
  • 2. 继承
  • 3. 属性变量定义与使用
  • 4. 版本管理
  • 5. 资源配置
  • 6. 多环境配置
  • 7. 跳过测试(了解)


1. 聚合

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

【Maven】进阶_xml

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

【Maven】进阶_数据库_02

构建顺序按照依赖顺序

2. 继承

【Maven】进阶_maven_03

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

【Maven】进阶_spring_04

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

父工程的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>     <!-- 子工程的版本,由于写了父工程,尽量和父工程一致,可省略 -->

【Maven】进阶_java_05

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

继承与聚合

  1. 作用
  • 聚合需要配置modules标签,用于快速构建项目,root进行build,所有的工程都需要build
  • 继承需要配置parent标签,用于简化配置,父工程配置过,子工程直接继承
  1. 相同点
  • 聚合与继承的pom.xml文件打包方式均为pom,可以将两种关系制作到同一个pom文件中
  • 聚合与继承均属于设计型模块,并无实际的内容,比如src
  1. 不同点
  • 聚合是在root模块中配置关系(modules),聚合可以感知到参与聚合的模块有哪些
  • 继承是在子模块中配置关系(parent),父模块无法感知哪些子模块继承了自己

3. 属性变量定义与使用

【Maven】进阶_spring_06


写版本的时候不再写具体的版本号,而是直接写变量

父工程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中配置的属性,其实是可以在配置文件中通过${}的方式使用的

【Maven】进阶_java_07

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

【Maven】进阶_maven_08


然后在ssm_dao的.properties中使用

【Maven】进阶_maven_09

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

【Maven】进阶_maven_10

【Maven】进阶_xml_11

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

【Maven】进阶_java_12

【Maven】进阶_maven_13

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

6. 多环境配置

不同环境加载不同配置,不要随意更改配置

【Maven】进阶_maven_14


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

【Maven】进阶_maven_15

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

7. 跳过测试(了解)

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

跳过全部测试环节

【Maven】进阶_xml_16

跳过指定测试环节,include标签写测试文件名

【Maven】进阶_maven_17


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年12月07日 0

暂无评论

推荐阅读
pgSCHb2FgBf2