父子模块的pom文件里会涉及dependencies、dependecyManagement、packaging元素、properties元素。
子模块里必须有parent元素,如下所示:
<parent>
<groupId>org.apache</groupId>
<artifactId>plugin</artifactId>
<version>1.3.0-SNAPSHOT</version>
</parent>
父模块里会有modules元素,如下所示:
<modules>
<module>provider</module>
<module>consumer</module>
<module>api</module>
</modules>
1、packaging元素
父模块里的packaging必须是pom。子模块一般是jar,但也可以是pom,此时子模块仅包含一些依赖,没有代码,或者也是父模块。
2、dependencies元素
一般出现在父模块的pom文件里。被dependencies包含的依赖会被子项目无条件继承下去,无需子项目显示指定。用法如下所示:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
3、dependecyManagement元素
一般出现在父模块的pom文件里。被dependencyManagement包含的依赖不会被子项目直接继承下去,子项目需要显示写出依赖,当然可以省略version和scope(从父项目中继承)。用法如下:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
4、mvn命令
在父项目的根目录下执行mvn clean compile,那么该父项目及其子项目都会被先清理再编译,并且是按照实际的依赖关系对子模块重排序,以确保所有模块都能顺利完成构建(参见http://blog.csdn.net/eclipser1987/article/details/5739177)。
5、properties元素
properties元素通常用来定义一些常量,会被子模块无条件的继承过去。可用来定义版本号,这样可以同时修改多个依赖的版本号。