什么是pom

pom代表项目对象模型,它是Maven中工作的基本组成单位。它是一个XML文件,始终保存在项目的基本目录中的pom.xml文件中。pom包含的对象是使用maven来构建的,pom.xml文件包含了项目的各种配置信息。 创建一个POM之前,应该要先决定项目组(groupId),项目名(artifactId)和版本(version),因为这些属性在项目仓库是唯一标识的。需要特别注意,每个项目都只有一个pom.xml文件。

pom中的节点分布如下

  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. http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <!-- 基本配置 -->
  7. <groupId>...</groupId>
  8. <artifactId>...</artifactId>
  9. <version>...</version>
  10. <packaging>...</packaging>
  11. <!-- 依赖配置 -->
  12. <dependencies>...</dependencies>
  13. <parent>...</parent>
  14. <dependencyManagement>...</dependencyManagement>
  15. <modules>...</modules>
  16. <properties>...</properties>
  17. <!-- 构建配置 -->
  18. <build>...</build>
  19. <reporting>...</reporting>
  20. <!-- 项目信息 -->
  21. <name>...</name>
  22. <description>...</description>
  23. <url>...</url>
  24. <inceptionYear>...</inceptionYear>
  25. <licenses>...</licenses>
  26. <organization>...</organization>
  27. <developers>...</developers>
  28. <contributors>...</contributors>
  29. <!-- 环境设置 -->
  30. <issueManagement>...</issueManagement>
  31. <ciManagement>...</ciManagement>
  32. <mailingLists>...</mailingLists>
  33. <scm>...</scm>
  34. <prerequisites>...</prerequisites>
  35. <repositories>...</repositories>
  36. <pluginRepositories>...</pluginRepositories>
  37. <distributionManagement>...</distributionManagement>
  38. <profiles>...</profiles>
  39. </project>

各节点解释说明

基本配置信息

  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. http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <!-- pom模型版本,maven2和3只能为4.0.0-->
  6. <modelVersion>4.0.0</modelVersion>
  7. <!-- 项目的组ID,用于maven定位-->
  8. <groupId>com.company.bank</groupId>
  9. <!-- 项目ID,通常是项目的名称,唯一标识符-->
  10. <artifactId>parent</artifactId>
  11. <!-- 项目的版本-->
  12. <version>1.0</version>
  13. <!-- 项目的打包方式-->
  14. <packaging>war</packaging>
  15. <project>

依赖配置

  1. dependencies 项目相关依赖配置,如果在父项目写的依赖,会被子项目引用。一般会在父项目中定义子项目中所有共用的依赖。
  2. parent 用于确定父项目的坐标位置。
  3. modules 有些maven项目会做成多模块的,这个标签用于指定当前项目所包含的所有模块。之后对这个项目进行的maven操作,会让所有子模块也进行相同操作。
  4. properties 用于定义pom常量
  5. dependencyManagement 在父模块中定义后,子模块不会直接使用对应依赖,但是在使用相同依赖的时候可以不加版本号,这样的好处是,父项目统一了版本,而且子项目可以在需要的时候才引用对应的依赖。 ```xml 父项目: junit junit 4.12 test

子项目:

junit junit

  1. <a name="qVA6i"></a>
  2. #### 构建配置
  3. ```xml
  4. <build>
  5. <!--该元素设置了项目源码目录,当构建项目的时候,构建系统会编译目录里的源码。该路径是相对于pom.xml的相对路径。-->
  6. <sourceDirectory/>
  7. <!--该元素设置了项目脚本源码目录,该目录和源码目录不同:绝大多数情况下,该目录下的内容 会被拷贝到输出目录(因为脚本是被解释的,而不是被编译的)。-->
  8. <scriptSourceDirectory/>
  9. <!--该元素设置了项目单元测试使用的源码目录,当测试项目的时候,构建系统会编译目录里的源码。该路径是相对于pom.xml的相对路径。-->
  10. <testSourceDirectory/>
  11. <!--被编译过的应用程序class文件存放的目录。-->
  12. <outputDirectory/>
  13. <!--被编译过的测试class文件存放的目录。-->
  14. <testOutputDirectory/>
  15. <!--使用来自该项目的一系列构建扩展-->
  16. <extensions>
  17. <!--描述使用到的构建扩展。-->
  18. <extension>
  19. <!--构建扩展的groupId-->
  20. <groupId/>
  21. <!--构建扩展的artifactId-->
  22. <artifactId/>
  23. <!--构建扩展的版本-->
  24. <version/>
  25. </extension>
  26. </extensions>
  27. <!--当项目没有规定目标(Maven2 叫做阶段)时的默认值-->
  28. <defaultGoal/>
  29. <!--这个元素描述了项目相关的所有资源路径列表,例如和项目相关的属性文件,这些资源被包含在最终的打包文件里。-->
  30. <resources>
  31. <!--这个元素描述了项目相关或测试相关的所有资源路径-->
  32. <resource>
  33. <!-- 描述了资源的目标路径。该路径相对target/classes目录(例如${project.build.outputDirectory})。举个例 子,如果你想资源在特定的包里(org.apache.maven.messages),你就必须该元素设置为org/apache/maven /messages。然而,如果你只是想把资源放到源码目录结构里,就不需要该配置。-->
  34. <targetPath/>
  35. <!--是否使用参数值代替参数名。参数值取自properties元素或者文件里配置的属性,文件在filters元素里列出。-->
  36. <filtering/>
  37. <!--描述存放资源的目录,该路径相对POM路径-->
  38. <directory/>
  39. <!--包含的模式列表,例如**/*.xml.-->
  40. <includes/>
  41. <!--排除的模式列表,例如**/*.xml-->
  42. <excludes/>
  43. </resource>
  44. </resources>
  45. <!--这个元素描述了单元测试相关的所有资源路径,例如和单元测试相关的属性文件。-->
  46. <testResources>
  47. <!--这个元素描述了测试相关的所有资源路径,参见build/resources/resource元素的说明-->
  48. <testResource>
  49. <targetPath/><filtering/><directory/><includes/><excludes/>
  50. </testResource>
  51. </testResources>
  52. <!--构建产生的所有文件存放的目录-->
  53. <directory/>
  54. <!--产生的构件的文件名,默认值是${artifactId}-${version}。-->
  55. <finalName/>
  56. <!--当filtering开关打开时,使用到的过滤器属性文件列表-->
  57. <filters/>
  58. <!--子项目可以引用的默认插件信息。该插件配置项直到被引用时才会被解析或绑定到生命周期。给定插件的任何本地配置都会覆盖这里的配置-->
  59. <pluginManagement>
  60. <!--使用的插件列表 。-->
  61. <plugins>
  62. <!--plugin元素包含描述插件所需要的信息。-->
  63. <plugin>
  64. <!--插件在仓库里的group ID-->
  65. <groupId/>
  66. <!--插件在仓库里的artifact ID-->
  67. <artifactId/>
  68. <!--被使用的插件的版本(或版本范围)-->
  69. <version/>
  70. <!--是否从该插件下载Maven扩展(例如打包和类型处理器),由于性能原因,只有在真需要下载时,该元素才被设置成enabled。-->
  71. <extensions/>
  72. <!--在构建生命周期中执行一组目标的配置。每个目标可能有不同的配置。-->
  73. <executions>
  74. <!--execution元素包含了插件执行需要的信息-->
  75. <execution>
  76. <!--执行目标的标识符,用于标识构建过程中的目标,或者匹配继承过程中需要合并的执行目标-->
  77. <id/>
  78. <!--绑定了目标的构建生命周期阶段,如果省略,目标会被绑定到源数据里配置的默认阶段-->
  79. <phase/>
  80. <!--配置的执行目标-->
  81. <goals/>
  82. <!--配置是否被传播到子POM-->
  83. <inherited/>
  84. <!--作为DOM对象的配置-->
  85. <configuration/>
  86. </execution>
  87. </executions>
  88. <!--项目引入插件所需要的额外依赖-->
  89. <dependencies>
  90. <!--参见dependencies/dependency元素-->
  91. <dependency>
  92. ......
  93. </dependency>
  94. </dependencies>
  95. <!--任何配置是否被传播到子项目-->
  96. <inherited/>
  97. <!--作为DOM对象的配置-->
  98. <configuration/>
  99. </plugin>
  100. </plugins>
  101. </pluginManagement>
  102. <!--使用的插件列表-->
  103. <plugins>
  104. <!--参见build/pluginManagement/plugins/plugin元素-->
  105. <plugin>
  106. <groupId/><artifactId/><version/><extensions/>
  107. <executions>
  108. <execution>
  109. <id/><phase/><goals/><inherited/><configuration/>
  110. </execution>
  111. </executions>
  112. <dependencies>
  113. <!--参见dependencies/dependency元素-->
  114. <dependency>
  115. ......
  116. </dependency>
  117. </dependencies>
  118. <goals/><inherited/><configuration/>
  119. </plugin>
  120. </plugins>
  121. </build>
  1. resource.filtering 是否使用参数值代替参数名。参数值取自properties元素或者文件里配置的属性,文件在filters元素里列出。
  2. filters 当filtering开关打开时,使用到的过滤器属性文件列表

    Profile

    profile里可以定义所有pom里定义的内容,当一个profile被激活时,他定义的内容会替换原pom中相同的内容

  3. 显式激活

    1. mvn -U clean package -Ptest
  4. 隐式激活
    1. 默认激活 activeByDefault
    2. 根据操作系统类型激活
    3. 根据JDK版本激活
    4. 根据环境变量激活
    5. 通过判断文件是否存在激活