假如新建了三个有依赖关系的maven项目,分别是user-core,user-dao,user-log,user-service,那么启动他们需要分别启动他们的配置文件pom.xml,是不是觉得很麻烦,那么,就新建一个parent的maven项目吧,用来聚合这些maven文件,同时用来更好的让这些有依赖关系的项目的jar包得到继承。
聚合的项目和其他项目在同一级模块中需要使用, 文件夹名称用..来设置,其中聚合文件仅仅需要的是pom.xml文件和jar包的所在文件夹。而且聚合的项目的文件格式是pom文件格式。
配置文件用<modules></modules>来管理聚合的模块。对于依赖而言,需要通<dependencyManagement></dependencyManagement>来完成管理。
以下为父类的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>org.konghao.user</groupId>
<artifactId>user-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<!-- 聚合管理 -->
<modules>
<module>../user-core</module>
<module>../user-dao</module>
<module>../user-log</module>
<module>../user-service</module>
</modules>
<url>http://maven.apache.org</url>
<distributionManagement>
<snapshotRepository>
<id>user-snapshots</id>
<name>User Project SNAPSHOTS</name>
<url>http://192.168.0.199:8081/nexus/content/repositories/MyUserReposSnapshots/</url>
</snapshotRepository>
<repository>
<id>user-releases</id>
<name>User Project Release</name>
<url>http://192.168.0.199:8081/nexus/content/repositories/MyUserReposRelease/</url>
</repository>
</distributionManagement>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>4.10</junit.version>
</properties>
<!-- 继承管理的关键dependencyManagement,如果不管理子类,会导致一些模块存在不需要的依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<!-- 用${project.groupId}可以继承上面的groupID,${project.version}可以继承上面的版本 -->
<groupId>${project.groupId}</groupId>
<artifactId>user-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>user-dao</artifactId>
<version>${project.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>user-log</artifactId>
<version>${project.version}</version>
<type>jar</type>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.10.Final</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.18</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.ow2.orchestra.eclipse.birt</groupId>
<artifactId>org.ow2.orchestra.eclipse.birt.chart.engine</artifactId>
<version>3.7.0</version>
</dependency>
<dependency>
<groupId>maven</groupId>
<artifactId>maven-abbot-plugin</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>mockobjects</groupId>
<artifactId>alt-jdk1.3</artifactId>
<version>0.07</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
子类的pom文件继承父类的pom文件
<parent>
<groupId>org.konghao.user</groupId>
<artifactId>user-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../user-parent/pom.xml</relativePath>
</parent>