Maven中的继承和聚合

一. 继承和聚合

1. 什么是继承?

maven为了消除重复,把很多相同的配置提取出来, 放入一个父项目的pom.xml中
例如:grouptId,version等


2. 什么是聚合?

如果我们想一次构建多个项目模块,那我们就需要对多个项目模块进行聚合
<modules>

<module>…</module>

</modules>


3. 继承与聚合的关系
继承主要为了消除重复

聚合主要为了快速构建项目


二. 继承聚合配置

1. 聚合配置代码:

<modules>
		<module>../Hello</module>
		<module>../HelloFriend</module>
		<module>../MakeFriends</module>
	</modules>
其中module的路径为相对路径。


2. 继承配置代码:

Parent项目(父):

<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.zdp.maven</groupId>
	<artifactId>Parent</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>
	<name>Parent</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
	
	<modules>
		<module>../Hello</module>
		<module>../HelloFriend</module>
		<module>../MakeFriends</module>
	</modules>
	
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>junit</groupId>
				<artifactId>junit</artifactId>
				<version>4.9</version>
				<scope>test</scope>
			</dependency>
			<dependency>
				<groupId>com.zdp.maven</groupId>
				<artifactId>Hello</artifactId>
				<version>0.0.1-SNAPSHOT</version>
				<scope>compile</scope>
			</dependency>
			<dependency>
				<groupId>com.zdp.maven</groupId>
				<artifactId>HelloFriend</artifactId>
				<version>0.0.1-SNAPSHOT</version>
				<scope>compile</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
</project>

父模块中将用dependencyManagement进行管理


MakeFriends项目(子):
<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>
	<artifactId>MakeFriends</artifactId>
	<packaging>jar</packaging>
	<name>MakeFriends</name>
	
	<parent>
		<groupId>com.zdp.maven</groupId>
		<artifactId>Parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
		<relativePath>../Parent/pom.xml</relativePath>
	</parent>
	
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
		</dependency>
		<dependency>
			<groupId>com.zdp.maven</groupId>
			<artifactId>HelloFriend</artifactId>
		</dependency>
	</dependencies>
</project>

HelloFriend项目(子):

<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>
	<artifactId>HelloFriend</artifactId>
	<packaging>jar</packaging>
	<name>HelloFriend</name>
	
	<parent>
		<groupId>com.zdp.maven</groupId>
		<artifactId>Parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
		<relativePath>../Parent/pom.xml</relativePath>
	</parent>
	
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
		</dependency>
		<dependency>
			<groupId>com.zdp.maven</groupId>
			<artifactId>Hello</artifactId>
		</dependency>
	</dependencies>
</project>

Hello项目(子):

<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>
	<artifactId>Hello</artifactId>
	<packaging>jar</packaging>
	<name>Hello</name>
	
	<parent>
		<groupId>com.zdp.maven</groupId>
		<artifactId>Parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
		<relativePath>../Parent/pom.xml</relativePath>
	</parent>
	
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
		</dependency>
	</dependencies>
</project>




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值