下面是一个简略的项目结构图
Parent
`------ childA(BusinessLayer)
`--- pom.xml
`------ childB(WebLayer)
`--- pom.xml
`------ pom.xml
1、parent模块指定子模块
在parent的pom文件里加入以下内容。
<modules>
<module>childA</module>
<module>childB</module>
</modules>
2、子模块找到父模块
在parent中,写入以下内容,其中"*"标识的行可以组成一个路径,通过这个路径可以在maven仓库中找到这个pom文件所以这三个标签是必须的!!!
<modelVersion>4.0.0</modelVersion>
<groupId>com.sang.main</groupId> *
<artifactId>Parent-Moduel</artifactId> *
<version>1.0.2</version> *
<packaging>pom</packaging>
<name>Simple-main</name>
父pom写好了,子pom就通过标签继承父pom的依赖,如下:
<parent>
<groupId>com.sang.main</groupId>
<artifactId>Parent-Moduel</artifactId>
<version>1.0.2</version>
<relativePath>../pom.xml</relativePath> <!--本例中此处是可选的-->
</parent>
值得注意的是标签,如果pom的层次关系就像本例中的那样只隔一层,则可以省略这个。maven同样可以找到子pom。
子pom中引入标签后,就会从父pom继承等属性了,
<modelVersion>4.0.0</modelVersion>
<groupId>com.sang.business</groupId> <!--和artifactId一起唯一标识这个jar文件-->
<artifactId>ChildA-module</artifactId>
<packaging>jar</packaging> <!--指明打包类型-->
<name>childA</name>
3、依赖包管理
父pom中把依赖通过引起来,表示子pom可能会用到的jar包依赖
<dependencyManagement>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
</dependencyManagement>
子pom如果需要引用该jar包,则直接引用即可!不需要加入,便于统一管理。此外也可以加入仅在子pom中用到的jar包,比如:
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId> <!--此处不再需要verison了!-->
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-lgpl</artifactId>
<version>1.9.4</version> <!--当然也可以加入只在这个子模块中用到的jar包-->
</dependency>
</dependencies>
4、除了jar包依赖,插件也可以通过这样的方式进行管理
<!-- mainModule -->
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.1</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<!-- childA -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
</plugins>
</build>
5、如果子pom间存在引用关系,比如childB 引用了childA
<dependency>
<groupId>com.module</groupId>
<artifactId>childA</artifactId> <!--加上childA的依赖-->
<version>1.0.0</version>
</dependency>