1.聚合与继承的关系
多模块maven项目中的聚合与继承是两个概念.
聚合主要是为了方便快速构建项目.
继承主要是为了消除重复配置.
聚合配置代码:
<modules> <module>../Hello</module> <module>../HelloFriend</module> <module>../MakeFriends</module> </modules>
其中module的路径为相对路径.
继承配置代码:
<parent> <groupId>com.zt.maven</groupId> <artifactId>Project</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../Project/pom.xml</relativePath> </parent>
在实际项目中,一般一个POM既是聚合POM,又是父POM.
Hello模块:
<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> <name>Hello</name> <url>http://maven.apache.org</url> <parent> <groupId>com.zt.maven</groupId> <artifactId>Parent</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../Parent/pom.xml</relativePath> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
HelloFirend模块:
<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>HelloFriend</artifactId> <packaging>jar</packaging> <name>HelloFriend</name> <url>http://maven.apache.org</url> <parent> <groupId>com.zt.maven</groupId> <artifactId>Parent</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../Parent/pom.xml</relativePath> </parent> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> <dependency> <groupId>cn.itcast.maven</groupId> <artifactId>Hello</artifactId> </dependency> </dependencies> </project>
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.itcast.maven</groupId> <artifactId>Parent</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <name>Parent</name> <url>http://maven.apache.org</url> <modules> <module>../Hello</module> <module>../HelloFriend</module> </modules> <properties> <project.build.sourceEncoding>GBK</project.build.sourceEncoding> <argLine>-Dfile.encoding=GBK</argLine> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> <scope>test</scope> </dependency> <dependency> <groupId>com.zt.maven</groupId> <artifactId>Hello</artifactId> <version>0.0.1-SNAPSHOT</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.zt.maven</groupId> <artifactId>HelloFriend</artifactId> <version>0.0.1-SNAPSHOT</version> <scope>compile</scope> </dependency> </dependencies> </dependencyManagement> </project>
其中:Parent代码可以视为聚合模块也可以视为父模块.Hello和HelloFriend是被聚合模块的同时也是子模块.
maven中一般遵循约定,各种目录结构等.其实就是这种约定就是一种maven的超级POM.任何maven项目都继承了这个超级POM.对于maven2而言,超级POM在文件$MAVEN_HOME/lib/maven-model-builder-3.0.4.jar中的org/apache/maven/model/pom-4.0.0.xml:
<?xml version="1.0" encoding="UTF-8"?> <!-- START SNIPPET: superpom --> <project> <modelVersion>4.0.0</modelVersion> <!--首先定义了仓库及插件仓库.默认都为中央仓库--> <repositories> <repository> <id>central</id> <name>Central Repository</name> <url>http://repo.maven.apache.org/maven2</url> <layout>default</layout> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>central</id> <name>Central Repository</name> <url>http://repo.maven.apache.org/maven2</url> <layout>default</layout> <snapshots> <enabled>false</enabled> </snapshots> <releases> <updatePolicy>never</updatePolicy> </releases> </pluginRepository> </pluginRepositories> <!--关于项目结构的定义--> <build> <directory>${project.basedir}/target</directory><!--主输出目录--> <outputDirectory>${project.build.directory}/classes</outputDirectory><!--主代码输出目录--> <finalName>${project.artifactId}-${project.version}</finalName><!--最终构件的名称格式--> <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory><!--测试代码目录--> <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory><!--主源码目录--> <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory><!--脚本源码目录--> <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory><!--测试源码目录--> <resources> <resource> <directory>${project.basedir}/src/main/resources</directory><!--主资源目录--> </resource> </resources> <testResources> <testResource> <directory>${project.basedir}/src/test/resources</directory><!--测试资源目录--> </testResource> </testResources> <!--关于插件版本的定义--> <pluginManagement> <plugins> <plugin> <artifactId>maven-antrun-plugin</artifactId> <version>1.3</version> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.2-beta-5</version> </plugin> <plugin> <artifactId>maven-dependency-plugin</artifactId> <version>2.1</version> </plugin> <plugin> <artifactId>maven-release-plugin</artifactId> <version>2.0</version> </plugin> </plugins> </pluginManagement> </build> <reporting> <outputDirectory>${project.build.directory}/site</outputDirectory> </reporting> <profiles> <!-- NOTE: The release profile will be removed from future versions of the super POM --> <profile> <id>release-profile</id> <activation> <property> <name>performRelease</name> <value>true</value> </property> </activation> <build> <plugins> <plugin> <inherited>true</inherited> <artifactId>maven-source-plugin</artifactId> <executions> <execution> <id>attach-sources</id> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> <plugin> <inherited>true</inherited> <artifactId>maven-javadoc-plugin</artifactId> <executions> <execution> <id>attach-javadocs</id> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> <plugin> <inherited>true</inherited> <artifactId>maven-deploy-plugin</artifactId> <configuration> <updateReleaseInfo>true</updateReleaseInfo> </configuration> </plugin> </plugins> </build> </profile> </profiles> </project> <!-- END SNIPPET: superpom -->