Maven模块化开发
多人协同开发时,特别是规模较大的项目,为方便日后代码维护管理会将每个人的工作细分到具体的功能和模块上。随着项目的不断扩大,模块会越来越多,后续更加难以维护和扩展,为应对这种情况后期会采用微服务的架构的方式进行开发。
商城模块划分范例
- 统一的依赖管理(dependencies)
- 通用的工具类(commons)
- 领域模型(domain)
- 管理后台(admin)
- 商城前端(ui)
- 接口模块(api)
利用Maven的依赖机制,实现模块化开发,使协同开发更简单
创建根项目(工程)
创建一个名为 my-shop
的工程,pom.xml 文件如下:
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.funtl</groupId>
<artifactId>my-shop</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
</modules>
</project>
该项目称之为 Root 项目,主要作用是管理整个工程的全部模块,当有新模块加入时需要在 modules 元素下配置对应的模块目录
创建统一的依赖
创建一个名为 my-shop-dependencies
的项目,pom.xml 文件如下:
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.funtl</groupId>
<artifactId>my-shop</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>my-shop-dependencies</artifactId>
<packaging>pom</packaging>
<name>my-shop-dependencies</name>
<description></description>
<properties>
<!-- 环境配置 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<!-- 统一的依赖管理 -->
<commons-lang3.version>3.5</commons-lang3.version>
<jstl.version>1.2</jstl.version>
<log4j.version>1.2.17</log4j.version>
<servlet-api.version>3.1.0</servlet-api.version>
<slf4j.version>1.7.25</slf4j.version>
<spring.version>4.3.17.RELEASE</spring.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- Spring Begin -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring End -->
<!-- Servlet Begin -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet-api.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<!-- Servlet End -->
<!-- Log Begin -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>