【maven】8、pom.xml介绍(一)

本章主要整体介绍pom.xml有哪些功能,即介绍在project顶级节点下可以使用哪些节点。
Maven官网关于pom介绍地址 http://maven.apache.org/pom.html

pom.xml主要分为四大部分:基础节点、Build配置、项目扩展信息、环境配置,当然还有一个modelVersion。

<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>

  <!-- The Basics -->
  <groupId>...</groupId>
  <artifactId>...</artifactId>
  <version>...</version>
  <packaging>...</packaging>
  <dependencies>...</dependencies>
  <parent>...</parent>
  <dependencyManagement>...</dependencyManagement>
  <modules>...</modules>
  <properties>...</properties>

  <!-- Build Settings -->
  <build>...</build>
  <reporting>...</reporting>

  <!-- More Project Information -->
  <name>...</name>
  <description>...</description>
  <url>...</url>
  <inceptionYear>...</inceptionYear>
  <licenses>...</licenses>
  <organization>...</organization>
  <developers>...</developers>
  <contributors>...</contributors>

  <!-- Environment Settings -->
  <issueManagement>...</issueManagement>
  <ciManagement>...</ciManagement>
  <mailingLists>...</mailingLists>
  <scm>...</scm>
  <prerequisites>...</prerequisites>
  <repositories>...</repositories>
  <pluginRepositories>...</pluginRepositories>
  <distributionManagement>...</distributionManagement>
  <profiles>...</profiles>
</project>

The Basics

Maven Coordinates(Maven坐标)

  • project
    pom.xml文件顶级元素
  • modelVersion
    POM版本,一般不怎么改动
  • groupId
    当前Maven项目隶属于于具体项目的名称,一般都是用公司名称标识.如com.mycompany。用spring项目为例,groupId为org.springframework。
  • artifactld
    项目名称,在groupId下所有项目唯一,一般打包时作为包名前缀。spring项目中有spring-core,spring-jdbc等
  • packaging
    项目构件类型:pom, jar, maven-plugin, ejb, war, ear, rar, par。默认值为jar
  • version
    项目版本,在打包时快照名称为${artifactld}-${version}.jar 格式为:主版本.次版本.增量版本-限定版本号
  • classifier
    当需要将构建的demo.jar包的源代码也一起输出,就需要使用classifier。生成demo-source.jar包不仅需要classifier,还需要Maven插件(maven-source-plugin,maven-javadoc-plugin),这是一种情况。
    还有一种情况是,当项目引入依赖包时dependency,当前所需要引入的依赖包由不同的JRE编译,它们的groupId、artifactId、version是一致,这是就需要通过classifier来区分。

POM Relationships

Dependencies Maven依赖管理
当在项目中需要使用第三方依赖包时,需要添加dependencies-dependency

<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
                      https://maven.apache.org/xsd/maven-4.0.0.xsd">
  ...
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.0</version>
      <type>jar</type>
      <scope>test</scope>
      <optional>true</optional>
    </dependency>
    ...
  </dependencies>
  ...
</project>
  • classifier
    上面解释了classifier用法,我们用具体的例子说明。
    https://maven.nuxeo.org/nexus/ 网站搜了一下存在这种情况的依赖包,如json-lib。2.4版本下存储jdk13、jdk15两个jar,当我们引用时dependency元素下需要添加<classifier>jdk15</classifier>
    这里写图片描述
<dependency>
  <groupId>net.sf.json-lib</groupId>
  <artifactId>json-lib</artifactId>
  <version>2.4</version>
  <classifier>jdk15</classifier>
</dependency>
  • type
    依赖包类型jar, ejb-client and test-jar,默认jar。当extensions设置为true是,可以使用插件定义新的类型。

  • scope
    scope指classpath在哪些范围下可用及限制依赖传递范围。一共有五种类型。
    1) compile
    scope默认值,没有限制范围,在所有classpath中可用,此外依赖可用被所有依赖项目传递。
    2) provided
    provided类似于compile,使用前提必须在JDK或者容器提供该依赖。仅适用于compilation and test classpath,不具有传递性。
    例如, 如果你开发了一个web 应用,你可能在编译
    classpath 中需要可用的Servlet API 来编译一个servlet,但是你不会想要在打包好的WAR 中包含这个Servlet API;这个
    Servlet API JAR 由你的应用服务器或者servlet 容器提供。已提供范围的依赖在编译classpath (不是运行时)可用。它们
    不是传递性的,也不会被打包。
    3) runtime
    运行时范围,编译不需要依赖关系,而是用于执行。它是在运行时和测试使用,编译不需要。
    比如,你可能在编译的时候只需要JDBC API JAR,而只
    有在运行的时候才需要JDBC
    4) test
    test范围依赖 在一般的编译和运行时都不需要,它们只有在测试编译和测试运行阶段可用。
    5) system
    类似于provided,依赖不从Maven资源查找而是根据指定的路径使用本地资源。和systemPath配合使用。
    scope的依赖传递
    A–>B–>C。当前项目为A,A依赖于B,B依赖于C。知道B在A项目中的scope,那么怎么知道C在A中的scope呢?答案是:
    当C是test或者provided时,C直接被丢弃,A不依赖C;
    否则A依赖C,C的scope继承于B的scope。
    下面是一张nexus画的图。
    这里写图片描述
    scope一节参考
    http://drizzlewalk.blog.51cto.com/2203401/665590
    http://blog.csdn.net/kimylrong/article/details/50353161

  • systemPath
    当scope为system时使用,指定本地依赖路径

  • optional
    当一个项目A依赖另一个项目B时,项目A只是用到B的一部分功能,此时可以在A中配置对B的可选依赖。举例来说,一个类似hibernate的项目,它支持对mysql、oracle等各种数据库的支持,但是在引用这个项目时,我们可能只用到其对mysql的支持,此时就可以在这个项目中配置可选依赖。如果有一个新的项目X依赖A,即:Project-X -> Project-A。此时项目X就不会依赖项目B了。如果项目X用到了涉及项目B的功能,那么就需要在pom.xml中重新配置对项目B的依赖。
    http://www.tuicool.com/articles/yaeIV3

  • version
    管理引入依赖包的版本。如果我们想每次都引用最新版本,那么就用version定义版本范围,规则如下:
    1) 1.0:非必须匹配,只是建议Maven使用1.0,可以匹配其他版本
    2) [1.0]:必须使用1.0版本
    3) (,1.0]:x<=1.0,版本小于等于1.0
    4) [1.2,1.3]:1.2 <= x <= 1.3
    5) [1.0,2.0): 1.0 <= x < 2.0
    6) [1.5,): x >= 1.5
    7) (,1.0],[1.2,): x <= 1.0 or x >= 1.2;
    8) (,1.1),(1.1,): 不包含1.1

  • Exclusions 依赖排除
    当项目A引用项目B,项目B引用项目C,当项目A不想使用项目C,需要将其排除,使用Exclusions。如项目使用maven-embedder,但是不希望使用maven-embedder中的maven-core。

<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
                      https://maven.apache.org/xsd/maven-4.0.0.xsd">
  ...
  <dependencies>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-embedder</artifactId>
      <version>2.0</version>
      <exclusions>
        <exclusion>
          <groupId>org.apache.maven</groupId>
          <artifactId>maven-core</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    ...
  </dependencies>
  ...
</project>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值