1.聚合
一次构建多个项目模块,将多个Maven项目合并为一个大的项目。
2.继承
为了消除重复,把很多相同的配置提取出来,例如groupid和version;
3.示例
3.1创建4个Maven项目,放在相同的目录下,其中hello_parent为父项目(聚合模块),hello_1/hello_2/hello_3为子项目(被聚合的模块);hello_parent的POM既是聚合POM,又是父POM这么做主要是为了方便。
3.1.1 hello_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/xsd/maven-4.0.0.xsd"> <!-- 当前pom的版本号 --> <modelVersion>4.0.0</modelVersion> <!-- groupId: 当前jar所属的命名空间 --> <groupId>cn.test.maven</groupId> <!-- 当前项目模块名称 --> <artifactId>parent</artifactId> <!-- 当前项目的版本, SNAPSHOT镜像版 --> <version>0.0.1-SNAPSHOT</version> <!-- 聚合、父继承的packaging必须是pom --> <packaging>pom</packaging> <!-- name提供一个相对容易读的名字,合理配name字段,会让Mavne的构建输出更清晰 --> <name>Parent</name> <url>http://maven.apache.org</url> <!-- 定义要聚合的模块 --> <modules> <!-- 每个module的值都是当前pom的相对路径 --> <module>../hello_ch1</module> <module>../hello_ch2</module> <module>../hello_ch3</module> </modules> <!-- 定义属性 --> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <junit.version>4.9</junit.version> <maven.version>0.0.1-SNAPSHOT</maven.version> </properties> <!-- 用dependencyManagement进行jar包依赖管理 --> <dependencyManagement> <!-- 配置jar包依赖 --> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <!-- 访问junit.version属性 --> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>cn.test.maven</groupId> <artifactId>hello_ch1</artifactId> <!-- 访问maven.version属性 --> <version>${maven.version}</version> <scope>compile</scope> </dependency> <dependency> <groupId>cn.test.maven</groupId> <artifactId>hello_ch2</artifactId> <!-- 访问maven.version属性 --> <version>${maven.version}</version> </dependency> </dependencies> </dependencyManagement> </project>
3.1.2 子项目1 POM(hello_ch1)
<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_ch1</artifactId> <!-- 继承parent项目中的pom.xml配置 --> <parent> <groupId>cn.test.maven</groupId> <artifactId>parent</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- 相对路径 --> <relativePath>../hello_parent/pom.xml</relativePath> </parent> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> </dependencies> </project>
3.1.3 子项目2 POM(hello_ch2)
<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_ch2</artifactId> <name>hello_ch2</name> <!-- 继承parent项目中的pom.xml配置 --> <parent> <groupId>cn.test.maven</groupId> <artifactId>parent</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../hello_parent/pom.xml</relativePath> </parent> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> <dependency> <groupId>cn.test.maven</groupId> <artifactId>hello_ch1</artifactId> </dependency> </dependencies> </project>
3.1.4 子项目3 POM(hello_ch3)
<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> <parent> <groupId>cn.test.maven</groupId> <artifactId>parent</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../hello_parent/pom.xml</relativePath> </parent> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> <dependency> <groupId>cn.test.maven</groupId> <artifactId>hello_ch2</artifactId> </dependency> </dependencies> </project>
4.在parent项目中构建:
时间: 2024-10-25 03:30:56