美文网首页
Maven 聚合工程

Maven 聚合工程

作者: YMeng_Zhang | 来源:发表于2021-10-10 20:24 被阅读0次

Maven 基础知识

  • Maven 介绍

Maven 是一个项目管理工具,主要作用是在项目开发阶段对 Java 项目进行依赖管理和项目构建。

依赖管理:是对 jar 包的管理。通过导入 maven 坐标,就相当于将仓库中的 jar 包导入了当前项目中。

项目构建:通过 maven 的一个命令就可以完成项目从清理、编译、测试、报告、打包,部署整个过程。

  • Maven 的仓库类型

本地仓库

远程仓库

  1. Maven 中央仓库(http://repo2.maven.org/maven2/
  2. Maven 私服(公司局域网内的仓库,需要自己搭建)
  3. 其他公共远程仓库(例如 apache 提供的远程仓库,http://repo.maven.apache.org/maven2/

本地仓库 ---> maven 私服 --> maven 中央仓库

  • Maven 常用命令

clean 清理

compile 编译

test 测试

package 打包

install 安装

Maven 坐标书写规范

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.47</version>
</dependency>

Maven 的依赖传递

  • 什么是依赖传递

在 maven 中,依赖是可以传递的,假设存在三个项目,分别是项目 A,项目 B 以及项目C。假设 C 依赖 B,B 依赖 A,那么可以根据 Maven 项目依赖的特征推出项目 C 也依赖 A。

...
<!-- springMVC 相关坐标-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.1.5.RELEASE</version>
</dependency>
...

如上所示,当 web 项目直接依赖了 spring-webmvc,而 spring-webmvc 依赖了 sping-aop、spring-beans 等。最终的结果就是在 web 项目
中间接依赖了 spring-aop、spring-beans 等。

  • 依赖冲突

...
<!-- springMVC-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.1.5.RELEASE</version>
</dependency>
<!-- springAOP-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>5.1.6.RELEASE</version>
</dependency>
...

如上所示,由于依赖传递现象的存在,spring-webmvc 依赖 spring-beans-5.1.5,而spring-aop 依赖 spring-beans-5.1.6,但是发现 spirng-beans-5.1.5 已经加入到了工程中,这时候如果希望 spring-beans-5.1.6 加入工程,就造成了依赖冲突。

  • 如何解决依赖冲突

  1. 使用 maven 提供的依赖调解原则:第一声明者优先原则、路径近者优先原则
  2. 排除依赖
  3. 锁定版本
  • 依赖调节原则---第一声明者优先原则

在 POM 文件中定义依赖,以先声明的依赖为准。其实就是根据坐标导入的顺序来确定最终使用哪个传递过来的依赖。

...
<!-- springAOP 第一声明者优先使用 -->
<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-aop</artifactId>
   <version>5.1.6.RELEASE</version>
</dependency>
<!-- springMVC-->
<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
   <version>5.1.5.RELEASE</version>
</dependency>
...

结论:spring-aop 和 spring-webmvc 都传递过来了 spring-beans,但是因为 spring-aop 在前面,所以最终使用的 spring-beans 是由 spring-aop 传递过来的,而 spring-webmvc 传递过来的 spring-beans 则被忽略了。

  • 依赖调节原则---路径近者优先原则

...
<!-- springAOP-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>5.1.6.RELEASE</version>
</dependency>
<!-- springMVC-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.1.5.RELEASE</version>
</dependency>
<!-- 直接依赖 spring-beans,路径更近,优先使用 5.1.7 版本 -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>5.1.7.RELEASE</version>
</dependency>
...

总结:直接依赖大于依赖传递。

  • 排除依赖

可以使用 exclusions 标签将传递过来的依赖排除出去。

...
<!-- springMVC-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.1.5.RELEASE</version>
    <!-- 排除依赖 5.1.5 版本的 spring-beans -->
    <exclusions>
        <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<!-- springAOP -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>5.1.6.RELEASE</version>
</dependency>
...
  • 版本锁定

采用直接锁定版本的方法确定依赖 jar 包的版本,版本锁定后则不考虑依赖的声明顺序或依赖的路径,以锁定的版本为准添加到工程中,此方法在企业开发中经常使用。

版本锁定的使用方式:

第一步 - 在 dependencyManagement 标签中锁定依赖的版本
第二步 - 在 dependencies 标签中声明需要导入的 maven 坐标

① 在 dependencyManagement 标签中锁定依赖的版本

...
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.1.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.5.RELEASE</version>
        </dependency>
    </dependencies>
</dependencyManagement>
...

②在 dependencies 标签中声明需要导入的 maven 坐标

...
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.1.5.RELEASE</version>
    </dependency>
    <!-- 对应的 jar 包就无需配置版本号  -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>
</dependencies>
...
  • properties 标签的使用

...

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
    <java.version>1.11</java.version>
    <maven.compiler.source>1.11</maven.compiler.source>
    <maven.compiler.target>1.11</maven.compiler.target>
    <spring.version>5.1.5.RELEASE</spring.version>
    <springmvc.version>5.1.5.RELEASE</springmvc.version>
    <mybatis.version>3.5.1</mybatis.version>
</properties>

...

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    ...
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>${mybatis.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${springmvc.version}</version>
    </dependency>
    ,,,
</dependencies>
,,,

Maven 聚合工程(分模块)

概念:在现实生活中,汽车厂家进行汽车生产时,由于整个生产过程非常复杂和繁琐,工作量非常大,所以厂家都会将整个汽车的部件分开生产,最终再将生产好的部件进行组装,形成一台完整的汽车。

  • 分模块构建 maven 工程分析

在企业项目开发中,由于项目规模大,业务复杂,参与的人员比较多,一般会通过合理的模块拆分将一个大型的项目拆分为 N 多个小模块,分别进行开发。而且拆分出的模块可以非常容易的被其他模块复用。

常见的拆分方式有两种:

  1. 按照业务模块进行拆分,每个模块拆分成一个 maven 工程,例如将一个项目分为用户模块,订单模块,购物车模块等,每个模块对应就是一个 maven 工程
  2. 按照层进行拆分,例如持久层、业务层、表现层等,每个层对应就是一个 maven 工程

不管上面哪种拆分方式,通常都会提供一个父工程,将一些公共的代码和配置提取到父工程中进行统一管理和配置。

父工程:资源的统一管理 - 依赖管理、版本锁定

按层拆分

  • maven_web 依赖 mavent_service 依赖 mavent_dao 依赖 maven_pojo

  • maven_web、mavent_service、mavent_dao、maven_pojo 四个工程都继承 maven_parent

  • Maven 工程的继承

在 Java 语言中,类之间是可以继承的,通过继承,子类就可以引用父类中非 private 的属性和方法。同样,在 maven 工程之间也可以继承,子工程继承父工程后,就可以使用在父工程中引入的依赖。继承的目的是为了消除重复代码。

被继承的 Maven 工程通常称为父工程,父工程的打包方式必须为 POM,所以区分某个 Maven 工程是否为父工程就看这个工程的打包方式是否为 POM:

<?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.renda</groupId>
    <artifactId>edu_home_parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <modules>
        <module>ssm-utils</module>
        <module>ssm-domain</module>
        <module>ssm-dao</module>
        <module>ssm-service</module>
        <module>ssm-web</module>
    </modules>

    <properties>
        <spring.version>5.1.5.RELEASE</spring.version>
        <springmvc.version>5.1.5.RELEASE</springmvc.version>
        <mybatis.version>3.5.1</mybatis.version>
    </properties>

    <!-- 统一版本锁定 -->
    <dependencyManagement>
        <dependencies>
            ...
        </dependencies>
    </dependencyManagement>

    <!-- 统一依赖管理 -->
    <dependencies>
        ...
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

继承其他 Maven 父工程的工程通常称为子工程,在 pom.xml 文件中通过 parent 标签进行父工程的继承:

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

    <parent>
        <groupId>com.renda</groupId>
        <artifactId>edu_home_parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>ssm-web</artifactId>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>com.lagou</groupId>
            <artifactId>ssm-service</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>
  • Maven 工程的聚合

在 maven 工程的 pom.xml 文件中可以使用标签将其他 maven 工程聚合到一起,聚合的目的是为了进行统一操作。

例如拆分后的 maven 工程有多个,如果要进行打包,就需要针对每个工程分别执行打包命令,操作起来非常繁琐。这时就可以使用标签将这些工程统一聚合到 maven 父工程中,需要打包的时候,只需要在此工程中执行一次打包命令,其下被聚合的工程就都会被打包了。

相关文章

网友评论

      本文标题:Maven 聚合工程

      本文链接:https://www.haomeiwen.com/subject/wizholtx.html