1、聚合-方便快速构建项目

  多个maven模块要构建需要分别执行一次maven构建命令,怎样只执行一次构建命令就构建多个maven模块呢?maven提供了聚合模块可以满足一次运行,构建多模块的要求

2、继承-消除重复配置,统一管理

  多个maven模块中的pxm.xml有很多相同的配置,如果简化配置?maven提供了继承机制,抽出重复的配置,统一依赖管理和插件管理

maven工程隐式的继承超级POM,超级POM如下:

<?xml version="1.0" encoding="UTF-8"?><!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
--><!-- START SNIPPET: superpom -->
<project><modelVersion>4.0.0</modelVersion><repositories><repository>   --中央仓库<id>central</id><name>Central Repository</name><url>https://repo.maven.apache.org/maven2</url><layout>default</layout><snapshots><enabled>false</enabled></snapshots></repository></repositories><pluginRepositories><pluginRepository>   --中央插件仓库<id>central</id><name>Central Repository</name><url>https://repo.maven.apache.org/maven2</url><layout>default</layout><snapshots><enabled>false</enabled></snapshots><releases><updatePolicy>never</updatePolicy></releases></pluginRepository></pluginRepositories><build>  --在子maven工程中设定相同的标签元素的值,则覆盖父POM的标签元素的值<directory>${project.basedir}/target</directory> --项目主输出目录<outputDirectory>${project.build.directory}/classes</outputDirectory> --主代码输出目录<finalName>${project.artifactId}-${project.version}</finalName> --最终构建的名称格式<testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory> --测试代码输出目录<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory> --主源码目录<scriptSourceDirectory>${project.basedir}/src/main/scripts</scriptSourceDirectory> --脚本源码目录<testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory> --测试源码目录<resources><resource> --主资源目录<directory>${project.basedir}/src/main/resources</directory></resource></resources><testResources> --测试资源目录<testResource><directory>${project.basedir}/src/test/resources</directory></testResource></testResources><pluginManagement>  --申明核心插件,设定了版本<!-- NOTE: These plugins will be removed from future versions of the super POM --><!-- They are kept for the moment as they are very unlikely to conflict with lifecycle mappings (MNG-4453) --><plugins><plugin><artifactId>maven-antrun-plugin</artifactId><version>1.3</version></plugin><plugin><artifactId>maven-assembly-plugin</artifactId><version>2.2-beta-5</version></plugin><plugin><artifactId>maven-dependency-plugin</artifactId><version>2.8</version></plugin><plugin><artifactId>maven-release-plugin</artifactId><version>2.3.2</version></plugin></plugins></pluginManagement></build><reporting><outputDirectory>${project.build.directory}/site</outputDirectory></reporting><profiles><!-- NOTE: The release profile will be removed from future versions of the super POM --><profile><id>release-profile</id><activation><property><name>performRelease</name><value>true</value></property></activation><build><plugins><plugin><inherited>true</inherited><artifactId>maven-source-plugin</artifactId><executions><execution><id>attach-sources</id><goals><goal>jar</goal></goals></execution></executions></plugin><plugin><inherited>true</inherited><artifactId>maven-javadoc-plugin</artifactId><executions><execution><id>attach-javadocs</id><goals><goal>jar</goal></goals></execution></executions></plugin><plugin><inherited>true</inherited><artifactId>maven-deploy-plugin</artifactId><configuration><updateReleaseInfo>true</updateReleaseInfo></configuration></plugin></plugins></build></profile></profiles></project>
<!-- END SNIPPET: superpom -->

以下元素可从超级POM获者其它的POM文件继承:

3、聚合和继承的关系

对于聚合模块,它知道有哪些被聚合的模块,但那些被聚合的模块不知道聚合模块的存在。

对于继承关系的父POM,它不知道有哪些子模块继承它,但那些子模块知道自己继承的父POM

相同点:聚合POM和继承关系中的父POM的packaging必须是pom,

    聚合模块和继承关系中到的父模块除了pom之外都没有实际内容

4、maven聚合工程(+父工程)

在实际项目中,通常聚合模块和继承关系中到的父模块是同一个,以下合并使用。两种常用的多模块的maven工程目录结构:

        

其中,parent既是聚合工程,也是父工程。

  • 新建聚合工程:略
  • parent模块的pom.xml文件
<?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.cn</groupId><artifactId>parent</artifactId><packaging>pom</packaging><version>1.0-SNAPSHOT</version><modules><module>../common</module>   --被聚合的common模块<module>../mananger</module></modules><name>parent</name><!-- FIXME change it to the project's website --><url>http://www.example.com</url><properties>  --定义属性的值,后续可以被引用<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.7</maven.compiler.source><maven.compiler.target>1.7</maven.compiler.target><spring-version>3.2.2.RELEASE</spring-version></properties><dependencies>  --需要引入的依赖<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency></dependencies><dependencyManagement>  --申明依赖,但实际不会被引入<dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${spring-version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring-version}</version></dependency></dependencies></dependencyManagement><build><pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->  --申明插件,但实际不会被引入<plugins><plugin><artifactId>maven-clean-plugin</artifactId><version>3.0.0</version></plugin><!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --><plugin><artifactId>maven-resources-plugin</artifactId><version>3.0.2</version></plugin><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.7.0</version></plugin><plugin><artifactId>maven-surefire-plugin</artifactId><version>2.20.1</version></plugin><plugin><artifactId>maven-jar-plugin</artifactId><version>3.0.2</version></plugin><plugin><artifactId>maven-install-plugin</artifactId><version>2.5.2</version></plugin><plugin><artifactId>maven-deploy-plugin</artifactId><version>2.8.2</version></plugin></plugins></pluginManagement></build>
</project>

  • 子模块manager的pom.xml
<?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"><parent>    --配置继承父工程<artifactId>parent</artifactId><groupId>com.cn</groupId><version>1.0-SNAPSHOT</version><relativePath>../parent/pom.xml</relativePath>   --父工程pom.xml的相对位置,未配置则默认为上一层目录下</parent><modelVersion>4.0.0</modelVersion><artifactId>mananger</artifactId> --省略了版本和组id,因为从父pom中继承了<packaging>war</packaging><name>mananger Maven Webapp</name><!-- FIXME change it to the project's website --><url>http://www.example.com</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.7</maven.compiler.source><maven.compiler.target>1.7</maven.compiler.target></properties><dependencies><dependency>  --实际引入了父pom中申明的jar包,只需指定id<groupId>org.springframework</groupId><artifactId>spring-core</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId></dependency></dependencies><build><plugins><plugin>  --实际引入父pom中申明的插件<artifactId>maven-compiler-plugin</artifactId><version>3.7.0</version><configuration><source>1.7</source><target>1.7</target></configuration></plugin></plugins></build>
</project>

  • 执行构建命令,多模块的maven工程构建过程

  Maven按序读取POM,如果该POM没有继承模块,就构建该模块,否则就先构建其继承的模块,如果该继承的模块还继承其它模块,则进一步先构建继承的模块。在此过程中,构建过的模块不再构建。(反应堆:所有模块组成的构建结构,包含了模块间的继承关系,能够计算出模块的构建顺序)

假设A项目聚合了B、C项目,B、C项目继承D项目,D继承E,则构建顺序为:A、E、D、B、C

转载于:https://www.cnblogs.com/shixiemayi/p/9536962.html

maven-聚合与继承相关推荐

  1. Maven 聚合与继承

    Maven 聚合与继承 1. 背景 在这个技术发展飞速的时代,各类用户对软件的要求越来越高,软件本身也变得越 来越复杂.因此,软件设计人员往往会采用各种方式对软件划分模块,以得到更清晰的 设计及更高的 ...

  2. 6.Maven聚合和继承,相关案例配置

     1有时候一个项目中有很多个模块,要想各个模块之间影响较小,可以将每个模块拿出来作为一个项目模块,对每个项目模块进行独立的开发. 2在这些过程中会遇到关于聚合和继承的问题. 3何为聚合? A如果我 ...

  3. Maven聚合和继承

    Part one 我们在开发过程中,创建了2个以上的模块,每个模块都是一个独立的maven project,在开始的时候我们可以独立的编译和测试运行每个模块,但是随着项目的不断变大和复杂化,我们期望能 ...

  4. Maven学习总结(五)——聚合与继承

    2019独角兽企业重金招聘Python工程师标准>>> Maven学习总结(五)--聚合与继承 一.聚合 如果我们想一次构建多个项目模块,那我们就需要对多个项目模块进行聚合 1.1. ...

  5. Maven详解之聚合与继承

    Maven详解之聚合与继承 说到聚合与继承我们都很熟悉,maven同样也具备这样的设计原则,下面我们来看一下Maven的pom如何进行聚合与继承的配置实现. 一.为什么要聚合? 随着技术的飞速发展和各 ...

  6. Maven学习总结(5)——聚合与继承

    2019独角兽企业重金招聘Python工程师标准>>> Maven学习总结(五)--聚合与继承 一.聚合 如果我们想一次构建多个项目模块,那我们就需要对多个项目模块进行聚合 1.1. ...

  7. java继承eclipse_Java-Maven(七):Eclipse中Maven依赖、聚合、继承特性

    之前通过学习了解,maven集成到eclipse中的如何创建项目,以及maven命令插件在eclipse中安装后的用法.那么接下来我们将会学习一些maven在项目中的一些特性,及如何使用. Maven ...

  8. Maven(3)--聚合与继承

    目录 聚合 重点: 继承 注意: 聚合与继承的关系区别 : 共同点 : Maven可继承的POM 元素 依赖管理 import依赖范围 插件管理 约定由于配置 反应堆 反应堆的构建顺序 裁剪反应堆 聚 ...

  9. Maven精选系列--继承与聚合

    转载自 Maven精选系列--继承与聚合 继承 什么是继承,我们可以定义项目的父类项目,用于继承父项目的依赖.插件.属性等信息. <parent> <groupId>com.a ...

  10. maven的聚合和继承详解(2021版)

    前言:日常开发中,两处常见的项目开发场景:多模块项目或者Springboot项目,都会用到Maven的聚合和继承,本篇博客就针对maven这两个技术点进行总结整理,希望能对你有所帮助! 在进入正题之前 ...

最新文章

  1. 新书预告 | 你肯定想读的一本Python好作品
  2. ModelSim之命令行仿真入门 (step 2)
  3. 16位代码段与32位代码段的区别
  4. python搭建简单http文件服务器
  5. 2020-2021年度第二届全国大学生算法设计与编程挑战赛 (秋季赛)-正式赛-详细题解
  6. mysql修改表结构权限_mysql 修改表结构操作
  7. unity官方教程-TANKS(一)
  8. Docker持续集成与容器管理--系列教程
  9. Javascript 链式运动框架——逐行分析代码,让你轻松了解运动的原理
  10. Redis在项目中的地位及使用场景剖析
  11. 【英语学习】【WOTD】encroach 释义/词源/示例
  12. 学而思编程python软件怎么样_学而思编程社区缺点详细分析
  13. python3.8使用pyttsx3报错_使用pyttsx3实现python语音播报
  14. NCBI安装影响因子插件
  15. 计算机软考里面的英语试题,2011全国计算机软考网管英语试题及答案(4)
  16. python气象卫星云图解析_python下载卫星云图合成gif
  17. 没有网服务器怎么打开网页,苹果手机没有浏览器怎么打开网页
  18. 从零学习Nginx配置文件,呕心沥血w字长文
  19. 计算机领域职业简介-PM,RD,FE,UE,UI,QA,OP,DBA,BRD,MRD,PRD,FSD等缩写的全称解析
  20. python爬取王者荣耀高清图

热门文章

  1. Mybatis复杂参数传参取参方式总结
  2. 《Metasploit渗透测试手册》—第3章3.5节在Windows 2003 Server上进行渗透测试
  3. 【web必知必会】—— 图解HTTP(转)good
  4. 高性能 TCP UDP 通信框架 HP-Socket v3.3.1
  5. combo 边写边选的注意事项
  6. 大数据时代 集群NAS更给力
  7. 【实用】CSS Border使用小分享——盒模型
  8. Hyper-V使用手记(一):无法引导安装FreeBSD7
  9. React Native Button使用
  10. startActivityForResult调用后立即响应了OnActivityResult()方法,导致setResult()后无响应