maven介绍
maven是apache公司下的一个jar包管理项目
maven将为程序员自动下载jar包,并确保版本相互兼容
maven将提供除idea外的项目构建解决方案
maven拥有三级仓库,中央仓库(境外服务器),私服仓库(国内或者局域网服务器),本地仓库(本机)
maven事先将大量的jar包下载到离程序员更近的本地仓库,在编写项目时可以直接从本地仓库下载,速度更快
maven配置文件
从官网下载maven的zip文件,并解压
位于conf文件夹中的settings.xml是maven的配置文件
要在settings中指定本地仓库的路径,中央仓库镜像网站
<localRepository>C:\Users\root\Desktop\mvnrepo</localRepository>
<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>
<profile>
<id>jdk-11</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.compilerVersion>11</maven.compiler.compilerVersion>
</properties>
</profile>
idea新建maven工程,指定本地仓库
- maven解压目录
- settings.xml路径
- 本地仓库路径
依赖管理
依赖传递:A依赖B,B依赖C,那么在A工程中,maven会一次性把B连带C导入
依赖冲突:依赖传递势必会导致大量重复或无关的jar包被导入到工程中,即产生了依赖重复或互不兼容的问题
最短优先原则:maven优先使用依赖路径最短的jar包
前排优先原则:当路径相同时,maven会使用位于pom文件偏上的jar包
依赖选择建议:很多人导依赖时,是乱来的,所以会有缺依赖或者相互冲突的情况,我建议选择发布时间相近的依赖导入,主要还是以发布时间和版本大更还是小更为准
<?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.pengjingzhao.shop</groupId>
<artifactId>demo2</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>common-service</module>
<module>user-service</module>
</modules>
<packaging>pom</packaging>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>6.0.15</spring.version>
<jackson.version>2.15.2</jackson.version>
<commons.version>2.14.0</commons.version>
<junit.version>5.10.0-M1</junit.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-war-plugin -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.1</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
pom常见标签
package(工程打包方式):
- pom:仅作依赖版本集中管理,不参与打包
- jar
- war
dependencyManagement: - 管理依赖版本,子工程不会把依赖全部导入
build - 配置构建使用的插件,文件的名称还有要编译的文件
构建过程
构建过程也叫生命周期,是指maven中一系列构建命令,并且项目的构建永远是以pom文件中的配置为前提
后面的命令包含前面的命令
构建过程中,看好控制台的日志信息
命令 | 解释 |
---|---|
mvn clean | 删除编译文件(target) |
mvn compile | 生成编译文件(target) |
mvn package | 打包(jar/war) |
mvn install | 打包并复制到本地仓库 |
mvn test | 执行工程中的测试方法 |