maven的聚合和继承(三)

一、聚合

当我们定义多个maven项目的时候,如果需要同时对这些项目进行打包编译,正常情况下会一一的进行打包编译操作,这样做会有些繁琐。这时我们会想,能不能只执行一次打包编译,就能对所有项目都有效?maven的聚合,就是为了解决这样的问题。

首先我们创建一个简单的maven项目xxx_parent,该项目是不作任何操作:



在Packageing中选择pom,意思是该项目是作为管理的项目。packaging中还有两个值:jar(打包成jar包)、war(打包成war包)



在pom.xml中使用modules标签对项目进行聚合操作,配置如下:(../表示相对路径)

<modules>
  	<module>../xxx_log</module>
  	<module>../xxx_service</module>
  	<module>../xxx_core</module>
  </modules>


经过综上配置以后,右键xxx_parent的pom.xml,执行clean package命令后,就会对xxx_log、xxx_service、xxx_core三个项目同时进行打包操作了。


二、继承

我们在创建了maven项目后,发现pom.xml里有些依赖是一样的,这些pom.xml文件中存在着大量的重复。为了不在每个pom.xml中都重复配置相同的依赖,

我们让项目都继承一个根类,在根类里面配置依赖,这样不仅不需要对项目进行重复的依赖配置,而且能对依赖进行统一的管理。这就是maven中jar包依赖的继承。

跟上面聚合一样,我们要创建一个pom的项目(即packaging=pom)作为根类管理项目,在该项目的pom.xml文件里,我们把需要用到的jar依赖都拷贝进来。

子类项目要使用根类项目的依赖时使用parent标签实现继承(relativePath指的是相对路径,指向根类项目的pom.xml文件):

<parent>
		<groupId>com.carlo</groupId>
	  	<artifactId>xxx_parent</artifactId>
	  	<version>0.0.1-SNAPSHOT</version>
	  	<relativePath>../xxx_parent/pom.xml</relativePath>
	</parent>


值得注意的是,我们在根类定义依赖的时候,不能直接使用:

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
因为在根类项目中使用这种方式去声明依赖的话,在所有子类中,都会直接继承这个依赖。然而,并不是每个子类都需要这个依赖。


所以这里我们在根类中使用一个叫依赖管理的标签<dependencyManagement>,来管理依赖,这种方式不会直接被子类项目所继承。

因为在根类项目中已经声明了依赖的版本号(version)和作用域(scope),所以当子类项目需要用到某个依赖时,

只需在自己项目的pom.xml中只需声明依赖的 groupId 和 artifactId ,就会从根类项目中继承获取到对应的依赖。
如果有使用<exclusions>标签排除依赖,在根类项目中声明了,子类项目就不需要再次声明了(如下面示例中的xx/yy.0.0.jar包,

因为在根类项目中已经定义了依赖解除,所以子类项目中就不需要再次声明了)。

【这里我们可以把 groupId 和 artifactId 理解为坐标,子类项目通过这两个值(坐标)去匹配获取到根类项目中声明的对应依赖】

根类pom.xml示例:

<pre name="code" class="html"><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.carlo</groupId>
  <artifactId>xxx_parent</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  <build/>
  
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  
  
  <dependencyManagement>
  	<dependencies>
  		<dependency>
	      <groupId>junit</groupId>
	      <artifactId>junit</artifactId>
	      <version>3.8.1</version>
	      <scope>test</scope>
	    </dependency>
	    
  		<dependency>
	      <groupId>xx</groupId>
	      <artifactId>yyy</artifactId>
	      <version>0.0.1</version>
	      <exclusions>
	      	<exclusion>
	      		<groupId>aa</groupId>
	      		<artifactId>bb</artifactId>
	      	</exclusion>
	      </exclusions>
	    </dependency>
  	</dependencies>
  </dependencyManagement>
  
</project>

 


子类pom.xml示例:

<pre name="code" class="html"><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.carlo</groupId>
	  	<artifactId>xxx_parent</artifactId>
	  	<version>0.0.1-SNAPSHOT</version>
	  	<relativePath>../xxx_parent/pom.xml</relativePath>
	</parent>
	

  <artifactId>xxx_log</artifactId>
  <packaging>jar</packaging>

  <name>xxx_log</name>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
    </dependency>
    
    <dependency>
	      <groupId>xx</groupId>
	      <artifactId>yyy</artifactId>
	  </dependency>
    
  </dependencies>
</project>

 


综上,就是聚合和继承的内容,我们发现,聚合和继承很类似,都是需要定义一个根类来作统一管理。

所以平时工作中,习惯把聚合和继承放到同一个pom管理项目中。

pom.xml示例:

<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.carlo</groupId>
  <artifactId>xxx_parent</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  <build/>
  
  <!-- 聚合 -->
  <modules>
  	<module>../xxx_log</module>
  	<module>../xxx_service</module>
  	<module>../xxx_core</module>
  </modules>
  
  <!-- 继承 -->
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencyManagement>
  	<dependencies>
  		<dependency>
	      <groupId>junit</groupId>
	      <artifactId>junit</artifactId>
	      <version>3.8.1</version>
	      <scope>test</scope>
	    </dependency>
	    
  		<dependency>
	      <groupId>xx</groupId>
	      <artifactId>yyy</artifactId>
	      <version>0.0.1</version>
	      <exclusions>
	      	<exclusion>
	      		<groupId>aa</groupId>
	      		<artifactId>bb</artifactId>
	      	</exclusion>
	      </exclusions>
	    </dependency>
  	</dependencies>
  </dependencyManagement>
  
</project>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值