首先,我们要明确的多模块项目的含义,它是指一个应用中包含多个module。一般来说,一个应用单独部署成服务,只是打包的时候,maven会把各个module组合在一起。各模块一般单独打成jar放到lib目录中,当然web应用也生成war包。
这里说的多模块项目要与那种单独自立门户的多个application区分开来,多个application也可能有包级的关联,但是它们各自分开了,不属于多模块项目的范畴。
maven对多模块项目的管理大概是这样的,它存在一个parent模块,但实际没有程序代码,只包含一个pom.xml,该pom是用来给子模块来引用的。
目录结构与下面的类似:
simple-parent
+-simple-weather
+-src
+-target
\-pom.xml
+-simple-webapp
+-src
+-target
\-pom.xml
\-pom.xml
在这个目录结构中,一个父模块包含了两个子模块。
各个pom.xml的内容大致如下:
pom.xml:
- <modules>
- <groupId>org.sonatype.mavenbook.multi</groupId>
- <artifactId>parent</artifactId>
- <version>0.8-SNAPSHOT</version>
- <packaging>pom</packaging>
- <module>simple-weather</module>
- <module>simple-webapp</module>
- </modules>
- <dependencies>
- <dependency>
- <groupId>velocity</groupId>
- <artifactId>velocity</artifactId>
- <version>1.5</version>
- </dependency>
- </dependencies>
simple-weather/pom.xml:
- <parent>
- <groupId>org.sonatype.mavenbook.multi</groupId>
- <artifactId>simple-parent</artifactId>
- <version>0.8-SNAPSHOT</version>
- </parent>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
simple-webapp/pom.xml:
- <parent>
- <groupId>org.sonatype.mavenbook.multi</groupId>
- <artifactId>simple-parent</artifactId>
- <version>0.8-SNAPSHOT</version>
- </parent>
- <dependencies>
- <dependency>
- <groupId>org.apache.geronimo.specs</groupId>
- <artifactId>geronimo-servlet_2.4_spec</artifactId>
- <version>1.1.1</version>
- </dependency>
- </dependencies>