依赖的归并
把依赖的版本统一管理, 定义为版本变量, 在
dependency
使用版本变量, 修改版本,只需要修改版本变量, 依赖跟着变化, 不需要修改依赖
版本的归并写在父项目中
继承与聚合
继承
项目的继承: A项目继承B项目: 继承B项目pom.xml
子项目继承父项目pom.xml
实际开发中项目结构: 分模块开发: 一个项目拆分为多个小模块(项目)
分模块开发有两种拆分方式:
按层拆分 学习过程中使用
shop-parent (父项目)
|-- shop-common(实体类,工具类) java项目
|-- shop-dao(Dao层) java项目
|--shop-service(业务逻辑层) java项目
|--shop-web(Servlet,页面,web项目)
按功能模块拆分: 实际开发使用模式
shop-parent (父项目)
|--shop-common(实体类,工具类) java项目
|-- shop-user(用户模块) dao,service
|-- shop-product(商品模块)dao,service
|--shop-order(订单模块)dao,service
|--....
|--shop-web(web项目, servlet,所有的页面)
搭建父项目:
>父项目要求: 打包方式: pom > >• 父项目不能写代码
修改父项目的打包方式: pom
<packaging>pom</packaging>
推荐删除父项目src目录:
在父项目的pom.xml文件中,添加一些通用的配置:
<properties>
中的编码,jdk, 版本变量<!--通用配置--> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.encoding>UTF-8</maven.compiler.encoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties>
创建子项目:
子项目的pom.xml
父项目pom.xml
聚合作用: 当聚合父项目进行相关的maven的指令操作, 对所有的被聚合模块都执行相同的操作
在实际开发中, 父项目也是聚合项目, 父项目聚合其他子项目
模块与模块之间的依赖关系:
shop-user 依赖 shop-common
shop-product依赖 shop-common
shop-order依赖 shop-common
shop-web依赖: shop-user ,shop-product,shop-order
配置的文件推荐放到web模块的src/main/resource
父项目中对整个子模块使用到所有的依赖进行版本控制
如果直接在父模块使用:
<!--依赖--> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>${fastjson.version}</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>${commons-fileupload.version}</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${javax.servlet-api.version}</version> <scope>provided</scope> </dependency> </dependencies>
每个子模块继承父模块的dependencies的依赖,每个子模块都有这些依赖
每个子模块按需加载: 子项目继承dependencies依赖, 继承依赖的配置
<dependencyManagement>
包裹<dependencies>
, 子模块只继承配置,而没有继承依赖,子模块需要那个依赖, 只要在子模块的pom.xml中,使用
<dependencies>
按需加载子模块加依赖,不需要写
<version>
父项目的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.fs</groupId> <artifactId>shop-parent</artifactId> <version>1.0-SNAPSHOT</version> <!--聚合模块: 父项目聚合那些模块--> <modules> <module>shop-common</module> <module>shop-user</module> <module>shop-product</module> <module>shop-order</module> </modules> <!--打包方式: 默认jar--> <!--修改父项目打包方式: pom--> <packaging>pom</packaging> <!--通用配置--> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.encoding>UTF-8</maven.compiler.encoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <!--自定义版本变量标签--> <mysql.version>5.1.47</mysql.version> <fastjson.version>1.2.75</fastjson.version> <commons-fileupload.version>1.4</commons-fileupload.version> <jstl.version>1.2</jstl.version> <javax.servlet-api.version>3.0.1</javax.servlet-api.version> </properties> <!--依赖--> <dependencyManagement> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>${fastjson.version}</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>${commons-fileupload.version}</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${javax.servlet-api.version}</version> <scope>provided</scope> </dependency> </dependencies> </dependencyManagement> </project>
子模块的pom.xml按需加载
<dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> </dependencies>
创建maven的web项目
web项目打包方式: war
maven创建web项目有两个问题:
缺少: src/main/java src/main/resource src/test/java
web.xml的版本2.3 需要的版本: 3.0以上
创建src/main/java src/main/resource src/test/java目录
修改web.xml