作者:dunwu

https://github.com/dunwu/blog

(点击即可跳转阅读)

1. SpringBoot内容聚合

2. 面试题内容聚合

3. 设计模式内容聚合

4. Mybatis内容聚合

5. 多线程内容聚合

简介

什么是 pom?

POM 是 Project Object Model 的缩写,即项目对象模型。

pom.xml 就是 maven 的配置文件,用以描述项目的各种信息。

pom 配置一览

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  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.   <!-- The Basics -->
  8.   <groupId>...</groupId>
  9.   <artifactId>...</artifactId>
  10.   <version>...</version>
  11.   <packaging>...</packaging>
  12.   <dependencies>...</dependencies>
  13.   <parent>...</parent>
  14.   <dependencyManagement>...</dependencyManagement>
  15.   <modules>...</modules>
  16.   <properties>...</properties>
  17.  
  18.   <!-- Build Settings -->
  19.   <build>...</build>
  20.   <reporting>...</reporting>
  21.  
  22.   <!-- More Project Information -->
  23.   <name>...</name>
  24.   <description>...</description>
  25.   <url>...</url>
  26.   <inceptionYear>...</inceptionYear>
  27.   <licenses>...</licenses>
  28.   <organization>...</organization>
  29.   <developers>...</developers>
  30.   <contributors>...</contributors>
  31.  
  32.   <!-- Environment Settings -->
  33.   <issueManagement>...</issueManagement>
  34.   <ciManagement>...</ciManagement>
  35.   <mailingLists>...</mailingLists>
  36.   <scm>...</scm>
  37.   <prerequisites>...</prerequisites>
  38.   <repositories>...</repositories>
  39.   <pluginRepositories>...</pluginRepositories>
  40.   <distributionManagement>...</distributionManagement>
  41.   <profiles>...</profiles>
  42. </project>

基本配置

  • project - project 是 pom.xml 中描述符的根。
  • modelVersion - modelVersion 指定 pom.xml 符合哪个版本的描述符。maven 2 和 3 只能为 4.0.0。

一般 jar 包被识别为: groupId:artifactId:version 的形式。

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  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>org.codehaus.mojo</groupId>
  8.   <artifactId>my-project</artifactId>
  9.   <version>1.0</version>
  10.   <packaging>war</packaging>
  11. </project>

maven 坐标

在 maven 中,根据 groupIdartifactIdversion 组合成 groupId:artifactId:version 来唯一识别一个 jar 包。

  • groupId - 团体、组织的标识符。团体标识的约定是,它以创建这个项目的组织名称的逆向域名(reverse domain name)开头。一般对应着 java 的包结构。
  • artifactId - 单独项目的唯一标识符。比如我们的 tomcat、commons 等。不要在 artifactId 中包含点号(.)。
  • version - 一个项目的特定版本。
  • maven 有自己的版本规范,一般是如下定义 major version、minor version、incremental version-qualifier ,比如 1.2.3-beta-01。要说明的是,maven 自己判断版本的算法是 major、minor、incremental 部分用数字比较,qualifier 部分用字符串比较,所以要小心 alpha-2 和 alpha-15 的比较关系,最好用 alpha-02 的格式。
  • maven 在版本管理时候可以使用几个特殊的字符串 SNAPSHOT、LATEST、RELEASE。比如 1.0-SNAPSHOT。各个部分的含义和处理逻辑如下说明:
    • SNAPSHOT - 这个版本一般用于开发过程中,表示不稳定的版本。
    • LATEST - 指某个特定构件的最新发布,这个发布可能是一个发布版,也可能是一个 snapshot 版,具体看哪个时间最后。
    • RELEASE :指最后一个发布版。
  • packaging - 项目的类型,描述了项目打包后的输出,默认是 jar。常见的输出类型为:pom, jar, maven-plugin, ejb, war, ear, rar, par。

依赖配置

dependencies

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  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>org.apache.maven</groupId>
  9.       <artifactId>maven-embedder</artifactId>
  10.       <version>2.0</version>
  11.       <type>jar</type>
  12.       <scope>test</scope>
  13.       <optional>true</optional>
  14.       <exclusions>
  15.         <exclusion>
  16.           <groupId>org.apache.maven</groupId>
  17.           <artifactId>maven-core</artifactId>
  18.         </exclusion>
  19.       </exclusions>
  20.     </dependency>
  21.     ...
  22.   </dependencies>
  23.   ...
  24. </project>
  • groupIdartifactIdversion - 和基本配置中的 groupIdartifactIdversion 意义相同。
  • type - 对应 packaging 的类型,如果不使用 type 标签,maven 默认为 jar。
  • scope - 此元素指的是任务的类路径(编译和运行时,测试等)以及如何限制依赖关系的传递性。有 5 种可用的限定范围:
  • compile - 如果没有指定 scope 标签,maven 默认为这个范围。编译依赖关系在所有 classpath 中都可用。此外,这些依赖关系被传播到依赖项目。
  • provided - 与 compile 类似,但是表示您希望 jdk 或容器在运行时提供它。它只适用于编译和测试 classpath,不可传递。
  • runtime - 此范围表示编译不需要依赖关系,而是用于执行。它是在运行时和测试 classpath,但不是编译 classpath。
  • test - 此范围表示正常使用应用程序不需要依赖关系,仅适用于测试编译和执行阶段。它不是传递的。
  • system - 此范围与 provided 类似,除了您必须提供明确包含它的 jar。该 artifact 始终可用,并且不是在仓库中查找。
  • systemPath - 仅当依赖范围是系统时才使用。否则,如果设置此元素,构建将失败。该路径必须是绝对路径,因此建议使用 propertie 来指定特定的路径,如$ {java.home} / lib。由于假定先前安装了系统范围依赖关系,maven 将不会检查项目的仓库,而是检查库文件是否存在。如果没有,maven 将会失败,并建议您手动下载安装。
  • optional - optional 让其他项目知道,当您使用此项目时,您不需要这种依赖性才能正常工作。
  • exclusions - 包含一个或多个排除元素,每个排除元素都包含一个表示要排除的依赖关系的 groupId 和 artifactId。与可选项不同,可能或可能不会安装和使用,排除主动从依赖关系树中删除自己。

parent

maven 支持继承功能。子 POM 可以使用 parent 指定父 POM ,然后继承其配置

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  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.   <modelVersion>4.0.0</modelVersion>
  6.  
  7.   <parent>
  8.     <groupId>org.codehaus.mojo</groupId>
  9.     <artifactId>my-parent</artifactId>
  10.     <version>2.0</version>
  11.     <relativePath>../my-parent</relativePath>
  12.   </parent>
  13.  
  14.   <artifactId>my-project</artifactId>
  15. </project>
  • relativePath - 注意 relativePath 元素。在搜索本地和远程存储库之前,它不是必需的,但可以用作 maven 的指示符,以首先搜索给定该项目父级的路径。

dependencyManagement

dependencyManagement 是表示依赖 jar 包的声明。即你在项目中的 dependencyManagement 下声明了依赖,maven 不会加载该依赖,dependencyManagement 声明可以被子 POM 继承。

dependencyManagement 的一个使用案例是当有父子项目的时候,父项目中可以利用 dependencyManagement 声明子项目中需要用到的依赖 jar 包,之后,当某个或者某几个子项目需要加载该依赖的时候,就可以在子项目中 dependencies 节点只配置 groupId 和 artifactId 就可以完成依赖的引用。

dependencyManagement 主要是为了统一管理依赖包的版本,确保所有子项目使用的版本一致,类似的还有pluginspluginManagement

modules

子模块列表。

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  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.   <modelVersion>4.0.0</modelVersion>
  6.  
  7.   <groupId>org.codehaus.mojo</groupId>
  8.   <artifactId>my-parent</artifactId>
  9.   <version>2.0</version>
  10.   <packaging>pom</packaging>
  11.  
  12.   <modules>
  13.     <module>my-project</module>
  14.     <module>another-project</module>
  15.     <module>third-project/pom-example.xml</module>
  16.   </modules>
  17. </project>

properties

属性列表。定义的属性可以在 pom.xml 文件中任意处使用。使用方式为 ${propertie} 。

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  1. <project>
  2.   ...
  3.   <properties>
  4.     <maven.compiler.source>1.7<maven.compiler.source>
  5.     <maven.compiler.target>1.7<maven.compiler.target>
  6.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  7.     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  8.   </properties>
  9.   ...
  10. </project>

构建配置

build

build 可以分为 "project build" 和 "profile build"。

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  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.   <!-- "Project Build" contains more elements than just the BaseBuild set -->
  7.   <build>...</build>
  8.  
  9.   <profiles>
  10.     <profile>
  11.       <!-- "Profile Build" contains a subset of "Project Build"s elements -->
  12.       <build>...</build>
  13.     </profile>
  14.   </profiles>
  15. </project>

基本构建配置:

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  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 : 默认执行目标或阶段。如果给出了一个目标,它应该被定义为它在命令行中(如 jar:jar)。如果定义了一个阶段(如安装),也是如此。

directory :构建时的输出路径。默认为:${basedir}/target 。

finalName :这是项目的最终构建名称(不包括文件扩展名,例如:my-project-1.0.jar)

filter :定义 * .properties 文件,其中包含适用于接受其设置的资源的属性列表(如下所述)。换句话说,过滤器文件中定义的“name = value”对在代码中替换$ {name}字符串。

resources

资源的配置。资源文件通常不是代码,不需要编译,而是在项目需要捆绑使用的内容。

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  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.   <build>
  6.     ...
  7.     <resources>
  8.       <resource>
  9.         <targetPath>META-INF/plexus</targetPath>
  10.         <filtering>false</filtering>
  11.         <directory>${basedir}/src/main/plexus</directory>
  12.         <includes>
  13.           <include>configuration.xml</include>
  14.         </includes>
  15.         <excludes>
  16.           <exclude>**/*.properties</exclude>
  17.         </excludes>
  18.       </resource>
  19.     </resources>
  20.     <testResources>
  21.       ...
  22.     </testResources>
  23.     ...
  24.   </build>
  25. </project>
  • resources: 资源元素的列表,每个资源元素描述与此项目关联的文件和何处包含文件。
  • targetPath: 指定从构建中放置资源集的目录结构。目标路径默认为基本目录。将要包装在 jar 中的资源的通常指定的目标路径是 META-INF。
  • filtering: 值为 true 或 false。表示是否要为此资源启用过滤。请注意,该过滤器 * .properties 文件不必定义为进行过滤 - 资源还可以使用默认情况下在 POM 中定义的属性(例如$ {project.version}),并将其传递到命令行中“-D”标志(例如,“-Dname = value”)或由 properties 元素显式定义。过滤文件覆盖上面。
  • directory: 值定义了资源的路径。构建的默认目录是${basedir}/src/main/resources
  • includes: 一组文件匹配模式,指定目录中要包括的文件,使用*作为通配符。
  • excludes: 与 includes 类似,指定目录中要排除的文件,使用*作为通配符。注意:如果 include 和 exclude 发生冲突,maven 会以 exclude 作为有效项。
  • testResourcestestResources 与 resources 功能类似,区别仅在于:testResources 指定的资源仅用于 test 阶段,并且其默认资源目录为:${basedir}/src/test/resources 。

plugins

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  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.   <build>
  6.     ...
  7.     <plugins>
  8.       <plugin>
  9.         <groupId>org.apache.maven.plugins</groupId>
  10.         <artifactId>maven-jar-plugin</artifactId>
  11.         <version>2.6</version>
  12.         <extensions>false</extensions>
  13.         <inherited>true</inherited>
  14.         <configuration>
  15.           <classifier>test</classifier>
  16.         </configuration>
  17.         <dependencies>...</dependencies>
  18.         <executions>...</executions>
  19.       </plugin>
  20.     </plugins>
  21.   </build>
  22. </project>
  • groupIdartifactIdversion :和基本配置中的 groupIdartifactIdversion 意义相同。

  • extensions :值为 true 或 false。是否加载此插件的扩展名。默认为 false。

  • inherited :值为 true 或 false。这个插件配置是否应该适用于继承自这个插件的 POM。默认值为 true。

  • configuration - 这是针对个人插件的配置,这里不扩散讲解。

  • dependencies :这里的 dependencies 是插件本身所需要的依赖。

  • executions :需要记住的是,插件可能有多个目标。每个目标可能有一个单独的配置,甚至可能将插件的目标完全绑定到不同的阶段。执行配置插件的目标的执行。

  • id: 执行目标的标识。

  • goals: 像所有多元化的 POM 元素一样,它包含单个元素的列表。在这种情况下,这个执行块指定的插件目标列表。

  • phase: 这是执行目标列表的阶段。这是一个非常强大的选项,允许将任何目标绑定到构建生命周期中的任何阶段,从而改变 maven 的默认行为。

  • inherited: 像上面的继承元素一样,设置这个 false 会阻止 maven 将这个执行传递给它的子代。此元素仅对父 POM 有意义。

  • configuration: 与上述相同,但将配置限制在此特定目标列表中,而不是插件下的所有目标。

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  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.   <build>
  7.     <plugins>
  8.       <plugin>
  9.         <artifactId>maven-antrun-plugin</artifactId>
  10.         <version>1.1</version>
  11.         <executions>
  12.           <execution>
  13.             <id>echodir</id>
  14.             <goals>
  15.               <goal>run</goal>
  16.             </goals>
  17.             <phase>verify</phase>
  18.             <inherited>false</inherited>
  19.             <configuration>
  20.               <tasks>
  21.                 <echo>Build Dir: ${project.build.directory}</echo>
  22.               </tasks>
  23.             </configuration>
  24.           </execution>
  25.         </executions>
  26.  
  27.       </plugin>
  28.     </plugins>
  29.   </build>
  30. </project>

pluginManagement

与 dependencyManagement 很相似,在当前 POM 中仅声明插件,而不是实际引入插件。子 POM 中只配置 groupId 和 artifactId 就可以完成插件的引用,且子 POM 有权重写 pluginManagement 定义。

它的目的在于统一所有子 POM 的插件版本。

directories

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  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.   <build>
  7.     <sourceDirectory>${basedir}/src/main/java</sourceDirectory>
  8.     <scriptSourceDirectory>${basedir}/src/main/scripts</scriptSourceDirectory>
  9.     <testSourceDirectory>${basedir}/src/test/java</testSourceDirectory>
  10.     <outputDirectory>${basedir}/target/classes</outputDirectory>
  11.     <testOutputDirectory>${basedir}/target/test-classes</testOutputDirectory>
  12.     ...
  13.   </build>
  14. </project>

目录元素集合存在于 build 元素中,它为整个 POM 设置了各种目录结构。由于它们在配置文件构建中不存在,所以这些不能由配置文件更改。

如果上述目录元素的值设置为绝对路径(扩展属性时),则使用该目录。否则,它是相对于基础构建目录:${basedir}

extensions

扩展是在此构建中使用的 artifacts 的列表。它们将被包含在运行构建的 classpath 中。它们可以启用对构建过程的扩展(例如为 Wagon 传输机制添加一个 ftp 提供程序),并使活动的插件能够对构建生命周期进行更改。简而言之,扩展是在构建期间激活的 artifacts。扩展不需要实际执行任何操作,也不包含 Mojo。因此,扩展对于指定普通插件接口的多个实现中的一个是非常好的。

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  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.   <build>
  7.     ...
  8.     <extensions>
  9.       <extension>
  10.         <groupId>org.apache.maven.wagon</groupId>
  11.         <artifactId>wagon-ftp</artifactId>
  12.         <version>1.0-alpha-3</version>
  13.       </extension>
  14.     </extensions>
  15.     ...
  16.   </build>
  17. </project>

reporting

报告包含特定针对 site 生成阶段的元素。某些 maven 插件可以生成 reporting 元素下配置的报告,例如:生成 javadoc 报告。reporting 与 build 元素配置插件的能力相似。明显的区别在于:在执行块中插件目标的控制不是细粒度的,报表通过配置 reportSet 元素来精细控制。

而微妙的区别在于 reporting 元素下的 configuration 元素可以用作 build 下的 configuration ,尽管相反的情况并非如此( build 下的 configuration 不影响 reporting 元素下的 configuration )。

另一个区别就是 plugin 下的 outputDirectory 元素。在报告的情况下,默认输出目录为 ${basedir}/target/site

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  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.   <reporting>
  7.     <plugins>
  8.       <plugin>
  9.         ...
  10.         <reportSets>
  11.           <reportSet>
  12.             <id>sunlink</id>
  13.             <reports>
  14.               <report>javadoc</report>
  15.             </reports>
  16.             <inherited>true</inherited>
  17.             <configuration>
  18.               <links>
  19.                 <link>http://java.sun.com/j2se/1.5.0/docs/api/</link>
  20.               </links>
  21.             </configuration>
  22.           </reportSet>
  23.         </reportSets>
  24.       </plugin>
  25.     </plugins>
  26.   </reporting>
  27.   ...
  28. </project>

项目信息

项目信息相关的这部分标签都不是必要的,也就是说完全可以不填写。

它的作用仅限于描述项目的详细信息。

下面的示例是项目信息相关标签的清单:

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  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.  
  7.   <!-- 项目信息 begin -->
  8.  
  9.   <!--项目名-->
  10.   <name>maven-notes</name>
  11.  
  12.   <!--项目描述-->
  13.   <description>maven 学习笔记</description>
  14.  
  15.   <!--项目url-->
  16.   <url>https://github.com/dunwu/maven-notes</url>
  17.  
  18.   <!--项目开发年份-->
  19.   <inceptionYear>2017</inceptionYear>
  20.  
  21.   <!--开源协议-->
  22.   <licenses>
  23.     <license>
  24.       <name>Apache License, Version 2.0</name>
  25.       <url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
  26.       <distribution>repo</distribution>
  27.       <comments>A business-friendly OSS license</comments>
  28.     </license>
  29.   </licenses>
  30.  
  31.   <!--组织信息(如公司、开源组织等)-->
  32.   <organization>
  33.     <name>...</name>
  34.     <url>...</url>
  35.   </organization>
  36.  
  37.   <!--开发者列表-->
  38.   <developers>
  39.     <developer>
  40.       <id>victor</id>
  41.       <name>Zhang Peng</name>
  42.       <email>forbreak at 163.com</email>
  43.       <url>https://github.com/dunwu</url>
  44.       <organization>...</organization>
  45.       <organizationUrl>...</organizationUrl>
  46.       <roles>
  47.         <role>architect</role>
  48.         <role>developer</role>
  49.       </roles>
  50.       <timezone>+8</timezone>
  51.       <properties>...</properties>
  52.     </developer>
  53.   </developers>
  54.  
  55.   <!--代码贡献者列表-->
  56.    <contributors>
  57.     <contributor>
  58.       <!--标签内容和<developer>相同-->
  59.     </contributor>
  60.   </contributors>
  61.  
  62.   <!-- 项目信息 end -->
  63.  
  64.   ...
  65. </project>

这部分标签都非常简单,基本都能做到顾名思义,且都属于可有可无的标签,所以这里仅简单介绍一下:

  • name - 项目完整名称

  • description - 项目描述

  • url - 一般为项目仓库的 host

  • inceptionYear - 开发年份

  • licenses - 开源协议

  • organization - 项目所属组织信息

  • developers - 项目开发者列表

  • contributors - 项目贡献者列表, 的子标签和 的完全相同。

环境配置

issueManagement

这定义了所使用的缺陷跟踪系统(Bugzilla,TestTrack,ClearQuest 等)。虽然没有什么可以阻止插件使用这些信息的东西,但它主要用于生成项目文档。

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  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.   <issueManagement>
  7.     <system>Bugzilla</system>
  8.     <url>http://127.0.0.1/bugzilla/</url>
  9.   </issueManagement>
  10.   ...
  11. </project>

ciManagement

CI 构建系统配置,主要是指定通知机制以及被通知的邮箱。

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  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.   <ciManagement>
  7.     <system>continuum</system>
  8.     <url>http://127.0.0.1:8080/continuum</url>
  9.     <notifiers>
  10.       <notifier>
  11.         <type>mail</type>
  12.         <sendOnError>true</sendOnError>
  13.         <sendOnFailure>true</sendOnFailure>
  14.         <sendOnSuccess>false</sendOnSuccess>
  15.         <sendOnWarning>false</sendOnWarning>
  16.         <configuration><address>continuum@127.0.0.1</address></configuration>
  17.       </notifier>
  18.     </notifiers>
  19.   </ciManagement>
  20.   ...
  21. </project>

mailingLists

邮件列表

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  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.   <mailingLists>
  7.     <mailingList>
  8.       <name>User List</name>
  9.       <subscribe>user-subscribe@127.0.0.1</subscribe>
  10.       <unsubscribe>user-unsubscribe@127.0.0.1</unsubscribe>
  11.       <post>user@127.0.0.1</post>
  12.       <archive>http://127.0.0.1/user/</archive>
  13.       <otherArchives>
  14.         <otherArchive>http://base.google.com/base/1/127.0.0.1</otherArchive>
  15.       </otherArchives>
  16.     </mailingList>
  17.   </mailingLists>
  18.   ...
  19. </project>

scm

SCM(软件配置管理,也称为源代码/控制管理或简洁的版本控制)。常见的 scm 有 svn 和 git 。

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  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.   <scm>
  7.     <connection>scm:svn:http://127.0.0.1/svn/my-project</connection>
  8.     <developerConnection>scm:svn:https://127.0.0.1/svn/my-project</developerConnection>
  9.     <tag>HEAD</tag>
  10.     <url>http://127.0.0.1/websvn/my-project</url>
  11.   </scm>
  12.   ...
  13. </project>

prerequisites

POM 执行的预设条件。

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  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.   <prerequisites>
  7.     <maven>2.0.6</maven>
  8.   </prerequisites>
  9.   ...
  10. </project>

repositories

repositories 是遵循 Maven 存储库目录布局的 artifacts 集合。默认的 Maven 中央存储库位于https://repo.maven.apache.org/maven2/上。

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  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.   <repositories>
  7.     <repository>
  8.       <releases>
  9.         <enabled>false</enabled>
  10.         <updatePolicy>always</updatePolicy>
  11.         <checksumPolicy>warn</checksumPolicy>
  12.       </releases>
  13.       <snapshots>
  14.         <enabled>true</enabled>
  15.         <updatePolicy>never</updatePolicy>
  16.         <checksumPolicy>fail</checksumPolicy>
  17.       </snapshots>
  18.       <id>codehausSnapshots</id>
  19.       <name>Codehaus Snapshots</name>
  20.       <url>http://snapshots.maven.codehaus.org/maven2</url>
  21.       <layout>default</layout>
  22.     </repository>
  23.   </repositories>
  24.   <pluginRepositories>
  25.     ...
  26.   </pluginRepositories>
  27.   ...
  28. </project>

pluginRepositories

与 repositories 差不多。

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2.   xmlns:xsi="http://www.w3.org/001/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.   <distributionManagement>
  7.     ...
  8.     <downloadUrl>http://mojo.codehaus.org/my-project</downloadUrl>
  9.     <status>deployed</status>
  10.   </distributionManagement>
  11.   ...
  12. </project>

distributionManagement

它管理在整个构建过程中生成的 artifact 和支持文件的分布。从最后的元素开始:

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2.   xmlns:xsi="http://www.w3.org/001/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.   <distributionManagement>
  7.     ...
  8.     <downloadUrl>http://mojo.codehaus.org/my-project</downloadUrl>
  9.     <status>deployed</status>
  10.   </distributionManagement>
  11.   ...
  12. </project>
  • repository - 与 repositories 相似

  • site - 站点信息

  • relocation - 项目迁移位置

profiles

activation 是一个 profile 的关键。配置文件的功能来自于在某些情况下仅修改基本 POM 的功能。这些情况通过 activation 元素指定。

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  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.   <profiles>
  7.     <profile>
  8.       <id>test</id>
  9.       <activation>
  10.         <activeByDefault>false</activeByDefault>
  11.         <jdk>1.5</jdk>
  12.         <os>
  13.           <name>Windows XP</name>
  14.           <family>Windows</family>
  15.           <arch>x86</arch>
  16.           <version>5.1.2600</version>
  17.         </os>
  18.         <property>
  19.           <name>sparrow-type</name>
  20.           <value>African</value>
  21.         </property>
  22.         <file>
  23.           <exists>${basedir}/file2.properties</exists>
  24.           <missing>${basedir}/file1.properties</missing>
  25.         </file>
  26.       </activation>
  27.       ...
  28.     </profile>
  29.   </profiles>
  30. </project>

参考资料

https://maven.apache.org/pom.html

Maven 教程之 pom.xml 详解的更多相关文章

  1. 【转】maven核心,pom.xml详解

    感谢如下博主: http://www.cnblogs.com/qq78292959/p/3711501.html maven核心,pom.xml详解 什么是pom?    pom作为项目对象模型.通过 ...

  2. maven核心,pom.xml详解(转)

    什么是pom?    pom作为项目对象模型.通过xml表示maven项目,使用pom.xml来实现.主要描述了项目:包括配置文件:开发者需要遵循的规则,缺陷管理系统,组织和licenses,项目的u ...

  3. maven的标准pom.xml详解

    maven是构建和管理理项目的利器,pom.xml 是其核心.一个标准的pom.xml该怎么写?其中的标签又有什么意义?话不多说,请看代码: <?xml version="1.0&qu ...

  4. maven核心,pom.xml详解

    什么是pom?    pom作为项目对象模型.通过xml表示maven项目,使用pom.xml来实现.主要描述了项目:包括配置文件:开发者需要遵循的规则,缺陷管理系统,组织和licenses,项目的u ...

  5. Maven实战:Pom.xml详解

    什么是pom?    pom作为项目对象模型.通过xml表示maven项目,使用pom.xml来实现.主要描述了项目:包括配置文件:开发者需要遵循的规则,缺陷管理系统,组织和licenses,项目的u ...

  6. Maven学习总结(15)——Maven 项目中pom.xml详解

    <project xmlns="http://maven.apache.org/POM/4.0.0"  xmlns:xsi="http://www.w3.org/2 ...

  7. Maven中的pom.xml详解

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  8. 【maven】 pom.xml详解

    pom.xml详解 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www ...

  9. Maven全局配置文件settings.xml详解(转)

    Maven全局配置文件settings.xml详解   目录 一.概要 1.settings.xml的作用2.settings.xml文件位置3.配置的优先级 二.settings.xml元素详解 1 ...

随机推荐

  1. Sockit 硬件接口编程——点亮一个LED

    1.话不多说上代码 #include <stdio.h> #include <stdlib.h> #include <string.h> #include < ...

  2. 在MSSQL中的简单数据类型递归

    在某些特定的项目需求中,我们需要实现树状数据结构, 由此,我们需要用递归将数据查询出来. WITH T AS ( SELECT ID,PID FROM TableName WHERE ID=1 UNI ...

  3. 建议3:正确处理Javascript特殊值---(1)正确使用NaN和Infinity

    NaN时IEEE 754中定义的一个特殊的数量值.他不表示一个数字,尽管下面的表达式返回的是true typeof(NaN) === 'number' //true 该值可能会在试图将非数字形式的字符 ...

  4. 《Java算法》贪心算法

    贪心算法(又称贪婪算法)是指,在对问题求解时,总是做出在当前看来是最好的选择.也就是说,不从整体最优上加以考虑,他所做出的是在某种意义上的局部最优解. 贪心算法的经典案例: 跳跃游戏: 给定一个非负整 ...

  5. DataSet、DataTable、DataView三者关系及DataView 常见用法

    DATASET   可以理解为是个数据库. DATATABLE  可以理解为是个数据表. DATAVIEW   可以理解为是表的视图. dataset 数据集合可以包含多个datatable,而dat ...

  6. JVM 学习笔记二 :JVM内存区域

    一.内存分配概述

  7. UWP 更强大的文件获取能力

    默认情况下,通用 Windows 平台 (UWP) 应用可以访问特定文件系统位置. 应用也可以通过文件选取器或通过声明功能访问其他位置. 在创建新的应用时,默认情况下你可以访问以下文件系统位置: 1. ...

  8. c++-多态和vptr指针

    多态的原理 #define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; class Parent { ...

  9. injected stylesheet 谷歌扩展插件,造成样式异常

    今天在开发的时候,遇到一个问题,就是我们我在写发送广告的功能,然后我用了一个textare文本框,这个时候,发现了一个问题.这个文本框凭空消失了.不见了,我以为是自己的那个样式不小心把这个隐藏掉了后来 ...

  10. Android 列表对话框 setItems

    private Button button; private final CharSequence[] items = { "北京", "上海", " ...