1有时候一个项目中有很多个模块,要想各个模块之间影响较小,可以将每个模块拿出来作为一个项目模块,对每个项目模块进行独立的开发。
2在这些过程中会遇到关于聚合和继承的问题。
3何为聚合?
a如果我们想一次构建多个项目模块,那我们就需要对多个项目模块进行聚合,也就是说当我们点击一个pom的时候,同时运行多个项目模块。这时候要用到以下的标签,将各个项目模块整合在一起。
<modules>
<module>…</module>
</modules>
4何为继承
a继承是为了消除重复,我们把很多相同的配置提取出来。(这样的好处是:统一管理jar包,方便升级)
b例如:groupid,version等。
5聚合与继承的关系:
a聚合主要是为了快速构建项目
b继承主要是为了消除重复。
案例(先简单概括,后全部列出):
一:聚合配置代码(注意,下面这些代码在父项目模块中)
<module>../hello</module>
<module>../hellofriend</module>
<module>../makefriends</module>
注意:其中module的路径为相对路径。
二:继承配置代码(下面配置在各个子项目模块中)
<parent>
<groupid>cn.toto.maven</groupid>
<artifactid>project</artifactid>
<version>0.0.1-snapshot</version>
<!—相对父pom的位置,也就是说通过下面的配置找到父pom的位置-->
<relativepath>../parent/pom.xml</relativepath>
</parent>
三、继承代码过程中,可以定义属性
<properties>
<project.build.sourceencoding>utf-8</project.build.sourceencoding>
<junit.version>4.9</junit.version>
<maven.version>0.0.1-snapshot</maven.version>
</properties>
访问属性的方式为${junit.version},例如:
<dependency>
<groupid>junit</groupid>
<artifactid>junit</artifactid>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
四:在父模块中用dependencymanagement进行管理(通过下面的配置可以知道每个模块是什么,同时:这样的好处是子模块可以有选择行的继承,而不需要全部继承)
<dependencymanagement>
<dependencies>
<dependency>
<groupid>junit</groupid>
<artifactid>junit</artifactid>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<groupid>cn.toto.maven</groupid>
<artifactid>hellofriend</artifactid>
<version>${maven.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependencies>
</dependencymanagement>
6实际案例进行说明
模块结构:
一:父模块(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>cn.toto.maven</groupid>
<artifactid>parent</artifactid>
<version>0.0.1-snapshot</version>
<packaging>pom</packaging> <!---注意,这里要写成pom-->
<name>parent</name>
<!—为实现聚合,点击这个pom.xml的时候四个同时运行--->
<module>../hello</module>
<module>../hellofriend</module>
<module>../makefriends</module>
<module>../web</module>
<!—属性配置--à
<properties>
</properties>
<!—对所有模块进行统一的管理-à
<dependencymanagement>
<dependencies>
<dependency>
<groupid>junit</groupid>
<artifactid>junit</artifactid>
<version>4.9</version>
<scope>test</scope>
</dependency>
<groupid>cn.itcast.maven</groupid>
<artifactid>hellofriend</artifactid>
<version>0.0.1-snapshot</version>
<scope>compile</scope>
<groupid>cn.itcast.maven</groupid>
<artifactid>hello</artifactid>
<version>0.0.1-snapshot</version>
<scope>compile</scope>
</dependency>
<dependency>
<artifactid>makefriends</artifactid>
</dependencies>
</dependencymanagement>
<distributionmanagement>
<repository>
<id>releases</id>
<name>internal releases</name>
<url>http://localhost:8080/nexus-2.1.2/content/repositories/releases/</url>
</repository>
<snapshotrepository>
<id>snapshots</id>
<name>internal snapshots</name>
<url>http://localhost:8080/nexus-2.1.2/content/repositories/snapshots/</url>
</snapshotrepository>
</distributionmanagement>
</project>
没有其它的代码了。
二:hello模块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">
<!---此处去掉了些公共的配置,但是在parent中的pom.xml中有相关的配置-->
<artifactid>hello</artifactid>
<name>hello</name>
<parent>
<groupid>cn.itcast.maven</groupid>
<artifactid>parent</artifactid>
<version>0.0.1-snapshot</version>
<relativepath>../parent/pom.xml</relativepath>
</parent>
<dependencies>
</dependencies>
相关java代码略
三:hellofriend(pom.xml)
<artifactid>hellofriend</artifactid>
<name>hellofriend</name>
<parent>
<groupid>cn.toto.maven</groupid>
<groupid>cn.toto.maven</groupid>
<artifactid>hello</artifactid>
makefriends(pom.xml配置)
<artifactid>makefriends</artifactid>
<name>makefriends</name>
<artifactid>hellofriend</artifactid>