文章目录
1. Maven
功能点: 构建管理和依赖管理工具,用于自动化构建、测试、打包和发布项目,统一构建规则。
引入maven前后:
前: 工程依赖引入jar包,将jar包复制到工程下才可以使用依赖
后: 利用maven可以简化依赖导入,标签化导入,maven会自动引入需要的依赖以及依赖的依赖
maven构建过程: 清理 编译 测试 报告 打包 部署
1.1 maven安装
- 具备Java环境
- 配置maven环境
- 更改本地仓库位置
maven/conf/setting.xml
<localRepository>D:/maven/repository</localRepository>
- 更改国内aliyun的镜像仓库,提高远程仓库的下载效率
<mirrors>
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
</mirrors>
- 配置构建JDK版本
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
1.2 IDEA上如何使用
1.2.1 配置maven主路经
在IDEA种更换Maven为当前构建工具 setting/build/Maven--maven-path
更换为本地maven的主路径
###1.2.2指定maven种的GVAP(groupId,version,artifactId,packaging)
- groupId: 全球唯一项目id(com.公司域名.工程业务)
- version: 根据公司内部要求实施,一般的1.0.0中,第一位指的是当模块改动时需要新增1,第二位指的是当业务逻辑有所修改时新增1,第三位指的是修复某个bug时新增1;
- artifactId: 产品模块id,例如ssm项目下的spring模块
- packaging: 工程打包方式,
jar、war、pom
1.3 Maven项目目录结构
api
├── pom.xml 依赖管理文件
└── src 核心代码工作区
└── main 代码工作区
├── java 逻辑代码工作区
├── resources 业务资源配置文件
└── webapp web资源存放
├── index.jsp
└── WEB-INF web
└── web.xml
1.4 引入依赖
maven中央仓库搜索需要引入的jar包坐标,https://mvnrepository.com/
在IDEA的插件市场安装maven搜索插件maven-search
<dependencies>
<dependency>
<groupId>${g}</groupId>
<artifactId>${a}</artifactId>
<version>${v}</version>
<scope>${scope}</scope>
</dependency>
<dependency>
<groupId>${g}</groupId>
<artifactId>${a}</artifactId>
<version>${v}</version>
<scope>${scope}</scope>
</dependency>
<dependency>
<groupId>${g}</groupId>
<artifactId>${a}</artifactId>
<version>${v}</version>
<scope>${scope}</scope>
</dependency>
</dependencies>
-
版本包统一管理
-
在properties中声明自定义标签,表示版本号(依赖名.version)
<properties> <spring.version>5.1.2</spring.version> <mybatis.version>5.1.2</mybatis.version> <junit.version>5.1.2</junit.version> .... </properties>
-
在GAV中引入集中版本号
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency>
-
-
依赖可用范围
- compile: main test 打包和运行
- test: test在测试中生效
- runtime: main test除外,打包和运行的时候生效
- provided: 生产模式的时候生效 servlet依赖在tomcat中有,但是生产时没有servlet
1.5 依赖冲突和依赖传递
依赖传递: 当工程中需要依赖A,只需要引入依赖A即可,但是依赖A有依赖了依赖B,依赖B又依赖了依赖C……,还需要我们手动引入依赖么?此时maven会自动的导入对应的依赖,也就是所谓的依赖传递
依赖冲突: 遇到依赖冲突则停止依赖传递 ,例如A->B->C->A,则在依赖C时终止依赖传递,从而避免依赖冲突和循环依赖
解决原则: 按照引入路径较短的原则引入依赖,如果路径一样长按照谁先引入谁优先
依赖1 A1.1.0 -> B1.1.0 -> C2.1.0
依赖2 E1.2.0 -> B2.1.0 -> T2.1.0
maven引入顺序: A1.1.0 E1.2.0
问:项目中存在哪些依赖呢?
A1.1.0 E1.2.0 B1.1.0 C2.1.0
B时谁先来谁优先导入,T不导入是因为在B2.1.0时终端了依赖传递
1.6 maven依赖引入失败问题
从中央仓库拉镜像时,网络出现错误时会在本地仓库中遗留一个lastUpdated后缀文件,一旦存在再下载也会出现错误,此时就需要删掉这个lastUpdated后缀文件即可,下面是一个windows 删除lastUpdated后缀文件的bat脚本,选择自己的本地仓库地址运行即可。
@echo off
rem 仓库路径
set REPOSITORY_PATH=D:\maven\repository
rem load search...
for /f "delims=" %%i in ('dir /b /s "%REPOSITORY_PATH%\*lastUpdated*"') do (
del /s /q %%i
)
rem search ok
pause
1.7 扩展构建管理和插件配置
1.7.1 构建命令
命令 | 描述 |
---|---|
mvn clean | 清理编译或者打包后的项目结构删掉target文件夹 |
mvn compile | 编译项目,生成target文件 |
mvn test | 执行测试源码 |
mvn site | 生成一个项目依赖信息的展示页面 |
mvn package | 打包项目,生成war / jar 文件 |
mvn install | 打包后上传到maven本地仓库(本地部署) |
mvn deploy | 之打包,上传到maven私服仓库(私服部署) |
1.7.2 构建命令周期
- 清理周期 clean
- 构建周期 compile test package install / deploy
- 报告周期 site
- 最佳周期: mvn clean cpackage mvn clearn compile 本地部署: mvn clean install
1.8 父子工程
父工程用于规定待使用的依赖信息,用于子工程引入,当子工程中需要使用依赖时只需要写GA即可V在父工程中已经声明。父工程中的依赖只是声明并不会引入依赖,只有子工程中引入时才会引入到子工程的依赖中。
1.8.1 子工程继承父工程
<!-- 子工程使用parent标签引入父工程的 gav即可 -->
<parent>
<groupId>com.sean</groupId>
<artifactId>testMavenParent</artifactId>
<version>1.0.0</version>
</parent>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<!-- version 父工程中已经声明 -->
</dependency>
</dependencies>
1.8.2 父工程中声明版本信息
<!-- 父工程中统一管理依赖信息 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
</dependencies>
</dependencyManagement>
工程聚合
在父工程中聚合子模块
<modules>
<module>shop-api</module>
<module>shop-user</module>
<module>shop-order</module>
<module>shuo-car</module>
</modules>
1.9 maven模拟搭建
maven工程目录结构模拟
- m-shop-user-service模块引入 spring-context jackson-databind
- m-shop-order-service模块引入 spring-context shiro-core
- m-shop-common-service模块引入 commons-io
m-shop
├── m-shop-common-service
│ ├── pom.xml
│ └── src
│ └── main
│ └── webapp
│ ├── index.jsp
│ └── WEB-INF
│ └── web.xml
├── m-shop-order-service
│ ├── pom.xml
│ └── src
│ └── main
│ └── webapp
│ ├── index.jsp
│ └── WEB-INF
│ └── web.xml
├── m-shop-user-service
│ ├── pom.xml
│ └── src
│ └── main
│ ├── resources
│ └── webapp
│ └── WEB-INF
│ ├── applicationContext.xml
│ ├── log4j.xml
│ └── web.xml
└── pom.xml
<!-- 父工程依赖结构 子工程按需引入即可-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>6.0.6</spring.version>
<jackson.version>2.15.0</jackson.version>
<shiro.version>1.10.0</shiro.version>
<io.version>2.11.0</io.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>${shiro.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${io.version}</version>
</dependency>
</dependencies>
</dependencyManagement>