zoukankan      html  css  js  c++  java
  • POM(project Object Model) Maven包管理依赖 pom.xml文件

    什么是POM

    POM全称为“Project Object Model”,意思是工程对象模型。Maven工程使用pom.xml来指定工程配置信息,和其他文本信息。该配置文件以xml为格式,使用xml语法表明信息。

    快速预览

    一个pom.xml文件主要包括以下元素信息:

    pom.xml

    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
    <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坐标)

    groupId:artifactId:version构成了Maven工程的坐标系统。

    • groundId: 组织标识,例如:org.codehaus.mojo,在M2_REPO目录下,将是: org/codehaus/mojo目录。
    • artifactId:项目名称,例如:my-project,在M2_REPO目录下,将是:org/codehaus/mojo/my-project目录。
    • version:版本号,例如:1.0,在M2_REPO目录下,将是:org/codehaus/mojo/my-project/1.0目录。
    • packaging:打包格式,可选值:jar(默认值), maven-pluginejbwarearrarpar

    POM Relationships(POM关系)

    Maven主要用于处理项目之间的关系,包括依赖关系(和过渡依赖)、继承和聚合(多模块项目)。

    Dependencies(依赖)

    大多数每个项目都需要依赖其他人构建的项目,Maven则可以对这些依赖进行管理 。在
    dependencies标签中指明所需要依赖的功能模块,Maven会自动下载编译和链接其依赖关系。另外,Maven屏蔽了这些依赖带来的更多的依赖项(依赖传递),让开发者只需要关注自己项目需要的依赖关系。

    junit4

     1 <project xmlns="http://maven.apache.org/POM/4.0.0"
     2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
     4 https://maven.apache.org/xsd/maven-4.0.0.xsd">
     5 ...
     6 <dependencies>
     7 <dependency>
     8 <groupId>junit</groupId>
     9 <artifactId>junit</artifactId>
    10 <version>4.0</version>
    11 <type>jar</type>
    12 <scope>test</scope>
    13 <optional>true</optional>
    14 </dependency>
    15 ...
    16 </dependencies>
    17 ...
    18 </project>
    • groupId, artifactId, version: 用于精准定位dependency
    • classifier: 用于区分名字相同但内容不同的POM。例如:jdk15jdk14指明目标版本;sourcesjavadoc指明部署的是源码还是文档。
    • type: 与packging中的type相对应。
    • scope: 用于指明dependency作用范围,有5种值:
      1. compile: 默认的scope,表示dependency可以在所有的生命周期中使用。而且,这些dependencies会传递到依赖的项目中。适用于所有阶段,会随着项目一起发布。
      2. provided:跟compile相似,但是表明了dependencyJDK或者容器提供,例如Servlet API和一些Java EE APIs。另外该dependency 只能作用在编译和测试时,同时没有传递性。
      3. runtime:表示dependency不作用在编译时,但会作用在运行和测试时,如JDBC驱动,适用运行和测试阶段。
      4. test:表示dependency只在测试时使用,用于编译和运行测试代码。不会随项目发布。
      5. system:跟provided相似,但是在系统中要以外部JAR包的形式提供,maven不会在repository查找它。
    • systemPath:只在scopesystem时有效,指明dependency路径。
    • optional:指明该项目没有该dependency,依然可以正确运行。
    Dependency Version Requirement Specification(依赖版本规范)
    • 1.0: “软性”要求 1.0 (推荐方式)
    • [1.0]: “硬性要求” 1.0
    • (,1.0]: <= 1.0
    • [1.2,1.3]: 1.2 <= x <= 1.3
    • [1.0,2.0): 1.0 <= x < 2.0
    • [1.5,): x >= 1.5
    • (,1.0],[1.2,): x <= 1.0 or x >= 1.2
    • (,1.1),(1.1,): 排除 1.1
    Exclusions(排除)

    排除掉dependency的依赖传递中的某个dependency。和optional不同,exclusions不会安装和使用该dependency,并从依赖树上去除它。例如,某个依赖树种某dependency可能会导致错误,则应该排除掉。

    Inheritance(继承)

    POM对象可以实现继承,子POM对象将从父POM继承各属性。

    Aggregation(聚类(多模块))

    一个pom打包项目通过聚合多个模块来构建,开发者不需要考虑模块间的依赖关系。

    Properties(属性)

    Maven中的Properties是占位符,可以在POM中任何一个地方使用符号${X},其中X是该Property。有以下5中形式:

    • env.X:系统变量,区分大小写。例如:${env.PATH}表示系统系统路径变量。
    • project.x:POM中相应的值。例如:<project><version>1.0</version></project>可以通过${project.version}访问。
    • settings.x settings.xml中相应的值。例如,<settings><offline>false</offline></settings>可以通过${settings.offline}访问。
    • Java System Properties:例如:${java.home}
    • x:POM中<properties />标签中的值。例如:<properties><someVar>value</someVar></properies>可以通过${someVar}访问。

    Build Settings

    Build(构建)

    build元素分为两块:”project build”和”profile build”。

    build

    1 <!-- "Project Build" contains more elements than just the BaseBuild set -->
    2 <build>...</build>
    3  
    4 <profiles>
    5 <profile>
    6 <!-- "Profile Build" contains a subset of "Project Build"s elements -->
    7 <build>...</build>
    8 </profile>
    9 </profiles>

    The BaseBuild Element Set 

    BaseBuild

    1 <build>
    2 <defaultGoal>install</defaultGoal>
    3 <directory>${basedir}/target</directory>
    4 <finalName>${artifactId}-${version}</finalName>
    5 <filters>
    6 <filter>filters/filter1.properties</filter>
    7 </filters>
    8 ...
    9 </build>


    defaultGoal

      :默认执行行为。
    • directory:构建目录。
    • finalName:文件名。
    • filter:过滤器。

    The Build Element Set

    Build:

    <build>
    <sourceDirectory>${basedir}/src/main/java</sourceDirectory>
    <scriptSourceDirectory>${basedir}/src/main/scripts</scriptSourceDirectory>
    <testSourceDirectory>${basedir}/src/test/java</testSourceDirectory>
    <outputDirectory>${basedir}/target/classes</outputDirectory>
    <testOutputDirectory>${basedir}/target/test-classes</testOutputDirectory>
    ...
    </build>
    

      


    定义工程目录。

    Reporting(报告)

    生成工程报告,如javadoc。

    Reporting

    <reporting>
    <plugins>
    <plugin>
    ...
    <reportSets>
    <reportSet>
    <id>sunlink</id>
    <reports>
    <report>javadoc</report>
    </reports>
    <inherited>true</inherited>
    <configuration>
    <links>
    <link>http://java.sun.com/j2se/1.5.0/docs/api/</link>
    </links>
    </configuration>
    </reportSet>
    </reportSets>
    </plugin>
    </plugins>
    </reporting>


     

    More Project Information

    • name:工程名。
    • description:工程描述。
    • url:工程URL。
    • inceptionYear:开始时间。

    Licenses(许可)

    Licenses

    <licenses>
    <license>
    <name>Apache License, Version 2.0</name>
    <url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
    <distribution>repo</distribution>
    <comments>A business-friendly OSS license</comments>
    </license>
    </licenses>


    name, url和comments

      :许可描述。
    • distribution:指明工程如何分布。repo,代表可以从Maven repository下载;manual,代表必须手动安装。

    Organization(组织)

    大多数项目都是由某组织(公司、私人组织等)管理。这是最基本的信息。

    Organization

    1
    2
    3
    4
    <organization>
    <name>Codehaus Mojo</name>
    <url>http://mojo.codehaus.org</url>
    </organization>

    Developers(开发者)

    Developers

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    <developers>
    <developer>
    <id>jdoe</id>
    <name>John Doe</name>
    <email>jdoe@example.com<email>
    <url>http://www.example.com/jdoe</url>
    <organization>ACME</organization>
    <organizationUrl>http://www.example.com</organizationUrl>
    <roles>
    <role>architect</role>
    <role>developer</role>
    </roles>
    <timezone>America/New_York</timezone>
    <properties>
    <picUrl>http://www.example.com/jdoe/pic</picUrl>
    </properties>
    </developer>
    </developers>
    • id, name, email:开发者身份标识。
    • organization, organizationUrl:个人从属组织信息。
    • roles:开发者在项目中的角色。
    • timezone:时区。
    • properties:个人属性。

    Contributors(参与者)

    开源项目往往有很多Contributors参与,基本元素节点信息与Developers类似。

    Environment Settings

    Issue Management(问题管理)

    定义了缺陷跟踪系统(Bugzilla、TestTrack ClearQuest等)的使用,主要用于生成项目文档。

    Continuous Integration Management(连续集成管理)

    用于自动化构建。

    Mailing Lists(邮件列表)

    项目开发人员联系方式。

    SCM(软件配置管理)

    SCM(Software Configuration Management,也叫Source Code/Control Management),用于版本控制。

    Prerequisites(先决条件)

    工程构建的先决条件。

    Repositories(仓库)

    Repositories

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    <repositories>
    <repository>
    <releases>
    <enabled>false</enabled>
    <updatePolicy>always</updatePolicy>
    <checksumPolicy>warn</checksumPolicy>
    </releases>
    <snapshots>
    <enabled>true</enabled>
    <updatePolicy>never</updatePolicy>
    <checksumPolicy>fail</checksumPolicy>
    </snapshots>
    <id>codehausSnapshots</id>
    <name>Codehaus Snapshots</name>
    <url>http://snapshots.maven.codehaus.org/maven2</url>
    <layout>default</layout>
    </repository>
    </repositories>
    <pluginRepositories>
    ...
    </pluginRepositories>
    • releases, snapshots:正式版和快照版(开发版)。
    • enabled:是否启用。
    • updatePolicy:更新频率。
    • checksumPolicy:校验政策。
    • layout:Maven仓库布局。

    Activation(激活)

    Activation

    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
    <profiles>
    <profile>
    <id>test</id>
    <activation>
    <activeByDefault>false</activeByDefault>
    <jdk>1.5</jdk>
    <os>
    <name>Windows XP</name>
    <family>Windows</family>
    <arch>x86</arch>
    <version>5.1.2600</version>
    </os>
    <property>
    <name>sparrow-type</name>
    <value>African</value>
    </property>
    <file>
    <exists>${basedir}/file2.properties</exists>
    <missing>${basedir}/file1.properties</missing>
    </file>
    </activation>
    ...
    </profile>
    </profiles>
    </project>

    在某些条件下修改工程配置。

    reference

    [1] https://sawyersun.github.io/2016/06/14/pom/

  • 相关阅读:
    DBA操作规范
    MySQL高可用之MHA
    Get MySQL这5个优化技巧,你将如虎添翼
    数据库的那些事
    Kubernetes
    nginx错误分析 `104: Connection reset by peer`
    kubernets资源预留
    kubernetes Pod亲和性
    kubernetes cpu限制参数说明
    zabbix 面板graph图上没有数据显示
  • 原文地址:https://www.cnblogs.com/hoojjack/p/6745557.html
Copyright © 2011-2022 走看看