(四)maven的聚合和继承


1.聚合

在一个POM文件中完成所有模块的编译,打包工作,而不用每个模块单独执行命令。
好处:简化过程,一个地方编译所有的模块
注意:
packaging为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.hqh.maven.user</groupId>
  <artifactId>user-aggregation</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <!--聚合的packaging属性为pom-->
  <packaging>pom</packaging>

  <modules>
        <!--路径要正确-->
  	<module>../user-log</module>
  	<module>../user-core</module>
  	<module>../user-dao</module>
  	<module>../user-service</module>
  </modules>

</project>



2.继承

通过继承的思想,父类定义子类通用的配置
好处:集中管理,避免重复,避免依赖导致的jar版本不一致的冲突问题
注意:
parent的packaging属性必须为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>
	
	<!-- 聚合:聚合其它模块 -->
	<modules>
		<module>../user-log</module>
		<module>../user-core</module>
		<module>../user-dao</module>
		<module>../user-service</module>
	</modules>
	
	<!-- 继承:超级pom的坐标,该pom中的配置用于其它模块继承所用 -->
	<groupId>com.hqh.maven.user</groupId>
	<artifactId>user-parent</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>

	<!-- 中心仓库 <repositories> <repository> <id>central</id> <name>my central 
		repository</name> <url>http://xxx.xxx.org</url> <layout>default</layout> 
		<snapshots> <enabled>false</enabled> </snapshots> <releases> <updatePolicy>false</updatePolicy> 
		</releases> </repository> </repositories> -->

	<!-- 单点维护属性,其它地方通过${name}来引用 -->
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<junit.version>4.10</junit.version>
	</properties>

	<!-- -->
	<dependencyManagement>
		<dependencies>
			<!-- junit测试单元 -->
			<dependency>
				<groupId>junit</groupId>
				<artifactId>junit</artifactId>
				<version>${junit.version}</version>
				<scope>test</scope>
			</dependency>

			<!-- 日志 -->
			<dependency>
				<groupId>log4j</groupId>
				<artifactId>log4j</artifactId>
				<version>1.2.16</version>
			</dependency>
			<dependency>
				<groupId>org.slf4j</groupId>
				<artifactId>slf4j-log4j12</artifactId>
				<version>1.6.1</version>
			</dependency>

			<!-- ORM -->
			<dependency>
				<groupId>org.hibernate</groupId>
				<artifactId>hibernate-core</artifactId>
				<version>3.6.10.Final</version>
			</dependency>

			<dependency>
				<groupId>javassist</groupId>
				<artifactId>javassist</artifactId>
				<version>3.8.0.GA</version>
			</dependency>

			<dependency>
				<groupId>mysql</groupId>
				<artifactId>mysql-connector-java</artifactId>
				<version>5.1.25</version>
			</dependency>
			
			<!-- 模块 -->
			<dependency>
				<groupId>${project.groupId}</groupId>
				<artifactId>user-log</artifactId>
				<version>${project.version}</version>
			</dependency>
			<dependency>
				<groupId>${project.groupId}</groupId>
				<artifactId>user-core</artifactId>
				<version>${project.version}</version>
			</dependency>
			<dependency>
				<groupId>${project.groupId}</groupId>
				<artifactId>user-service</artifactId>
				<version>${project.version}</version>
			</dependency>
			<dependency>
				<groupId>${project.groupId}</groupId>
				<artifactId>user-dao</artifactId>
				<version>${project.version}</version>
			</dependency>
		</dependencies>
	</dependencyManagement>

</project>



你可能感兴趣的:(maven)