Maven介绍
(1)Maven 是 Apache 提供的一款自动化构建工具,用于自动化构建和依赖管理。
(2)Maven的本质是一个项目管理工具,将项目开发和管理过程抽象成一个项目对象模型(POM)
(3)Maven的作用
| 作用 | 简介 |
|---|
| 项目构建 | 项目依赖管理,软件项目持续集成 |
| 版本管理 | 项目的站点描述信息管理 |
| 项目构建 | 提供标准的、跨平台的自动化项目构建方式 |
| 依赖管理 | 方便快捷的管理项目依赖的资源(jar包),避免资源间的版本冲突问题 |
Maven仓库
(1)仓库用于存储资源,包含各种jar包
(2)仓库分类
| 分类 | 简介 |
|---|
| 本地仓库 | 自己电脑上存储资源的仓库,连接远程仓库获取资源 |
| 远程仓库 | 非本机电脑上的仓库,为本地仓库提供资源 中央仓库:Maven团队维护,存储所有资源的仓库 私服:部门/公司范围内存储资源的仓库,从中央仓库获取资源 |
(3)三种仓库关系

Maven坐标
(1)坐标是Jar包的唯一标识,Maven通过坐标在仓库中找到项目所需的Jar包
(2)Maven坐标示例
1 2 3 4 5 6 7 8
| <dependency> <groupId>com.example</groupId> <artifactId>example-library</artifactId> <version>1.0.0</version> </dependency>
|
(3)查找坐标
https://mvnrepository.com/
https://central.sonatype.com/?smo=true
https://developer.aliyun.com/mvn/search
Maven构建命令
| 命令 | 功能 | 简介 |
|---|
| mvn clean | 清理 | 删除target文件夹,表示在编译代码前将之前生成的内容删除 |
| mvn compile | 编译 | 编译源代码,将源代码编译为字节码,将java源文件编译成class文件 |
| mvn test | 编译测试 | 执行单元测试用例程序 |
| mvn package | 打包 | 打包,将 java 项目打成 jar 包;将 Web 项目打成 war 包 |
| mvn install | 安装 | 打包并把打好的包保存到本地仓库 将当前项目 jar 或 war 生成到 Maven 仓库中,供其他项目使用 |
| mvn deploy | 上传 | 打包并把打好的包上传到远程仓库 |
| mvn -v | 查询 | 查询Maven版本 |
依赖管理
依赖配置
(1)在 Maven 项目中,通过 POM 文件中的 <dependencies> 标签来配置依赖项
1 2 3 4 5 6 7
| <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>example-library</artifactId> <version>1.0.0</version> </dependency> </dependencies>
|
(2)除了 <dependency> 标签之外,还可以使用其他标签来配置依赖项的属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <dependency> <groupId>com.example</groupId> <artifactId>example-library</artifactId> <version>1.0.0</version> <scope>compile</scope> <optional>false</optional> <exclusions> <exclusion> <groupId>org.unwanted.dependency</groupId> <artifactId>unwanted-artifact</artifactId> </exclusion> </exclusions> </dependency>
|
| 依赖范围可选项 | 简介 |
|---|
| compile | 默认范围,用于编译 |
| provided | 类似于编译,但支持你期待jdk或者容器提供,类似于classpath |
| runtime | 在执行时需要使用 |
| test | 用于test任务时使用 |
| system | 需要外在提供相应的元素,通过systemPath来取得 |
| systemPath | 仅用于范围为system,提供相应的路径 |
| optional | 当项目自身被依赖时,标注依赖是否传递。用于连续依赖时使用 |
依赖传递
A依赖了B和C,B和C有分别依赖了其他jar包,所以在A项目中就可以使用上面所有jar包,这就是所说的依赖传递。B,C,D,E,F,G代表的是项目所依赖的jar包;D1和D2、E1和E2代表是相同jar包的不同版本

可选依赖
在 Maven 项目中,有时我们需要引入一些可选的依赖项,这些依赖项可能只在某些情况下才需要使用,但如果不加以配置,可能会导致编译错误或运行时异常。Maven 提供了 optional 标签来解决这个问题。在下面的例子中,我们将 example-library 这个库标记为可选依赖,这意味着 Maven 在构建项目时不会强制下载这个库及其相关的依赖项。如果我们需要使用这个库,可以在其他地方手动添加它
1 2 3 4 5 6 7 8
| <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>example-library</artifactId> <version>1.0.0</version> <optional>true</optional> </dependency> </dependencies>
|
排除依赖
排除依赖指主动断开依赖的资源,被排除的资源无需指定版本(不需要)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <dependency> <groupId>com.example</groupId> <artifactId>example-library</artifactId> <version>1.0.0</version> <exclusions> <exclusion> <groupId>org.unwanted.dependency</groupId> <artifactId>unwanted-artifact</artifactId> </exclusion> </exclusions> </dependency>
|
完整pom文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
| <?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.0http://maven.apache.org/maven-v4_0_0.xsd"> <parent> <artifactId/> <groupId/> <version/> </parent> <modelVersion>4.0.0</modelVersion> <groupId>cn.missbe.web</groupId>
<artifactId>search-resources</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>search-resources</name> <url>http://www.missbe.cn</url> <description>A maven project to study maven.</description> <prerequisites> <maven/> </prerequisites> <build> <sourceDirectory/> <scriptSourceDirectory/> <testSourceDirectory/> <outputDirectory/> <testOutputDirectory/> <extensions> <extension> <groupId/> <artifactId/> <version/> </extension> </extensions> <resources> <resource> <targetPath/> <filtering/> <directory/> <includes/> <excludes/> </resource> </resources> <testResources> <testResource> <targetPath/> <filtering/> <directory/> <includes/> <excludes/> </testResource> </testResources> <directory/> <finalName/> <filters/> <pluginManagement> <plugins> <plugin> <groupId/> <artifactId/> <version/> <extensions/> <executions> <execution> <id/> <phase/> <goals/> <inherited/> <configuration/> </execution> </executions> <dependencies> <dependency>......</dependency> </dependencies> <inherited/> <configuration/> </plugin> </plugins> </pluginManagement> <plugins> <plugin> <groupId/> <artifactId/> <version/> <extensions/> <executions> <execution> <id/> <phase/> <goals/> <inherited/> <configuration/> </execution> </executions> <dependencies> <dependency>......</dependency> </dependencies> <goals/> <inherited/> <configuration/> </plugin> </plugins> </build> <modules/> <repositories> <repository> <releases> <enabled/> <updatePolicy/> <checksumPolicy/> </releases> <snapshots> <enabled/> <updatePolicy/> <checksumPolicy/> </snapshots> <id>banseon-repository-proxy</id> <name>banseon-repository-proxy</name> <url> http://192.168.1.169:9999/repository/</url> <layout>default</layout> </repository> </repositories> <pluginRepositories> <pluginRepository>......</pluginRepository> </pluginRepositories> <dependencies> <dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-artifact</artifactId> <version>3.8.1</version>
<type>jar</type> <classifier/>
<scope>test</scope> <systemPath/> <exclusions> <exclusion> <artifactId>spring-core</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> <optional>true</optional> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency>......</dependency> </dependencies> </dependencyManagement> <distributionManagement> <repository> <uniqueVersion/> <id>banseon-maven2</id> <name>banseon maven2</name> <url>file://${basedir}/target/deploy</url> <layout/> </repository> <snapshotRepository> <uniqueVersion/> <id>banseon-maven2</id> <name>Banseon-maven2 Snapshot Repository</name> <url> scp://svn.baidu.com/banseon:/usr/local/maven-snapshot</url> <layout/> </snapshotRepository> <site> <id>banseon-site</id> <name>business api website</name> <url> scp://svn.baidu.com/banseon:/var/www/localhost/banseon-web</url> </site>
<downloadUrl/> <status/> </distributionManagement> <properties/> </project>
|