Maven----模块化开发
我们可以将模块划分为如下形式:
- 统一的依赖管理(dependencies)
- 通用的工具类(commons)
- 领域模型(domain)
- 管理后台(admin)
- 商城前端(ui)
- 接口模块(api)
my-shop 工程的 pom.xml文件如下
注意:<packaging>pom</packaging> 管理模块,主要作用是管理整个工程的全部模块(管理每一个项目),当有新模块加入时需要在 modules
元素下配置对应的模块目录
<?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工程目录下创建一个名为 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> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <!-- Log End --> <!-- Commons Begin --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>${commons-lang3.version}</version> </dependency> <!-- Commons End --> </dependencies> </dependencyManagement> <build> <plugins> <!-- Compiler 插件, 设定 JDK 版本 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> <encoding>${project.build.sourceEncoding}</encoding> <showWarnings>true</showWarnings> </configuration> </plugin> </plugins> <!-- 资源文件配置, javac不会讲资源文件打包--> <resources> <resource> <directory>src/main/java</directory> <excludes> <exclude>**/*.java</exclude> </excludes> </resource> <resource> <directory>src/main/resources</directory> </resource> </resources> </build> </project>
PS:别忘记在 my-shop
工程的 pom.xml
中增加 <module>my-shop-dependencies</module>
配置
创建通用的工具类
在my-shop工程目录下创建名为 my-shop-commons
的项目,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.zy</groupId> <artifactId>my-shop-dependencies</artifactId> <version>1.0.0-SNAPSHOT</version> <relativePath>../my-shop-dependencies/pom.xml</relativePath> </parent> <artifactId>my-shop-commons</artifactId> <packaging>jar</packaging> <name>my-shop-commons</name> <description></description> <!--忽略了版本号,my-shop-dependencies已经控制了版本的管理--> <dependencies> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> </dependencies> </project>
PS:别忘记在 my-shop
工程的 pom.xml
中增加 <module>my-shop-commons</module>
配置
创建领域模型
在my-shop工程目录下创建一个名为 my-shop-domain
的项目,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-dependencies</artifactId> <version>1.0.0-SNAPSHOT</version> <relativePath>../my-shop-dependencies/pom.xml</relativePath> </parent> <artifactId>my-shop-domain</artifactId> <packaging>jar</packaging> <name>my-shop-domain</name> <description></description> </project>
PS:别忘记在 my-shop
工程的 pom.xml
中增加 <module>my-shop-domain</module>
配置
创建管理后台
在my-shop工程目录下创建一个名为 my-shop-web-admin
的项目,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-dependencies</artifactId> <version>1.0.0-SNAPSHOT</version> <relativePath>../my-shop-dependencies/pom.xml</relativePath> </parent> <artifactId>my-shop-web-admin</artifactId> <packaging>war</packaging> <name>my-shop-web-admin</name> <description></description> <dependencies> <dependency> <groupId>com.funtl</groupId> <artifactId>my-shop-commons</artifactId> <version>${project.parent.version}</version> </dependency> <dependency> <groupId>com.funtl</groupId> <artifactId>my-shop-domain</artifactId> <version>${project.parent.version}</version> </dependency> </dependencies> </project>
PS:别忘记在 my-shop
工程的 pom.xml
中增加 <module>my-shop-web-ui</module>
配置
创建接口模块
在my-shop工程目录下创建一个名为 my-shop-web-api
的项目,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-dependencies</artifactId> <version>1.0.0-SNAPSHOT</version> <relativePath>../my-shop-dependencies/pom.xml</relativePath> </parent> <artifactId>my-shop-web-api</artifactId> <packaging>war</packaging> <name>my-shop-web-api</name> <description></description> <dependencies> <dependency> <groupId>com.funtl</groupId> <artifactId>my-shop-commons</artifactId> <version>${project.parent.version}</version> </dependency> </dependencies> </project>
PS:别忘记在 my-shop
工程的 pom.xml
中增加 <module>my-shop-web-api</module>
配置
清理、编译、打包
至此一个完整的模块化工程创建完毕,此时的 Root 工程 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> <module>my-shop-dependencies</module> <module>my-shop-commons</module> <module>my-shop-domain</module> <module>my-shop-web-admin</module> <module>my-shop-web-ui</module> <module>my-shop-web-api</module> </modules> </project>
我们可以在 Root 工程中使用 Maven 提供的 mvn clean
命令测试一下效果
检查环境是否有问题
mvn clean package -Dmaven.test.skip=true