大型项目将模块系统化拆分是为了方便系统的开发维护,因为通常大型项目都是由多个团队共同开发的。如果一个业务出现了问题,也不会对整个项目造成太大的影响,实现了软件架构之间的松耦合。将原来项目的模块系统化(一个模块一个项目),这样的拆分方式叫垂直拆分。
maven继承与依赖
对于多个项目共同开发,可以对一些公共的jar包文件进行抽取,抽取为一个单独的项目xxx-parent,其他的系统只需要继承这个项目,就可以获取对应的jar文件,这样方便包文件的管理和升级;
对于一些常用的工具类,可以对工具类进行抽取,抽取为单独的项目xxx-common。如果开发时需要用到工具类,只需要将工具类像引用maven的依赖包一样,引用工具类像引用maven的依赖包一样,引用工具类的jar包文件即可。
如下所示:
父项目pom文件:
<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>cn.kiiwii.framework</groupId>
<artifactId>spring-boot-sample</artifactId>
<version>1.0-SNAPSHOT</version>
<!--<modules>-->
<!--<module>spring-boot-with-schedule</module>-->
<!--<module>spring-boot-with-hibernate</module>-->
<!--<module>spring-boot-with-dynamic-datasource</module>-->
<!--<module>spring-boot-with-druid</module>-->
<!--<module>spring-boot-with-dubbo</module>-->
<!--<module>spring-boot-with-jpa</module>-->
<!--<module>spring-boot-with-mybatis</module>-->
<!--<!–<module>spring-boot-with-druid-dpcp</module>–>-->
<!--<module>spring-boot-dubbo-plugin</module>-->
<!--<module>spring-boot-with-freemarker</module>-->
<!--</modules>-->
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
<name>spring-boot-sample</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<version>1.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.25</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
</dependencies>
</project>
注:
1)父类pom文件的packaging属性可以设为pom,其mvn clean install后在本地仓库中会生成个pom文件(没有jar包)
2)父类pom文件中module标签标示聚合,可以将多个项目模块聚在一起。
子项目pom文件:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>spring-boot-sample</artifactId>
<groupId>cn.kiiwii.framework</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-boot-with-druid</artifactId>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
注:
子类pom文件需要parent标签,标记继承的pom对象属性