Maven - 继承和聚合

*聚合(多模块)

             在一个项目中 往往有多个模块组成  例如有项目demo  下面有a, b两个模块

             为了能使用一条命令就能构建demo-a, demo-b两个模块, 需要创建一个额外的聚合模块, 然后通过该模块构建整个项目的所有模块。

              聚合模块(demo-parent) pom:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.x.demo</groupId>
  <artifactId>demo-parent</artifactId>
  <packaging>pom</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>demo-parent Maven Webapp</name>
  <url>http://maven.apache.org</url>
  
  <modules>
  	<module>../demo-a</module>
  	<module>../demo-b</module>
  </modules>
</project>

             模块a(demo-a) pom:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.x.demo</groupId>
  <artifactId>demo-a</artifactId>
  <packaging>jar</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>demo-a Maven Webapp</name>
  <url>http://maven.apache.org</url>
  
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  
  <build>
    <finalName>demo-a</finalName>
  </build>
</project>

             模块b(demo-b) pom:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.x.demo</groupId>
  <artifactId>demo-b</artifactId>
  <packaging>jar</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>demo-b Maven Webapp</name>
  <url>http://maven.apache.org</url>
  
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  
  <build>
    <finalName>demo-b</finalName>
  </build>
</project>

      额外模块(demo-parent) pom中的modules节点有多个module,每个module的值都是一个被聚合模块的相对目录。

     关于聚合模块目录与其他模块的目录主要有两种形式:

      a.父子关系:

     

         父子关系  聚合模块的pom:

  <modules>
  	<module>demo-a</module>
  	<module>demo-b</module>
  </modules>

      a.平行目录:

    

         平行目录  聚合模块的pom:

  <modules>
  	<module>../demo-a</module>
  	<module>../demo-b</module>
  </modules>

    最后在聚合模块(demo-parent)的pom上面 运行mvn命令 根据聚合顺序依次构建多个模块。

*继承

       上面的例子中, a,b两个模块都依赖junit, 为了消除重复,可以使用pom的继承,以达到一处声明,多处使用的目的。

      聚合模块(demo-parent) pom:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.x.demo</groupId>
  <artifactId>demo-parent</artifactId>
  <packaging>pom</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>demo-parent Maven Webapp</name>
  <url>http://maven.apache.org</url>
  
   <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
   <modules>
  	<module>../demo-a</module>
  	<module>../demo-b</module>
  </modules>
</project>

             模块a(demo-a) pom:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  
  <parent>
    <groupId>com.x.demo</groupId>
    <artifactId>demo-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <relativePath>../demo-parent/pom.xml</relativePath>
  </parent>
  
  <artifactId>demo-a</artifactId>
  <name>demo-a Maven Webapp</name>
  
  <build>
    <finalName>demo-a</finalName>
  </build>
</project>

             模块b(demo-b) pom:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.x.demo</groupId>
    <artifactId>demo-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <relativePath>../demo-parent/pom.xml</relativePath>
  </parent>
  
  <artifactId>demo-b</artifactId>
  <name>demo-a Maven Webapp</name>
  
  <build>
    <finalName>demo-b</finalName>
  </build>
</project>

   parent元素声明父模块,parent下的子节点groupId,artifactId,version指定父模块的坐标,这三个元素是必须的。

   节点relativePath指定父模块pom的路径,默认值是:../pom.xml,也就是说父pom在上一层目录,(<relativePath>../demo-parent/pom.xml</relativePath> 表示父模块pom和子模块是平行目录)

可以被继承的POM元素

groupId:项目id,项目坐标的核心元素

version:项目版本,项目坐标的核心元素

description:项目描述信息

organization:项目组织信息

inceptionYear:项目创世年月

developers:项目开发者信息

contributors:项目贡献者信息

distributionManagement:项目部署配置

scm:项目的版本控制信息

mailingLists:项目邮件列表信息

properties:自定义的属性

dependencies:项目的依赖配置

dependencyManagement:项目的依赖管理配置

repositories:项目的仓库配置

build:项目源码目录配置。输出目录配置,插件配置等。

*依赖管理

             父模块(demo-parent) pom:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.x.demo</groupId>
  <artifactId>demo-parent</artifactId>
  <packaging>pom</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>demo-parent Maven Webapp</name>
  <url>http://maven.apache.org</url>

	<!-- dependencyManagement 定义的依赖  需要在子pom中声明  不然不会产生效果 -->
	<dependencyManagement>  
 	  <dependencies>
	    <dependency>
	      <groupId>junit</groupId>
	      <artifactId>junit</artifactId>
	      <version>3.8.1</version>
	      <scope>test</scope>
		</dependency>
  	</dependencies>
 </dependencyManagement>
  
  <modules>
  	<module>../demo-a</module>
  	<module>../demo-b</module>
  </modules>
</project>

             模块a(demo-a) pom:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  
  <parent>
    <groupId>com.x.demo</groupId>
    <artifactId>demo-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <relativePath>../demo-parent/pom.xml</relativePath>
  </parent>
  
  <artifactId>demo-a</artifactId>
  <name>demo-a Maven Webapp</name>

  <properties>
  	<mail.version>1.4.1</mail.version>
  </properties>
  
   <dependencies>
   		<!-- 声明父pom中的依赖  这样才会真正被使用 -->
	    <dependency>
	      <groupId>junit</groupId>
	      <artifactId>junit</artifactId>
		</dependency>
		
		<!-- 扩展依赖  父pom中并没有声明 不会影响到父POM 和其他模块 -->
		<dependency>
			<groupId>javax.mail</groupId>
			<artifactId>mail</artifactId>
			<version>${mail.version}</version>
		</dependency>
  	</dependencies>
  	
  <build>
    <finalName>demo-a</finalName>
  </build>
</project>
             模块b(demo-b) pom:
<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.x.demo</groupId>
    <artifactId>demo-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <relativePath>../demo-parent/pom.xml</relativePath>
  </parent>
  
  <artifactId>demo-b</artifactId>
  <name>demo-a Maven Webapp</name>
  
  <!-- 没有声明父pom中的依赖  不会被使用 -->
  
  <build>
    <finalName>demo-b</finalName>
  </build>
</project>

父POM 中使用dependencyManagement 声明的依赖不会给子模块引入依赖, 只会继承这段配置。

在模块a(demo-a) pom中声明了父pom中junit依赖 在执行的时候才会获得真正的依赖信息。

模块b(demo-b) pom中没有声明父pom中junit依赖 不会产生实际效果。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值