maven继承与聚合简介说明
下文笔者讲述maven的继承和聚合的简介说明,如下所示:
maven继承简介
maven继承:通常指子项目从父项目中继承指定信息,
继承的目的是用于消除重复,
我们通常将具有相同的groupId,artifactId,version提取出来,放入父项目中
然后子项中直接使用输入相应的groupId和artifactId无需输入版本号,通过这种方式可避免版本的问题
maven的继承方式
父工程的设置
父工程的pom.xml中的打包方式
必须设置为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.java265</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<!-- 集中定义依赖版本号 -->
<properties>
<junit.version>4.10</junit.version>
<spring.version>4.1.0.RELEASE</spring.version>
<slf4j.version>1.6.5</slf4j.version>
</properties>
<!-- 版本锁定,当子工程中有需要并且自行添加了具体依赖后才有效 -->
<dependencyManagement>
<dependencies>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
子工程的设置
子工程中的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.java265</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 父项目的pom.xml文件的相对路径;一般可不指定 -->
<relativePath>../parent</relativePath>
</parent>
<groupId>cn.java265</groupId>
<artifactId>subTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 依赖 -->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<!-- 版本号由父工程里面统一指定不再需要特别指定 -->
<!-- <version>${junit.version}</version> -->
<scope>test</scope>
</dependency>
</dependencies>
</project>
relativePath:
父项目的pom.xml文件的相对路径
默认值为../pom.xml
maven首先从当前构建项目开始查找父项目的pom文件,然后从本地仓库,最后从远程仓库
RelativePath允许你选择一个不同的位置
聚合
当一个项目存在多个模块时,此时我们可以在项目添加module
然后父模块的pom文件中 设置modules信息,即可实现一个聚合项目的定义
<modules>
<module>../submodule1</module>
<module>../submodule2</module>
<module>../submodule3</module>
</modules>
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


