MAVEN高级应用
分模块设计
介绍
分模块设计即将一个项目分成几个模块开发
优点:方便项目的维护、管理和拓展,模块间相互调用,资源共享更方便
实现
以前面的javaWeb项目为例,进行拆分
步骤
1.将原项目中的工具类拆分为一个独立的模块,在模块中创建相同的包存放工具类
在新模块的pom文件中引入所需依赖(jwt,阿里云oss,web起步依赖)
2.将项目中的实体类拆分为一个独立的模块,在模块中创建相同的包存放实体类
3.删除原项目中的jwt,阿里云oss依赖,引入工具类模块和实体类模块坐标
4.创建一个父工程,统一管理依赖,父工程的打包方式为pom文件方式,在原有的工程以及新建的模块中设置父工程坐标及相对路径,原项目的父工程坐标迁移到现父工程中,抽取子工程共有的依赖(lombok依赖)到父工程中去,子模块自动继承父工程的依赖,简化子模块依赖配置
5.在父工程中使用<dependencyManagement>标签统一管理依赖版本,配置后子工程中引入相关依赖不再需要指定版本,默认使用父工程配置版本,方便后期维护更新,也可在<properties>标签中使用自定义属性和属性引用的方式统一管理依赖版本
6.将父工程设置为聚合工程,快速构建项目,使用<modules><module>xxx</module></modules>配置参与构建的模块
而后只需在聚合工程中构建项目即可实现多个模块的项目构建
工程结构图如下:
父工程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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!--
在聚合工程中聚合其他模块
聚合:将多个模块组织成一个整体,同时进行项目的构建
聚合工程:一个不具有业务功能的“空”工程(有且仅有一个pom文件)PS:一般来说,继承关系中的父工程与聚合关系中的聚合工程是同一个
作用:快速构建项目(无需根据依赖关系手动构建,直接在聚合工程上构建即可)
不使用聚合的情况下,若要打包tlias-web-management模块,需要先将此工程依赖的tlias-pojo模块
和tlias-utils模块install到本地仓库,然后才能成功打包tlias-web-management模块
而在聚合工程中聚合了这三个模块后,只需要在聚合工程中执行package命令,聚合的模块都会同时执行该命令
从而实现了多个模块同时构建,不必根据依赖关系自己手动一个个模块构建
-->
<modules>
<module>../tlias-pojo</module>
<module>../tlias-utils</module>
<module>../tlias-web-management</module>
</modules>
<groupId>com.sihfan</groupId>
<artifactId>tlias-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<!--
使用<properties>标签,通过自定义属性和属性引用的方式进行版本管理
-->
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<lombok.version>1.18.24</lombok.version>
<jjwt.version>0.9.1</jjwt.version>
<aliyun.oss.version>3.15.1</aliyun.oss.version>
<jaxb.version>2.3.1</jaxb.version>
<activation.version>1.1.1</activation.version>
<jaxb.runtime.version>2.3.3</jaxb.runtime.version>
<fastJSON.version>1.2.76</fastJSON.version>
<PageHelper.version>1.4.2</PageHelper.version>
<mybatis.version>2.2.2</mybatis.version>
</properties>
<!--
依赖继承,在父工程引入的依赖会被子工程继承
简化子工程依赖配置
-->
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<!--
使用<dependencyManagement>标签
在父工程进行统一依赖管理,用于保持子工程依赖版本一致
父工程不会引入这些依赖,仅仅指定版本
子工程要使用这些依赖还是要引入,但无需再指定版本
-->
<dependencyManagement>
<dependencies>
<dependency>
<!--jwt令牌-->
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
<!--阿里云OSS-->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.15.1</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<!-- no more than 2.3.3-->
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.3</version>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 配置本地私服地址 -->
<distributionManagement>
<!-- release版本的发布地址 -->
<repository>
<id>maven-releases</id>
<url>http://localhost:8081/repository/maven-releases/</url>
</repository>
<!-- snapshot版本的发布地址 -->
<snapshotRepository>
<id>maven-snapshots</id>
<url>http://localhost:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
<!--
dependencyManagement和dependencies的区别:
前者不会引入依赖,仅仅对依赖版本进行统一控制管理,子工程中还是要引入依赖,但不需要指定版本
后者是直接引入依赖,若父工程中引入了依赖,则子工程会继承下来
-->
<!--
继承的作用:简化子工程依赖引入,统一依赖管理
聚合的作用:快速构建项目
相同点:pom.xml文件的打包方式都是pom方式,通常将两种关系制作到同一个pom文件中,都是设计型模块,并无实际内容
不同点:继承是在子模块中配置关系,父模块无法感知哪些模块继承了自己
聚合是在聚合工程中配置关系,聚合工程可以感知到哪些模块参与了聚合
-->
</project>
实体类模块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>
<artifactId>tlias-parent</artifactId>
<groupId>com.shifan</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../tlias-parent/pom.xml</relativePath>
</parent>
<groupId>com.sihfan</groupId>
<artifactId>tlias-pojo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project>
工具类模块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>
<artifactId>tlias-parent</artifactId>
<groupId>com.shifan</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../tlias-parent/pom.xml</relativePath>
</parent>
<groupId>com.shifan</groupId>
<artifactId>tlias-utils</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<!--web起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--JWT令牌-->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
</dependency>
<!--阿里云OSS-->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
</dependency>
<!-- no more than 2.3.3-->
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
</dependency>
</dependencies>
</project>
原项目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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>tlias-parent</artifactId>
<groupId>com.shifan</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../tlias-parent/pom.xml</relativePath>
</parent>
<groupId>com.shifan</groupId>
<artifactId>tlias-web-management</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<!--web起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mybatis起步依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<!--属性引用,引用父工程自定义属性中的mybatis依赖版本-->
<version>${mybatis.version}</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<!--springboot单元测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--PageHelper分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<!--属性引用,引用父工程自定义属性中的PageHelper依赖版本-->
<version>${PageHelper.version}</version>
</dependency>
<!--fastJSON-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<!--属性引用,引用父工程自定义属性中的fastJSON依赖版本-->
<version>${fastJSON.version}</version>
</dependency>
<!--AOP-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!--其他模块依赖-->
<dependency>
<groupId>com.itheima</groupId>
<artifactId>tlias-pojo</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.itheima</groupId>
<artifactId>tlias-utils</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
私服
作用
架设在局域网内的仓库服务,用于解决团队内部的资源共享和资源同步问题
资源上传与下载步骤
第一步配置:在maven的配置文件中配置访问私服的用户名、密码。
第二步配置:在maven的配置文件中配置连接私服的地址(url地址)。
第三步配置:在项目的pom.xml文件中配置上传资源的位置(url地址)。
配置好了上述三步之后,要上传资源到私服仓库,就执行执行maven生命周期:deploy。
私服仓库说明:
- RELEASE:存储自己开发的RELEASE发布版本的资源。
- SNAPSHOT:存储自己开发的SNAPSHOT发布版本的资源。
- Central:存储的是从中央仓库下载下来的依赖。
项目版本说明:
- RELEASE(发布版本):功能趋于稳定、当前更新停止,可以用于发行的版本,存储在私服中的RELEASE仓库中。
- SNAPSHOT(快照版本):功能不稳定、尚处于开发中的版本,即快照版本,存储在私服的SNAPSHOT仓库中。