首页 > 代码库 > Maven中的继承和聚合

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>