利用Maven可以对项目进行分模块开发。那么怎样把各个模块整合到一起呢?这就利用了Maven继承的特性。一般是每个模块都继承一个父工程。
几个注意的地方:
(1)父工程的packaging是pom
(2)父工程中定义属性,子模块不用定义,会继承下来
(3)父工程的依赖放在dependencyManagement中,这样子模块才能继承父工程的依赖
(4)父工程的pom.xml文件中定义了modules,整合子模块的artifactId
(5)子模块的pom.xml文件中定义了parent标签,里面是父工程的gav
(6)子模块只需定义自己的artifactId
(7)子模块如果继承父工程的依赖可以不写版本,但是如果父工程中没有该依赖,那么子模块必须写完整的gav
举例说明:
1、构建父工程
父工程blog的pom.xml
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sl.edu</groupId>
<artifactId>blog</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.0.4</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.31</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
</dependencyManagement>
<modules>
<module>blog-api</module>
<module>blog-admin</module>
</modules>
</project>
2、构建子工程
子模块blog-api的pom.xml
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent><!-- 子类里面显示声明才能有明确的继承表现,无意外就是父类的默认版本否则自己定义 -->
<groupId>com.sl.edu</groupId>
<artifactId>blog</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>blog-api</artifactId><!-- 当前Module自己叫什么名字 -->
<dependencies><!-- 当前Module需要用到的jar包,按自己需求添加,如果父类已经包含了,可以不用写版本号 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
</dependencies>
</project>
子模块blog-admin的pom.xml
复制代码
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent><!-- 子类里面显示声明才能有明确的继承表现,无意外就是父类的默认版本否则自己定义 -->
<groupId>com.sl.edu</groupId>
<artifactId>blog</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>blog-admin</artifactId><!-- 当前Module自己叫什么名字 -->
<dependencies><!-- 当前Module需要用到的jar包,按自己需求添加,如果父类已经包含了,可以不用写版本号 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
</dependencies>
</project>
注:如果用Eclipse开发,新建父工程选择Maven Project;新建子模块,右键父工程,选择Maven Module,会自动生成一些配置。