pom.xml里面各个配置的含义如下:

  1. <!-- 主项目标识,表示当前maven属于哪个实际项目,与包是一样的 -->
  2. <groupId>反写的公司网址+项目名</groupId>
  3. <!-- 模块标识-->
  4. <artifactId>项目名+模块名</artifactId>
  5. <!--
  6. 版本号,一般由三个数字组成
  7. 第一个0,表示大版本号,
  8. 第二个0表示分支版本号,
  9. 第三个0表示小版本号,
  10. snapshot 快照
  11. beta 公测
  12. alpha 内测
  13. Release 稳定
  14. GA正式发布
  15. -->
  16. <version></version>
  17. <!-- 打包的方式,默认为jar,还可以打包成war,zip,pom -->
  18. <packaging></packaging>
  19.  
  20. <!-- 项目描述名 -->
  21. <name></name>
  22. <!-- 项目的地址 -->
  23. <url></url>
  24. <!-- 项目的描述 -->
  25. <description></description>
  26. <!-- 开发人员信息 -->
  27. <developers></developers>
  28. <!-- 许可证信息 -->
  29. <licenses></licenses>
  30. <!-- 组织信息 -->
  31. <organization></organization>
  32.  
  33. <!-- 依赖列表-->
  34. <dependencies>
  35. <!-- 依赖项 -->
  36. <dependency>
  37. <groupId></groupId>
  38. <artifactId></artifactId>
  39. <version></version>
  40. <type></type>
  41. <!-- 依赖的范围 -->
  42. <scope></scope>
  43. <!-- 设置依赖是否可选,默认false。 -->
  44. <optional></optional>
  45. <!-- 排除依赖传递列表 -->
  46. <exclusions>
  47. <exclusion>
  48. </exclusion>
  49. </exclusions>
  50. </dependency>
  51. </dependencies>
  52.  
  53. <!-- 依赖的管理 -->
  54. <dependencyManagement>
  55. <dependencies>
  56. <dependency></dependency>
  57. </dependencies>
  58. </dependencyManagement>
  59.  
  60. <build>
  61. <!-- 插件列表 -->
  62. <plugins>
  63. <plugin>
  64. <groupId></groupId>
  65. <artifactId></artifactId>
  66. <version></version>
  67. </plugin>
  68. </plugins>
  69. </build>
  70.  
  71. <!-- 用于子模块对父模块pom的继承 -->
  72. <parent></parent>
  73. <!-- 用来聚合多个maven项 -->
  74. <modules>
  75. <module></module>
  76. </modules>

例如上一节创建的如下:

  1. <groupId>org.maven.mavenDemo</groupId>
  2. <artifactId>mavenDemo01</artifactId>
  3. <version>0.0.1-SNAPSHOT</version>
  4. <packaging>jar</packaging>
  5.  
  6. <name>mavenDemo01</name>
  7. <url>http://maven.apache.org</url>
  8.  
  9. <properties>
  10. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  11. </properties>
  12.  
  13. <dependencies>
  14. <dependency>
  15. <groupId>junit</groupId>
  16. <artifactId>junit</artifactId>
  17. <version>3.8.1</version>
  18. <scope>test</scope>
  19. </dependency>
  20. </dependencies>

依赖范围:<scop>

三种classpath:

  • 编译
  • 测试
  • 运行

scop选项:

  • compile:默认的范围,编译测试运行都有效
  • provided:在编译和测试时候有效
  • runtime:在测试和运行时有效
  • test:只在测试范围有效
  • system:与本机系统相关联,可移植性差。
  • import:导入的范围,只使用在dependenceManagement中。表示从其他的pom中导入dependecy的配置

依赖传递

这里建立三个maven项目演示

demo02要依赖demo01,要想依赖,必须在本地仓库安装demo01的项目

首先对demo01进行如下操作:

1,右键demo01,使用maven方式运行,将其打包:

2,将mavendemo01安装到本地仓库中,同样执行maven方式运行,执行install命令。

在demo02中加入demo01的依赖:

  1. <dependencies>
  2. <dependency>
  3. <groupId>junit</groupId>
  4. <artifactId>junit</artifactId>
  5. <version>3.8.1</version>
  6. <scope>test</scope>
  7. </dependency>
  8.  
  9. <dependency>
  10. <groupId>org.maven.mavenDemo</groupId>
  11. <artifactId>mavenDemo01</artifactId>
  12. <version>0.0.1-SNAPSHOT</version>
  13. </dependency>
  14.  
  15. </dependencies>

如下:

同样方式将demo02安装,为了省去操作,同时执行两个命令:clean  install

在demo03中依赖demo02项目。

  1. <dependencies>
  2. <dependency>
  3. <groupId>junit</groupId>
  4. <artifactId>junit</artifactId>
  5. <version>3.8.1</version>
  6. <scope>test</scope>
  7. </dependency>
  8.  
  9. <dependency>
  10. <groupId>org.maven.mavenDemo</groupId>
  11. <artifactId>mavenDemo02</artifactId>
  12. <version>0.0.1-SNAPSHOT</version>
  13. </dependency>
  14. </dependencies>

同样对demo03安装到本地仓库。

观察mavenDemo03的依赖包发现,demo03本来只想依赖demo02,但是连demo01也依赖了,

这表明依赖具有传递性

要想不依赖demo01,使用exclusions,可以排除demo01的依赖。

  1. <dependencies>
  2. <dependency>
  3. <groupId>junit</groupId>
  4. <artifactId>junit</artifactId>
  5. <version>3.8.1</version>
  6. <scope>test</scope>
  7. </dependency>
  8.  
  9. <dependency>
  10. <groupId>org.maven.mavenDemo</groupId>
  11. <artifactId>mavenDemo02</artifactId>
  12. <version>0.0.1-SNAPSHOT</version>
  13. <exclusions>
  14. <exclusion><!--下面填入mavenDemo01的坐标-->
  15. <groupId>org.maven.mavenDemo</groupId>
  16. <artifactId>mavenDemo01</artifactId>
  17. </exclusion>
  18. </exclusions>
  19. </dependency>
  20. </dependencies>

保存后发现只剩demo02的依赖了。

依赖冲突

解决依赖冲突的两条原则:

1,短路优先:A->B-C->X(jar)

       A->D->X(jar)

由于第二条路比较短,会依赖第二条的方式。

2,先声明先优先:

如果路径长度相同,则谁先声明,先解析谁。

聚合和继承

聚合

如果想要将多个项目进行install,安装到本地仓库中,必须对其依次进行install命令。

而聚合可以将多个项目放到一起运行,同时安装。

例如:将前面的三个项目聚合,一起安装。

新建一个mavenDemo04,在pom.xml里面配置如下:

首先把packaging改成pom

  1. <groupId>org.maven.mavenDemo</groupId>
  2. <artifactId>mavenDemo04</artifactId>
  3. <version>0.0.1-SNAPSHOT</version>
  4. <packaging>pom</packaging>

然后添加聚合标签:分别加入要安装的三个项目

  1. <modules>
  2. <module>
  3. ../mavenDemo01
  4. </module>
  5. <module>
  6. ../mavenDemo02
  7. </module>
  8. <module>
  9. ../mavenDemo03
  10. </module>
  11. </modules>

而这里的dependency无所谓了,可以删除。

然后运行maven命令,执行 clean  install命令。

此时就同时安装了三个项目到本地仓库。

继承

例如对于之前的三个项目中,每个项目都依赖了一个junit,其实这样重复了很多,可以使用继承方式代替这种。

1,新建一个demo5,demo5中定义如下:

  1. <groupId>org.maven.mavenDemo</groupId>
  2. <artifactId>mavenDemo05</artifactId>
  3. <version>0.0.1-SNAPSHOT</version>
  4. <packaging>pom</packaging>
  5.  
  6. <name>mavenDemo05</name>
  7. <url>http://maven.apache.org</url>
  8.  
  9. <properties>
  10. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  11. <junit.version>3.8.1</junit.version>
  12. </properties>
  13.  
  14. <dependencyManagement>
  15. <dependencies>
  16. <dependency>
  17. <groupId>junit</groupId>
  18. <artifactId>junit</artifactId>
  19. <version>${junit.version}</version>
  20. <scope>test</scope>
  21. </dependency>
  22. </dependencies>
  23. </dependencyManagement>

一共关注三点:

1,将packaging改为pom

2,在properties中新增一共junit.version属性。然后可以在version标签中通过${属性名}的方式使用。

3,新增一个dependencyManagement标签,将dependencyies标签放进去。

假如在demo3中继承这里。

demo3中junit依赖定义如下:

  1. <dependencies>
  2. <dependency>
  3. <groupId>junit</groupId>
  4. <artifactId>junit</artifactId>
  5. <version>3.8.1</version>
  6. <scope>test</scope>
  7. </dependency>
  1. </dependencies>

修改,需要将上面的红色部分删除,然后添加一个parent标签。parent标签引入demo05的坐标。

  1. <parent>
  2. <groupId>org.maven.mavenDemo</groupId>
  3. <artifactId>mavenDemo05</artifactId>
  4. <version>0.0.1-SNAPSHOT</version>
  5. </parent>
  6.  
  7. <dependencies>
  8. <dependency>
  9. <groupId>junit</groupId>
  10. <artifactId>junit</artifactId>
  11. </dependency>
  12.  
  13. <dependency>
  14. <groupId>org.maven.mavenDemo</groupId>
  15. <artifactId>mavenDemo02</artifactId>
  16. <version>0.0.1-SNAPSHOT</version>
  17. <exclusions>
  18. <exclusion>
  19. <groupId>org.maven.mavenDemo</groupId>
  20. <artifactId>mavenDemo01</artifactId>
  21. </exclusion>
  22. </exclusions>
  23. </dependency>
  24. </dependencies>

04-maven学习-pom.xml解析的更多相关文章

  1. maven项目pom.xml解析

  2. Maven项目pom.xml文件简单解析

    Maven项目pom.xml简单解析 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="h ...

  3. 史上最全的maven的pom.xml文件详解(转载)

    此文出处:史上最全的maven的pom.xml文件详解——阿豪聊干货 <project xmlns="http://maven.apache.org/POM/4.0.0" x ...

  4. (六)Maven之pom.xml文件简单说明

    通过前面几部分知识,我们对maven已经有了初步的印象,就像Make的Makefile.Ant的build.xml一样,Maven项目的核心是pom.xml.POM(Project Object Mo ...

  5. SSH项目搭建(四)——Maven的pom.xml配置

    史上最全的maven的pom.xml文件详解: https://www.cnblogs.com/qq765065332/p/9238135.html 下面的节点有不理解是啥意思的可以到上面链接的文章里 ...

  6. maven(4)------maven核心pom.xml文件常用元素分析

    在maven项目中,pom文件是核心文件 pom.xml: <?xml version="1.0" encoding="UTF-8"?> <p ...

  7. Maven项目pom.xml配置详解

    maven项目pom.xml文件配置详解,需要时可以用作参考: <project xmlns="http://maven.apache.org/POM/4.0.0" xmln ...

  8. Maven 教程(6)— Maven之pom.xml文件简单说明

    原文地址:https://blog.csdn.net/liupeifeng3514/article/details/79543963 通过前面几部分知识,我们对maven已经有了初步的印象,就像Mak ...

  9. [转]Maven的pom.xml文件详解

    Maven的pom.xml文件详解------Build Settings 2013年10月30日 13:04:01 阅读数:44678 根据POM 4.0.0 XSD,build元素概念性的划分为两 ...

随机推荐

  1. hdu 5207 Greatest Greatest Common Divisor 数学

    Greatest Greatest Common Divisor Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/ ...

  2. 解决新版本webpack vue-cli生成文件没有dev.server.js问题

    新版本webpack生成的dev.server.js 在webpack.dev.conf.js中 webpack.dev.conf.js const axios = require('axios') ...

  3. (转)Ext.Button点击事件的三种写法

    转自:http://maidini.blog.163.com/blog/static/377627042008111061844345/ ExtJs的写法太灵活了,现在收集了关于Button点击事件的 ...

  4. NSArray与NSMutableArray 数组与可变数组的创建和遍历 复习

    1.NSArray 是一个父类,NSMUtableArray是其子类,他们构成了OC的数组. 2.NSArray的创建 NSArray * array = [[NSArray alloc]initWi ...

  5. 强悍的javascript手势库

    /** * Toucher * git:https://github.com/cometwo/Toucher-1 */ "use strict"; (function (root, ...

  6. Delegates and Events

    People often find it difficult to see the difference between events and delegates. C# doesn't help m ...

  7. Arcgis license 服务无法启动的解决问题

    来自:http://blog.csdn.net/u013719339/article/details/51240312 1.检查服务开没开.打开资源管理器然后按照下面就出现了.也可以打开运行——ser ...

  8. python接口自动化10-token登录

    前言 有些登录不是用cookie来验证的,是用token参数来判断是否登录. token传参有两种一种是放在请求头里,本质上是跟cookie是一样的,只是换个单词而已:另外一种是在url请求参数里,这 ...

  9. Singleton 单例模式(懒汉方式和饿汉方式)

    单例模式的概念: 单例模式的意思就是只有一个实例.单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例.这个类称为单例类. 关键点: 1)一个类只有一个实例       这是最基本 ...

  10. python的with和__enter__ 、 __exit__

    __enter__(): 在使用with语句时调用,会话管理器在代码块开始前调用,返回值与as后的参数绑定 __exit__():   会话管理器在代码块执行完成好后调用,在with语句完成时,对象销 ...