为方便项目构建,通常将聚合模块放在项目目录的最顶层,其他模块则作为聚合模块的子目录存在,
这样我们得到源码的时候,最快发现的是聚合模块的pom
运行命令
查看结果
创建parent项目,用于被继承
parent/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.demo.inheritance</groupId> <artifactId>parent</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> </project>
创建子模块sub-a,继承parent
sub-a/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> <parent> <groupId>com.demo.inheritance</groupId> <artifactId>parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>sub-a</artifactId> </project>
pom说明
parent元素下的groupId,artifactId,version指明父模块的坐标(必需)
parent元素下的relativePath:父模块的pom文件所在位置,默认: ../pom.xml,即maven的默认父pom在上一层目录下
parent/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.demo.inheritance</groupId> <artifactId>parent</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <modules> <module>sub-a</module> </modules> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.0.7.RELEASE</version> </dependency> </dependencies> </dependencyManagement> </project>
dependencyManagement元素可以让子元素继承到父模块的依赖配置,又能保证子模块依赖使用的灵活性dependencyManagement下的dependency只会依赖声明,不会引用实际依赖
sub-a/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> <parent> <groupId>com.demo.inheritance</groupId> <artifactId>parent</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath></relativePath> </parent> <artifactId>sub-a</artifactId> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </dependency> </dependencies> </project>
子模块的依赖只要使用groupId,artifaceId,不需要version
parent/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.demo.inheritance</groupId> <artifactId>parent</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <modules> <module>sub-a</module> </modules> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.0.7.RELEASE</version> </dependency> </dependencies> </dependencyManagement> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.8</version> <executions> <execution> <!--指定在哪个阶段后执行目标 --> <phase>test</phase> <goals> <!--执行的目标 --> <goal>tree</goal> </goals> </execution> </executions> </plugin> </plugins> </pluginManagement> </build> </project>
sub-a/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> <parent> <groupId>com.demo.inheritance</groupId> <artifactId>parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>sub-a</artifactId> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> </plugin> </plugins> </build> </project>
与依赖管理类似:插件版本可以不用
注意:
当项目中有多个模块有同样的插件配置时,应当将配置移动父pom的pluginManagement元素中,即各个模块
对同一个插件具体的配置不尽相同,也应当使用父pom的pluginManagement元素统一版本,甚至可以要求
将所有的用到的插件的版本在父pom的pluginManagement元素中统一声明插件版本
项目构建之maven篇:7.聚合与继承,布布扣,bubuko.com
原文:http://blog.csdn.net/wobendiankun/article/details/32352743