介绍:一个多模块项目通过一个父POM(也称顶层POM)引用一个或多个子模块来定义.在父POM,定义一些将会被所有子模块继承的设置.build配置和dependencies都会被所有的子模块继承.在父POM定义GAV,然后定义packaging只能为pom.使用modules定义多个子模块,每个子元素module对应一个子目录,然后maven会根据这些子目录寻找pom.xml.
下面是一个例子simple-parent包含simple-java子模块(一般java项目)和simple-web子模块(一般web项目).
一.各pom.xml定义
1.simple-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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>simple-parent</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<name>simple-parent maven</name>
<modules>
<module>simple-java</module>
<module>simple-web</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
2.simple-java子模块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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.test</groupId>
<artifactId>simple-parent</artifactId>
<version>1.0</version>
</parent>
<artifactId>simple-java</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>simple-java maven</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
</dependencies>
</project>
3.simple-web子模块:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.test</groupId>
<artifactId>simple-parent</artifactId>
<version>1.0</version>
</parent>
<artifactId>simple-web</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>simple-web Maven</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<tomcat.version>7.0.47</tomcat.version>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<server>Tomcat7</server>
<url>http://localhost:8080/manager/text</url>
<path>/simple-web</path>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<dependency>
<groupId>com.test</groupId>
<artifactId>simple-java</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>${tomcat.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
4.当然,各子模块可以像一般情况下引入其它的依赖,对于所有子模块都用的依赖,可以放入父pom,也就是所谓的全局依赖
二.先在simple-parent运行mvn clean install或在eclipse的goals填clean install,将simple-java安装到本地库,再在simple-web部署才能正确发布引用依赖.
源码:http://download.youkuaiyun.com/detail/xiejx618/6957289