1 简介
POM概念
POM( Project Object Model,项目对象模型 ) 是 Maven 工程的基本工作单元,是一个XML文件,包含了项目的基本信息,用于描述项目如何构建,声明项目依赖,等等。
pom.xml 项目模型对象文件。POM 中可以指定以下配置:
- 坐标GAV、Name、pakage、author
- 项目依赖 dependencies/dependency
- 项目模块 modules/module
- 项目集成 parent
- 项目构建 build、plugins/plugin
- 配置文件 profiles/profile
pom工程之间的关系
- 在dependency中的依赖关系。
- 在parent中的继承关系
- 在module中的聚合关系
2 依赖关系dependency
依赖的传递主要通过dependency标签实现的
scope依赖范围标签
scope的取值用来定义依赖生效的空间(main目录、test目录)和时间(开发中、部署中)。
标签位置 dependencies/dependency/scope
标签取值 compile/test/provided/system/runtime/import
- compile是最基本的标签,在所有的范围、所有的时间都能生效。
- test标签,仅仅在test目录开发阶段生效的依赖,生效空间和时间范围最小。
- prvoide标签,在开发阶段生效,在部署阶段不生效的依赖(因为部署环境提供了该依赖,不需要打包到jar或者war包中)
- import maven也是单继承。通过import导入多个依赖。导入的依赖必须是POM工程,package中的打包方式是pom

- system 强行将当前系统下的jar包作为依赖导入进来。使用系统根路径,可以是相对路径。

- runtime编译时不需要,但是运行时需要的jar。实际运行时需要接口的实现类。JDBC接口标准提供了一系列借口,能够进行编译。但是运行时需要JDBC的具体实现。

| 值 | 解释 |
|---|
| compile | 默认的scope,表示 dependency 都可以在生命周期中使用。而且,这些dependencies 会传递到依赖的项目中。适用于所有阶段,会随着项目一起发布。 |
| provided | 跟compile相似,但是表明了dependency 由JDK或者容器提供,例如Servlet AP和一些Java EE APIs。这个scope 只能作用在编译和测试时,同时没有传递性。 |
| runtime | 表示dependency不作用在编译时,但会作用在运行和测试时,如JDBC驱动,适用运行和测试阶段。 |
| test | 表示dependency作用在测试时,不作用在运行时。 只在测试时使用,用于编译和运行测试代码。不会随项目发布。 |
system | 与provided类似,但是它不会去maven仓库寻找依赖,而是在本地找;而systemPath标签将提供本地路径 |
| import | 这个标签就是 引入该dependency的pom中定义的所有dependency定义 |
### optional可选依赖
在开发阶段需要的类,但是在运行阶段可能不需要的类,就不需要再打包的时候导入到其中。

依赖的传递性
能够通过依赖传递,导入大量相关的间接依赖。而不需要手动导入所有的依赖。
- A依赖B,B依赖C的时候,A不需要引入C即可使用C依赖。A会间接依赖C
- compile范围的依赖能够传递,test和provide范围的依赖无法传递。
依赖的排除
阻断依赖的传递性。多个间接依赖可能不兼容,需要排除不兼容的间接依赖。

标签位置 dependency/exclusions/exclusion
1 2 3 4 5 6 7 8
| <dependency> <exclusions> <exclusion> <groupId></groupId> <artifactId></artifactId> </exclusion> <exclusions> </dependency>
|
版本仲裁

- 最短路径优先
- 路径相同先声明者优先。
3 继承关系parent
依赖的继承主要是通过parent标签实现的。
继承关系
Maven工程之间,A工程继承B工程
本质上是A工程的pom.xml中的配置继承了B工程中pom.xml的配置。
继承,也就是父工程,主要管理依赖信息的版本
继承的作用
在付工程中统一管理项目中的依赖信息,管理以来信息的版本。
背景
- 一个大型的项目进行了模块拆分
- 一个project下面创建了很多module
- 每一个module都需要自己的依赖信息
需求
- 每一个module中各自维护各自的依赖信息很容易发生不一致的情况,不易统一管理
- 使用框架内的的不同的jar包,应该是同一个版本,所以整个项目使用的框架版本需要统一
- 使用框架时所需要的jar包组合,需要经过长期摸索和反复调试,最终确定一个可用的组合。
通过在父工程中为整个项目维护依赖信息的组合,既保证了整个项目的使用规范、准确的jar包;又能讲以往的经验沉淀下来,节约时间和精力。
继承的使用
- 创建父工程,修改工程的打包方式。只有打包方式为pom的maven工程能够管理其他的maven工程。打包方式为pom的maven工程总不写业务代码,是专门管理其他maven工程的工程。
1 2 3 4
| <groupId>com.ykl</groupId> <artifactId>project-maven</artifactId> <version>1.0.2</version> <packaging>pom</packaging>
|
创建模块工程。idea中的module。从聚合分解的角度分析就是聚合和模块两种工程。而不是父子工程。
模块间的依赖关系
- 在父工程中通过modules来引入子工程
- 在子工程中通过parent引入父工程
1 2 3 4 5
| <parent> <groupId></groupId> <artifactId></artifactId> <version></version> </parent>
|
- 在付工程进行依赖信息的统一管理
1 2 3 4 5 6
| <dependencyManagement> <depencies> <dependency> </dependency> </dependencies> </dependencyManagement>
|
继承中的属性
创建自定以的属性标签,标签名也就是属性名,标签纸也就是属性值。
引用方式 $com.ykl.spring.version$。通过属性名解析后才知道具体的版本号值。
1 2 3 4 5
| <properties> <com.ykl.spring.version>4.1.0.RELEASE</com.ykl.spring.version> </properties>
${com.ykl.spring.version}
|
实际意义

- 梳理出一整套版本之间依赖的方案。与我们的火车头版本很想,会总结出一套可以相互兼容没有问题的中间件的合集。
继承内容
父POM中的大多数元素都能被子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
| groupId version description url inceptionYear organization licenses developers contributors mailingLists scm issueManagement ciManagement properties dependencyManagement dependencies repositories pluginRepositories build plugin executions with matching ids plugin configuration etc. reporting profiles
|
4 聚合关系modules
聚合的含义
通过moudules标签聚合子项目。好处
- 如果没有聚合标签,或者说聚合功能,则需要去每个模块下执行maven命令,并且按照依赖顺序从前到后执行。
- 如果有聚合标签,可以一键执行maven命令。mvn 会按照模块之间的依赖关系依次执行命令。
- 能够显示各个所有的模块。
1 2 3 4 5
| <modules> <module>project04-maven-moduele</module> <module>project05-maven-moduele</module> <module>project06-maven-moduele</module> </modules>
|
循环依赖
1 2
| the ... dependencies has cyclic dependency
|
5 POM的层次
基本概念
Maven不仅仅用来做依赖管理和项目构建。还用来做项目管理。
POM有四个层次:超级POM、父POM、当前POM、生效POM
超级pom
默认继承了超级POM。超级POM
- moduleversion版本信息
- repository 仓库的地址
- pluginrepostory 插件仓库的地址
- builid 主要设置各个目录的位置。
- 插件管理
- profiles 配置文件
父POM
当前的POM的parent
当前POM
下层POM会继承上层POM。最近的有效配置是下层pom
有效POM
最终生效的POM文件,将所有的父POM叠加到一起
6 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 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
| <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 />
<relativePath /> </parent> <modelVersion>4.0.0</modelVersion> <groupId>asia.banseon</groupId>
<artifactId>banseon-maven2</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>banseon-maven</name> <url>http://www.baidu.com/banseon</url>
<description>A maven project to study maven.</description> <prerequisites> <maven /> </prerequisites> <issueManagement> <system>jira</system> <url>http://jira.baidu.com/banseon</url> </issueManagement> <ciManagement> <system /> <url /> <notifiers> <notifier> <type /> <sendOnError /> <sendOnFailure /> <sendOnSuccess /> <sendOnWarning /> <address /> <configuration /> </notifier> </notifiers> </ciManagement> <inceptionYear /> <mailingLists> <mailingList> <name>Demo</name> <post>[email protected]</post> <subscribe>[email protected]</subscribe> <unsubscribe>[email protected]</unsubscribe> <archive>http:/hi.baidu.com/banseon/demo/dev/</archive> </mailingList> </mailingLists> <developers> <developer> <id>HELLO WORLD</id> <name>banseon</name> <email>[email protected]</email> <url /> <roles> <role>Project Manager</role> <role>Architect</role> </roles> <organization>demo</organization> <organizationUrl>http://hi.baidu.com/banseon</organizationUrl> <properties> <dept>No</dept> </properties> <timezone>-5</timezone> </developer> </developers> <contributors> <contributor> <name /> <email /> <url /> <organization /> <organizationUrl /> <roles /> <timezone /> <properties /> </contributor> </contributors> <licenses> <license> <name>Apache 2</name> <url>http://www.baidu.com/banseon/LICENSE-2.0.txt</url> <distribution>repo</distribution> <comments>A business-friendly OSS license</comments> </license> </licenses> <scm> <connection> scm:svn:http://svn.baidu.com/banseon/maven/banseon/banseon-maven2-trunk(dao-trunk) </connection> <developerConnection> scm:svn:http://svn.baidu.com/banseon/maven/banseon/dao-trunk </developerConnection> <tag /> <url>http://svn.baidu.com/banseon</url> </scm> <organization> <name>demo</name> <url>http://www.baidu.com/banseon</url> </organization> <build> <sourceDirectory /> <scriptSourceDirectory /> <testSourceDirectory /> <outputDirectory /> <testOutputDirectory /> <extensions> <extension> <groupId /> <artifactId /> <version /> </extension> </extensions> <defaultGoal /> <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> <profiles> <profile> <id /> <activation> <activeByDefault /> <jdk /> <os> <name>Windows XP</name> <family>Windows</family> <arch>x86</arch> <version>5.1.2600</version> </os> <property> <name>mavenVersion</name> <value>2.0.3</value> </property> <file> <exists>/usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/ </exists> <missing>/usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/ </missing> </file> </activation> <build> <defaultGoal /> <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> <goals /> <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 /> <name /> <url /> <layout /> </repository> </repositories> <pluginRepositories> <pluginRepository> <releases> <enabled /> <updatePolicy /> <checksumPolicy /> </releases> <snapshots> <enabled /> <updatePolicy /> <checksumPolicy /> </snapshots> <id /> <name /> <url /> <layout /> </pluginRepository> </pluginRepositories> <dependencies> <dependency> ...... </dependency> </dependencies> <reports /> <reporting> ...... </reporting> <dependencyManagement> <dependencies> <dependency> ...... </dependency> </dependencies> </dependencyManagement> <distributionManagement> ...... </distributionManagement> <properties /> </profile> </profiles> <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></classifier>
<scope>test</scope> <systemPath></systemPath> <exclusions> <exclusion> <artifactId>spring-core</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> <optional>true</optional> </dependency> </dependencies> <reports></reports> <reporting> <excludeDefaults /> <outputDirectory /> <plugins> <plugin> <groupId /> <artifactId /> <version /> <inherited /> <configuration /> <reportSets> <reportSet> <id /> <configuration /> <inherited /> <reports /> </reportSet> </reportSets> </plugin> </plugins> </reporting>
<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 /> <relocation> <groupId /> <artifactId /> <version /> <message /> </relocation>
<status /> </distributionManagement> <properties /> </project>
|