首先,我們要明確的多模塊項目的含義,它是指一個應用中包含多個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>