您的位置:首页 > 其它

maven项目管理之-09-聚合和继承

2015-09-03 15:46 281 查看
maven的聚合和继承

1. 聚合

项目开发过程中有多个模块,如果一个工项目有多个模块工程,如果对整个项目进行maven操作,例如使用maven分别单个进行install安装到本地仓库中。前面的方法操作比较麻烦,

为了简化这些操作,maven提供一种方式把这些模块一起执行的操作叫做聚合(非官方描述)。

通过maven快速向导分别建立三个maven工程A、B、C,再建一个作为聚合A、B、C的maven工程——可以依赖前面的三个工程,这里且叫做together。 同时修改together的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.bamboo</groupId>
<artifactId>aggregation-Together</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- pom用于parent和合成多个项目。我们需要增加相应的值给父pom,用于子项目继承。 -->
<packaging>pom</packaging>

<name>aggregation-Together</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<!-- 聚合运行多个maven项目-->
<modules>
<module>../aggregation-A</module>
<module>../aggregation-B</module>
<module>../aggregation-C</module>
</modules>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>


创建上面4个工程以后如下图所示,A、B、C、together工程的target目录都是空的。



这里我们只对together的工程执行install操作后,发现A、B、C三个工程分别也执行了install操作,同时里target目录也有更新。如下:



2.继承

开发过程中,通常需要把通用的固定构件抽到父模块定义,然后让子模块继承父模块。如下例把junit的依赖抽到父类配置,子类继承。

我们通过快速向导新建一个maven工程inherit-sub,未做任何修改前,目录如下。



我们需要把inherit-sub工程的junit的依赖抽到父工程模块里,新建父maven工程inherit-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"> <modelVersion>4.0.0</modelVersion>

<groupId>com.bamboo</groupId>
<artifactId>inherit-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<name>inherit-parent</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- 把继承给子类的junit版本号抽到配置里 -->
<junit.version>4.0</junit.version>
</properties>

<!-- 依赖管理(包含多个依赖列表),主要在父模块中定义,子模块继承-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version> <!-- 使用配置里的版本号 -->
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>

</project>


现在修改inherit-sub的pom.xml,使其可以依赖父模块定义的junit构件。然后继承效果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: