maven 聚合、继承详解

项目

  1. github 建个仓库
  2. 执行 git clone 克隆项目到本地
  3. idea 新建项目
    在这里插入图片描述

Maven聚合

<modules>
    <module>模块1</module>
    <module>模块2</module>
    <module>模块n</module>
</modules>
<package>pom</package>

新的项目中执行任何mvn命令,都会modules中包含的所有模块执行同样的命令,而被包含的模块不需要做任何特殊的配置,正常的maven项目就行

rpc 模块创建

不继承父pom, parent:None
在这里插入图片描述

继承
  1. 创建一个父maven构件,将依赖信息放在pom.xml中

    <dependencies>
       <dependency>依赖的构件的坐标信息</dependency>
       <dependency>依赖的构件的坐标信息</dependency>
       <dependency>依赖的构件的坐标信息</dependency>
    </dependencies>
    
  2. 将父构件的package元素的值置为pom

    <packaging>pom</packaging>
    
  3. 在子构件的pom.xml引入父构件的配置:

    <parent>
       <groupId>父构件groupId</groupId>
       <artifactId>父构件artifactId</artifactId>
       <version>父构件的版本号</version>
       <relativePath>父构件pom.xml路径</relativePath>
    </parent>
    

relativePath表示父构件pom.xml相对路径,默认是../pom.xml,所以一般情况下父子结构的maven构件在目录结构上一般也采用父子关系。

在这里插入图片描述
pom.xml 文 件
在这里插入图片描述

pom 依赖配置

mvn dependency:tree这个插件可以根据pom.xml的配置,列出构件的依赖树信息。

relativePath元素

父构件和子构件的目录结构刚好符合父子关系,如果父构件和子构件的目录不是父子关系,比如都位于同等级别的目录或者位于更复杂的目录的时候,此时我们需要在子pom.xmlparent元素中使用relativePath元素来指定父pom.xml相对路径位置,这个值我们上面没有指定,默认是../pom.xml,表示父pom.xml位于子pom.xml的上一级目录,我们的模块刚好符合这种关系,所以这个值省略了。

正确的设置relativePath是非常重要的,这个需要注意,子模块中执行mvn命令的时候,会去找父pom.xml的配置,会先通过relativePath指定的路径去找,如果找不到,会尝试通过坐标在本地仓库进行查找,如果本地找不到,会去远程仓库找,如果远程仓库也没有,会报错。

可以通过继承的元素有以下这些
  • groupId:项目组ID,项目坐标的核心元素
  • version:项目版本,项目坐标的核心元素
  • description:项目的描述信息
  • organization:项目的组织信息
  • inceptionYear:项目的创始年份
  • url:项目的url地址
  • developers:项目的开发者信息
  • contributors:项目的贡献者信息
  • distributionManagement:项目的部署配置信息
  • issueManagement:项目的缺陷跟踪系统信息
  • ciManagement:项目的持续集成系统信息
  • scm:项目的版本控制系统信息
  • mailingLists:项目的邮件列表信息
  • properties:自定义的maven属性配置信息
  • dependencyManagement:项目的依赖管理配置
  • repositories:项目的仓库配置
  • build:包括项目的源码目录配置、输出目录配置、插件管理配置等信息
  • reporting:包括项目的报告输出目录配置、报告插件配置等信息

依赖管理(dependencyManagement)

在新增一个子构件,都会默认从父构件中继承依赖的一批构建,父pom.xml中配置的这些依赖的构建可能是其他项目不需要的,可能某个子项目只是想使用其中一个构件,但是上面的继承关系却把所有的依赖都给传递到子构件中了,这种显然是不合适的。

maven提供的dependencyManagement元素既能让子模块继承到父模块的依赖配置,又能保证子模块依赖使用的灵活性,dependencyManagement元素下声明的依赖不会引入实际的依赖,他只是声明了这些依赖,不过它可以对dependencies中使用的依赖起到一些约束作用。

子模块如果想用到这些配置,可以dependencies进行引用,引用之后,依赖才会真正的起效。并且版本号可以省略。

使用dependencyManagement来解决继承的问题,子pom.xml中只用写groupId,artifactId就可以了,其他信息都会从父dependencyManagement中声明的依赖关系中传递过来,通常我们使用这种方式将所有依赖的构建在父pom.xml中定义好,子构件中只需要通过groupId,artifactId就可以引入依赖的构建,而不需要写version,可以很好的确保多个子项目中依赖构件的版本的一致性,对应依赖构件版本的升级也非常方便,只需要在父pom.xml中修改一下就可以了。

单继承问题

dependencyManagement的使用,但是有个问题,只有使用继承的时候,dependencyManagement中声明的依赖才可能被子pom.xml用到,如果我的项目本来就有父pom.xml了,但是我现在想使用另外一个项目dependencyManagement中声明的依赖,此时我们怎么办?这就是单继承的问题.

当我们想在项目中使用另外一个构件中dependencyManagement声明的依赖,而又不想继承这个项目的时候,可以在我们的项目中使用加入下面配置:

     <dependencyManagement>
        <dependencies>
            <dependency>
                <!-- spring cloud 依赖-->
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

插件管理(pluginManagement)

maven中提供了dependencyManagement来解决继承的问题,同样也提供了解决插件继承问题的pluginManagement元素,在父pom中可以在这个元素中声明插件的配置信息,但是子pom.xml中不会引入此插件的配置信息,只有在子pom.xml中使用plugins->plugin元素正在引入这些声明的插件的时候,插件才会起效,子插件中只需要写groupIdartifactId,其他信息都可以从父构件中传递过来.

pom.xml中写上插件的groupId、artifactId就可以了,其他信息会从父pom.xml中插件的定义中传递过来,而子pom.xml中也可以自定义插件的这些配置

案例

pom.xml

 <pluginManagement>
            <plugins>
                <!--
                verify是在测试完成之后并将构件安装到本地仓库之前执行的阶段,
                在这个阶段我们生成源码
                -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-source-plugin</artifactId>
                    <version>3.2.1</version>
                    <executions>
                        <!-- 使用插件需要执行的任务 -->
                        <execution>
                            <!-- 任务id -->
                            <id>attach-source</id>
                            <!-- 任务中插件的目标,可以指定多个 -->
                            <goals>
                                <goal>jar-no-fork</goal>
                            </goals>
                            <!-- 绑定的阶段 -->
                            <phase>verify</phase>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>

pom.xml

 <!--
            生成源码
            其他信息会从父pom.xml中插件的定义中传递过来,
            但子pom.xml中也可以自定义插件的这些配置。
            父子pom.xml中插件配置信息会合并。

            可以通过 mvn help:effective-pom 命令解析得到这个构件最终 pom.xml 的内容。
            -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <executions>
                    <execution>
                        <id>attach-source</id>
                        <goals>
                            <goal>help</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

父子pom.xml中插件配置信息会合并。可以通过 mvn help:effective-pom 命令解析得到这个构件最终 pom.xml的内容

聚合与继承的关系

  • 聚合主要是为了方便多模块快速构建。
  • 而继承主要是为了重用相同的配置。

代码编译时文件编码配置

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

编译代码的时候,涉及到资源文件和测试资源文件的拷贝,拷贝文件的时候涉及到文件的编码,这个是设置文件的编码为UTF-8格式的.

执行命令查看插件目标 resources 的详细参数
mvn help:describe -Dplugin=resources -Dgoal=resources -Ddetail
输出内容如下:

  encoding (Default: ${project.build.sourceEncoding})
      The character encoding scheme to be applied when filtering resources.

encoding这个参数用来指定编码的,默认值是${project.build.sourceEncoding},也可以通过encoding用户属性来设置。

所以设置编码的共四种:

pom.xml中2种:
<encoding>UTF-8</encoding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

mvn命令中2种:
mvn compile -Dencoding=UTF-8
mvn compile -Dproject.build.sourceEncoding=UTF-8

mvn test 命令

mvn test运行测试用例的时候,测试用例类名的写法默认是有规则的,这些规则有人知道么?从哪里可以看到这些规则?如何自定义?

目标详细参数

$ mvn help:describe -Dplugin=surefire -Dgoal=test -Ddetail
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-application 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-help-plugin:3.2.0:describe (default-cli) @ maven-application ---
[INFO] Mojo: 'surefire:test'
surefire:test
  Description: Run tests using Surefire.
  Implementation: org.apache.maven.plugin.surefire.SurefirePlugin
  Language: java
  Bound to phase: test

  Available parameters:
   includes
      A list of <include> elements specifying the tests (by pattern) that
      should be included in testing. When not specified and when the test
      parameter is not specified, the default includes will be
      <includes>
       <include>**/Test*.java</include>
       <include>**/*Test.java</include>
       <include>**/*Tests.java</include>
       <include>**/*TestCase.java</include>
      </includes>

      Each include item may also contain a comma-separated sub-list of items,
      which will be treated as multiple  <include> entries.
      Since 2.19 a complex syntax is supported in one parameter (JUnit 4, JUnit
      4.7+, TestNG):

可以看到上面有个includes参数,可以用来配置需要运行的测试用例,可以配置通配符的方式。

上面还有一段信息:

Implementation: org.apache.maven.plugin.surefire.SurefirePlugin

上面这部分列出了这个目标的具体实现类是SurefirePlugin。

查看实现类:SurefirePlugin 中 的 includes 的默认值

 protected String[] getDefaultIncludes() {
        return new String[]{"**/Test*.java", "**/*Test.java", "**/*Tests.java", "**/*TestCase.java"};
    }

这部分代码就是我们测试用例默认需要满足的格式,你创建的测试用例默认情况下必须满足上面这3种格式,否则,测试用例不会被mvn test执行

参考

聚合、继承、单继承问题详解

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值