什么是 POM?
POM (Project Object Model) 项目对象模型。它是一个XML文件,其中包含有关Maven用于构建项目的项目和配置细节的信息。它包含大多数项目的默认值。例如,构建项目的目录:target;java源码文件目录: src/main/java;测试java源码文件目录: src/test/java;等等。当执行任务或目标时,Maven将在当前目录中查找POM,它读取POM。获取所需要的配置信息,然后执行目标。
POM中可以指定项目的依赖,可以执行的插件或目标,构建配置文件等。其他信息,如项目版本,说明,开发人员,邮件列表等也可以指定。
POM 结构
这是直接在POM的项目元素下列出的元素。注意modelVersion为4.0.0。这是目前唯一支持Maven 2和3的POM版本,并且是必需的。
 <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>
Maven 坐标
groupId:artifactId:version三个字段是Maven必不可少的字段,这三个字段的作用非常类似与一个地址和时间戳。这标志着存储库中特定位置,就像Maven项目的坐标系统一样。
- groupId: 这是组织或则项目独一无二的ID值。
 - artifactId: 是项目的名称,把groupId进行却分开来。
 - version: 指项目的当前版本。
 - packaging: 如果没有指定默认是jar,我们可以设置为pom, jar, maven-plugin, ejb, war, ear, rar, par等,如果设置为war则会将项目最终打包成war包。
 - classifier: 你有时候会在项目中看到这个熟悉,这样项目显示为groupId:artifactId:packaging:classifier:version。
 
Maven 依赖
Maven的基石就是它的依赖列表,大多数项目都是依赖其它构建才能正常运行。如下是一个简单依赖。
 <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>
- groupId、artifactId、version: 如上一节所述,这里可以计算出项目的坐标,并将其引入到项目依赖中。
 - classifier: 这是一个可选参数。
 - type: 依赖项目的包装类型,默认为jar。
 - scope: 该元素用来引入到当前项目的类路径(编译、测试或运行等),以及限制依赖的传递性。总共有如下五个范围:
- compile:默认值,编译期。依赖项可以在所有类路径中使用,且会传递到子项目。
 - provided:只可以在编译和测试中使用,切不会传递给子项目。
 - runtiome:在项目测试和运行类路径中使用,不会在编译中使用。
 - test:只可以在测试中使用,不会传递子项目。
 - system:这个和provided非常类似,工件始终可以使用,并且不会存储库中查找。
 
 - systemPath:只有当scope为system时,需要指定JAR的绝对路径。
 - optional:可选元素,如果optional为true时,如果A依赖B,B依赖D,如果A没有显示的引入D,则A不会依赖D。
 
exclusions可以显示的排出依赖项中传递的依赖项目。例如maven-embedder需要依赖maven-core,但我们不希望引入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>
这样你的项目中不会有maven-core依赖项。
Super POM
Super POM 是Maven默认的POM,除非明确设置,否则所有的POM都会扩展Super POM,这意味着您为项目创建的POM继承Super POM。下面的代码是Maven 2.0.x 的 Super POM。
 
 <project>
   <modelVersion>4.0.0</modelVersion>
   <name>Maven Default Project</name>
   <repositories>
     <repository>
       <id>central</id>
       <name>Maven Repository Switchboard</name>
       <layout>default</layout>
       <url>http://repo1.maven.org/maven2</url>
       <snapshots>
         <enabled>false</enabled>
       </snapshots>
     </repository>
   </repositories>
   <pluginRepositories>
     <pluginRepository>
       <id>central</id>
       <name>Maven Plugin Repository</name>
       <url>http://repo1.maven.org/maven2</url>
       <layout>default</layout>
       <snapshots>
         <enabled>false</enabled>
       </snapshots>
       <releases>
         <updatePolicy>never</updatePolicy>
       </releases>
     </pluginRepository>
   </pluginRepositories>
   <build>
     <directory>target</directory>
     <outputDirectory>target/classes</outputDirectory>
     <finalName>${artifactId}-${version}</finalName>
     <testOutputDirectory>target/test-classes</testOutputDirectory>
     <sourceDirectory>src/main/java</sourceDirectory>
     <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>
     <testSourceDirectory>src/test/java</testSourceDirectory>
     <resources>
       <resource>
         <directory>src/main/resources</directory>
       </resource>
     </resources>
     <testResources>
       <testResource>
         <directory>src/test/resources</directory>
       </testResource>
     </testResources>
   </build>
   <reporting>
     <outputDirectory>target/site</outputDirectory>
   </reporting>
   <profiles>
     <profile>
       <id>release-profile</id>
       <activation>
         <property>
           <name>performRelease</name>
         </property>
       </activation>
       <build>
         <plugins>
           <plugin>
             <inherited>true</inherited>
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-source-plugin</artifactId>
             <executions>
               <execution>
                 <id>attach-sources</id>
                 <goals>
                   <goal>jar</goal>
                 </goals>
               </execution>
             </executions>
           </plugin>
           <plugin>
             <inherited>true</inherited>
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-javadoc-plugin</artifactId>
             <executions>
               <execution>
                 <id>attach-javadocs</id>
                 <goals>
                   <goal>jar</goal>
                 </goals>
               </execution>
             </executions>
           </plugin>
           <plugin>
             <inherited>true</inherited>
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-deploy-plugin</artifactId>
             <configuration>
               <updateReleaseInfo>true</updateReleaseInfo>
             </configuration>
           </plugin>
         </plugins>
       </build>
     </profile>
   </profiles>
 </project>
View Code
下面是Maven 2.1.x 的Super POM
 
 <project>
   <modelVersion>4.0.0</modelVersion>
   <name>Maven Default Project</name>
   <repositories>
     <repository>
       <id>central</id>
       <name>Maven Repository Switchboard</name>
       <layout>default</layout>
       <url>http://repo1.maven.org/maven2</url>
       <snapshots>
         <enabled>false</enabled>
       </snapshots>
     </repository>
   </repositories>
   <pluginRepositories>
     <pluginRepository>
       <id>central</id>
       <name>Maven Plugin Repository</name>
       <url>http://repo1.maven.org/maven2</url>
       <layout>default</layout>
       <snapshots>
         <enabled>false</enabled>
       </snapshots>
       <releases>
         <updatePolicy>never</updatePolicy>
       </releases>
     </pluginRepository>
   </pluginRepositories>
   <build>
     <directory>${project.basedir}/target</directory>
     <outputDirectory>${project.build.directory}/classes</outputDirectory>
     <finalName>${project.artifactId}-${project.version}</finalName>
     <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
     <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
     <!-- TODO: MNG-3731 maven-plugin-tools-api < 2.4.4 expect this to be relative... -->
     <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>
     <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
     <resources>
       <resource>
         <directory>${project.basedir}/src/main/resources</directory>
       </resource>
     </resources>
     <testResources>
       <testResource>
         <directory>${project.basedir}/src/test/resources</directory>
       </testResource>
     </testResources>
    <pluginManagement>
        <plugins>
          <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.3</version>
          </plugin>       
          <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.2-beta-2</version>
          </plugin>         
          <plugin>
            <artifactId>maven-clean-plugin</artifactId>
            <version>2.2</version>
          </plugin>
          <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.0.2</version>
          </plugin>
          <plugin>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.0</version>
          </plugin>
          <plugin>
            <artifactId>maven-deploy-plugin</artifactId>
            <version>2.4</version>
          </plugin>
          <plugin>
            <artifactId>maven-ear-plugin</artifactId>
            <version>2.3.1</version>
          </plugin>
          <plugin>
            <artifactId>maven-ejb-plugin</artifactId>
            <version>2.1</version>
          </plugin>
          <plugin>
            <artifactId>maven-install-plugin</artifactId>
            <version>2.2</version>
          </plugin>
          <plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.2</version>
          </plugin>
          <plugin>
            <artifactId>maven-javadoc-plugin</artifactId>
            <version>2.5</version>
          </plugin>
          <plugin>
            <artifactId>maven-plugin-plugin</artifactId>
            <version>2.4.3</version>
          </plugin>
          <plugin>
            <artifactId>maven-rar-plugin</artifactId>
            <version>2.2</version>
          </plugin>        
          <plugin>                
            <artifactId>maven-release-plugin</artifactId>
            <version>2.0-beta-8</version>
          </plugin>
          <plugin>                
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.3</version>
          </plugin>
          <plugin>
            <artifactId>maven-site-plugin</artifactId>
            <version>2.0-beta-7</version>
          </plugin>
          <plugin>
            <artifactId>maven-source-plugin</artifactId>
            <version>2.0.4</version>
          </plugin>         
          <plugin>
             <artifactId>maven-surefire-plugin</artifactId>
             <version>2.4.3</version>
          </plugin>
          <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1-alpha-2</version>
          </plugin>
        </plugins>
      </pluginManagement>
   </build>
   <reporting>
     <outputDirectory>${project.build.directory}/site</outputDirectory>
   </reporting>
   <profiles>
     <profile>
       <id>release-profile</id>
       <activation>
         <property>
           <name>performRelease</name>
           <value>true</value>
         </property>
       </activation>
       <build>
         <plugins>
           <plugin>
             <inherited>true</inherited>
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-source-plugin</artifactId>
             <executions>
               <execution>
                 <id>attach-sources</id>
                 <goals>
                   <goal>jar</goal>
                 </goals>
               </execution>
             </executions>
           </plugin>
           <plugin>
             <inherited>true</inherited>
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-javadoc-plugin</artifactId>
             <executions>
               <execution>
                 <id>attach-javadocs</id>
                 <goals>
                   <goal>jar</goal>
                 </goals>
               </execution>
             </executions>
           </plugin>
           <plugin>
             <inherited>true</inherited>
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-deploy-plugin</artifactId>
             <configuration>
               <updateReleaseInfo>true</updateReleaseInfo>
             </configuration>
           </plugin>
         </plugins>
       </build>
     </profile>
   </profiles>
 </project>
View Code
最小的 POM
POM最低要求如下:
- project 根节点
 - modelVersion 需要设置为4.0.0
 - groupId 项目组的标识(建议使用网站的倒序)
 - artifactId 项目名称
 - version 项目的版本信息,这里提一下(SNAPSHOT 和 RELEASE),在项目中常常看到这两个单词。SNAPSHOT 代表不稳定版,常常属于开发中的项目,而RELEASE稳定版。
 
下面是已个最小POM的简单例子:
 <project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.cnblogs.hello</groupId>
    <artifactId>hello</artifactId>
    <version>1.0.SNAPSHOT</version>
 </project>
POM要求配置groupId,artifactId 和 version,这三个值构成了项目完全限定的项目名称,就是
另外,如果配置文件未指定详细信息,Maven将使用默认值。每个Maven项目都有一个packaging类型。如果没有在POM中指定,那么就会使用默认值“jar”。
此外,正如你看到的,在最小POM中,repositories存储库也没有被指定,如果使用最小POM构建项目,它将继承Super POM中的repositories配置的存储库。
继承
项目继承会合并下列元素:
- dependencies : 依赖
 - developers 和 contributors : 开发者和贡献者
 - plugin lists (including reports) : 插件列表(包括报表)
 - plugin executions with matching ids : 匹配ids的执行插件
 - plugin configuration : 插件配置
 - resources : 资源
 
Super POM是项目继承的一个例子,但是您也可以通过在POM中指定父元素来引入您自己的父POM,如以下示例所示。
 <project>
   <parent>
     <groupId>com.cnblogs.hello</groupId>
     <artifactId>hello</artifactId>
     <version>1.0.SNAPSHOT</version>
   </parent>
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.cnbligs.hello</groupId>
   <artifactId>app</artifactId>
   <version>1.0.SNAPSHOT</version>
 </project>
上面示例是将最小POM指定为app的父POM。指定父POM我需要使用完全限定名指定(即需要使用groupId,artifactID 和 version),通过这个设置,我们的模块现在可以继承父POM的一些属性。
或者,如果我们希望groupId 和 version 和父POM的保持一致,则可以将在POM删除groupId和version,如下所示:
 <project>
   <parent>
     <groupId>com.cnblogs.hello</groupId>
     <artifactId>hello</artifactId>
     <version>1.0.SNAPSHOT</version>
   </parent>
   <modelVersion>4.0.0</modelVersion>
   <artifactId>app</artifactId>
 </project>
这里注意下parent中的relativePath,如果父POM不是在该POM的上一级目录,就必须配置这个属性并定位到父POM中。relativePath的默认值为../pom.xml。
聚合
项目聚合跟项目继承有些类似,但不是从POM中指定父POM,而是从父POM中指定POM。通过这样做,父项目现在知道它所有的子项目,并且对父项目执行Maven命令,那么Maven命令也会执行到所有的子项目中。要做一个项目聚合,必须执行以下操作:
- 改变父POM的packaging属性为pom。
 - 在父POM中指定子模块(子项目)。
 
将如上两个POM做如下修改。
com.cnblogs.hello:app:1.0.SNAPSHOT
 <project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.cnbligs.hello</groupId>
    <artifactId>app</artifactId>
    <version>1.0.SNAPSHOT</version>
 </project>
com.cnblogs.hello:hello:1.0.SNAPSHOT
 <project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.cnblogs.hello</groupId>
    <artifactId>hello</artifactId>
    <version>1.0.SNAPSHOT</version>
    <!-- 指定为父POM,project 聚合的第一步 -->
    <packaging>pom</packaging>
    <!-- 添加子模块 根据POM的相对路径进行指定。 -->
    <modules>
        <module>../app</module>
    </modules>
 </project>
现在,每当Maven命令处理hello项目时,通过的Maven命令也会针对app项目运行。
继承 和 聚合
如果你有多个Maven项目,并且他们都具有相似的配置,则可以通过抽出相似的配置并制作父项目来构建。因此,您所要做的就是让你的Maven项目继承该父项目,然后将这些配置应用与所有的这些项目。
如果你有一组构建或一起处理的项目。你可以创建一个父项目并让该父项目将这些子项目声明进来。通过这样做,你只需要构建父项目,其余的也随之而来。
当然,你也可以同时拥有继承和聚合。意思是说,你可以让你的模块指定一个父项目,同时让这个父项目指定这些Maven作为它的子模块。你只需要符合以下三条规则:
- 在每一个子POM中指定他们的父POM是谁。
 - 在父POM中将packaging的值改为pom。
 - 在父POM中指定其所有的子项目模块(子POM)。
 
参考一下代码示例:
com.cnblogs.hello:app:1.0.SNAPSHOT
 <project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.cnblogs.hello</groupId>
    <artifactId>hello</artifactId>
    <version>1.0.SNAPSHOT</version>
    <packaging>pom</packaging>
    <modules>
        <module>../app</module>
    </modules>
 </project>
com.cnblogs.hello:app:1.0.SNAPSHOT
 <project>
    <parent>
      <groupId>com.cnblogs.hello</groupId>
      <artifactId>hello</artifactId>
      <version>1.0.SNAPSHOT</version>
      <relativePath>../pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.cnbligs.hello</groupId>
    <artifactId>app</artifactId>
    <version>1.0.SNAPSHOT</version>
 </project>
变量
在某些情况下,您需要在多个不同位置使用相同的值。为了帮助确保值仅指定一次,Maven允许您在POM中使用自己的和预定义的变量。例如如下示例:
<!-- 首先在properties标签中自定义 project.version 属性 值为 1.0.SANPSHOT  在后续的代码中可以直接使用${project.version}获取值 -->
<properties>
    <project.version>1.0.SNAPSHOT</project.version>
</properties>
<!-- version可以这样写 -->
<version>${project.version}</version>
其实properties标签不仅仅是只能声明版本号,凡是需要统一声明后再引用的场合都可以使用。