maven的超级pom

对于 Maven3,超级 POM 在文件 %MAVEN_HOME%/lib/maven-model-builder-x.x.x.jar 中的 org/apache/maven/model/pom-4.0.0.xml 路径下、

对于 Maven2,超级 POM 在文件 %MAVEN_HOME%/lib/maven-x.x.x-uber.jar 中的 org/apache/maven/project/pom-4.0.0.xml 目录下。

这里的 x.x.x 表示 Maven 的具体版本。

对于使用java的人而言,继承这个词大家应该都不陌生。要继承pom就需要有一个父pom,在Maven中定义了超级pom.xml,任何没有申明自己父pom.xml的pom.xml都将默认继承自这个超级pom.xml。

       先来看一下这个超级pom.xml的定义:

Xml代码  收藏代码
  1. <project>  
  2.   <modelVersion>4.0.0</modelVersion>  
  3.   <name>Maven Default Project</name>  
  4.    
  5.   <repositories>  
  6.     <repository>  
  7.       <id>central</id>  
  8.       <name>Maven Repository Switchboard</name>  
  9.       <layout>default</layout>  
  10.       <url>http://repo1.maven.org/maven2</url>  
  11.       <snapshots>  
  12.         <enabled>false</enabled>  
  13.       </snapshots>  
  14.     </repository>  
  15.   </repositories>  
  16.    
  17.   <pluginRepositories>  
  18.     <pluginRepository>  
  19.       <id>central</id>  
  20.       <name>Maven Plugin Repository</name>  
  21.       <url>http://repo1.maven.org/maven2</url>  
  22.       <layout>default</layout>  
  23.       <snapshots>  
  24.         <enabled>false</enabled>  
  25.       </snapshots>  
  26.       <releases>  
  27.         <updatePolicy>never</updatePolicy>  
  28.       </releases>  
  29.     </pluginRepository>  
  30.   </pluginRepositories>  
  31.    
  32.   <build>  
  33.     <directory>${project.basedir}/target</directory>  
  34.     <outputDirectory>${project.build.directory}/classes</outputDirectory>  
  35.     <finalName>${project.artifactId}-${project.version}</finalName>  
  36.     <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>  
  37.     <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>  
  38.     <!-- TODO: MNG-3731 maven-plugin-tools-api < 2.4.4 expect this to be relative... -->  
  39.     <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>  
  40.     <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>  
  41.     <resources>  
  42.       <resource>  
  43.         <directory>${project.basedir}/src/main/resources</directory>  
  44.       </resource>  
  45.     </resources>  
  46.     <testResources>  
  47.       <testResource>  
  48.         <directory>${project.basedir}/src/test/resources</directory>  
  49.       </testResource>  
  50.     </testResources>  
  51.    <pluginManagement>  
  52.        <plugins>  
  53.          <plugin>  
  54.            <artifactId>maven-antrun-plugin</artifactId>  
  55.            <version>1.3</version>  
  56.          </plugin>        
  57.          <plugin>  
  58.            <artifactId>maven-assembly-plugin</artifactId>  
  59.            <version>2.2-beta-2</version>  
  60.          </plugin>          
  61.          <plugin>  
  62.            <artifactId>maven-clean-plugin</artifactId>  
  63.            <version>2.2</version>  
  64.          </plugin>  
  65.          <plugin>  
  66.            <artifactId>maven-compiler-plugin</artifactId>  
  67.            <version>2.0.2</version>  
  68.          </plugin>  
  69.          <plugin>  
  70.            <artifactId>maven-dependency-plugin</artifactId>  
  71.            <version>2.0</version>  
  72.          </plugin>  
  73.          <plugin>  
  74.            <artifactId>maven-deploy-plugin</artifactId>  
  75.            <version>2.4</version>  
  76.          </plugin>  
  77.          <plugin>  
  78.            <artifactId>maven-ear-plugin</artifactId>  
  79.            <version>2.3.1</version>  
  80.          </plugin>  
  81.          <plugin>  
  82.            <artifactId>maven-ejb-plugin</artifactId>  
  83.            <version>2.1</version>  
  84.          </plugin>  
  85.          <plugin>  
  86.            <artifactId>maven-install-plugin</artifactId>  
  87.            <version>2.2</version>  
  88.          </plugin>  
  89.          <plugin>  
  90.            <artifactId>maven-jar-plugin</artifactId>  
  91.            <version>2.2</version>  
  92.          </plugin>  
  93.          <plugin>  
  94.            <artifactId>maven-javadoc-plugin</artifactId>  
  95.            <version>2.5</version>  
  96.          </plugin>  
  97.          <plugin>  
  98.            <artifactId>maven-plugin-plugin</artifactId>  
  99.            <version>2.4.3</version>  
  100.          </plugin>  
  101.          <plugin>  
  102.            <artifactId>maven-rar-plugin</artifactId>  
  103.            <version>2.2</version>  
  104.          </plugin>         
  105.          <plugin>                 
  106.            <artifactId>maven-release-plugin</artifactId>  
  107.            <version>2.0-beta-8</version>  
  108.          </plugin>  
  109.          <plugin>                  
  110.            <artifactId>maven-resources-plugin</artifactId>  
  111.            <version>2.3</version>  
  112.          </plugin>  
  113.          <plugin>  
  114.            <artifactId>maven-site-plugin</artifactId>  
  115.            <version>2.0-beta-7</version>  
  116.          </plugin>  
  117.          <plugin>  
  118.            <artifactId>maven-source-plugin</artifactId>  
  119.            <version>2.0.4</version>  
  120.          </plugin>          
  121.          <plugin>  
  122.             <artifactId>maven-surefire-plugin</artifactId>  
  123.             <version>2.4.3</version>  
  124.          </plugin>  
  125.          <plugin>  
  126.            <artifactId>maven-war-plugin</artifactId>  
  127.            <version>2.1-alpha-2</version>  
  128.          </plugin>  
  129.        </plugins>  
  130.      </pluginManagement>  
  131.   </build>  
  132.    
  133.   <reporting>  
  134.     <outputDirectory>${project.build.directory}/site</outputDirectory>  
  135.   </reporting>  
  136.   <profiles>  
  137.     <profile>  
  138.       <id>release-profile</id>  
  139.    
  140.       <activation>  
  141.         <property>  
  142.           <name>performRelease</name>  
  143.           <value>true</value>  
  144.         </property>  
  145.       </activation>  
  146.    
  147.       <build>  
  148.         <plugins>  
  149.           <plugin>  
  150.             <inherited>true</inherited>  
  151.             <groupId>org.apache.maven.plugins</groupId>  
  152.             <artifactId>maven-source-plugin</artifactId>  
  153.             <executions>  
  154.               <execution>  
  155.                 <id>attach-sources</id>  
  156.                 <goals>  
  157.                   <goal>jar</goal>  
  158.                 </goals>  
  159.               </execution>  
  160.             </executions>  
  161.           </plugin>  
  162.           <plugin>  
  163.             <inherited>true</inherited>  
  164.             <groupId>org.apache.maven.plugins</groupId>  
  165.             <artifactId>maven-javadoc-plugin</artifactId>  
  166.             <executions>  
  167.               <execution>  
  168.                 <id>attach-javadocs</id>  
  169.                 <goals>  
  170.                   <goal>jar</goal>  
  171.                 </goals>  
  172.               </execution>  
  173.             </executions>  
  174.           </plugin>  
  175.           <plugin>  
  176.             <inherited>true</inherited>  
  177.             <groupId>org.apache.maven.plugins</groupId>  
  178.             <artifactId>maven-deploy-plugin</artifactId>  
  179.             <configuration>  
  180.               <updateReleaseInfo>true</updateReleaseInfo>  
  181.             </configuration>  
  182.           </plugin>  
  183.         </plugins>  
  184.       </build>  
  185.     </profile>  
  186.   </profiles>  
  187.    
  188. </project>  

       对于一个pom.xml来说有几个元素是必须定义的,一个是project根元素,然后就是它里面的modelVersion、groupId、artifactId和version。由上面的超级pom.xml的内容我们可以看到pom.xml中没有groupId、artifactId和version的定义,所以我们在建立自己的pom.xml的时候就需要定义这三个元素。和java里面的继承类似,子pom.xml会完全继承父pom.xml中所有的元素,而且对于相同的元素,一般子pom.xml中的会覆盖父pom.xml中的元素,但是有几个特殊的元素它们会进行合并而不是覆盖。这些特殊的元素是:

Ø  dependencies

Ø  developers

Ø  contributors

Ø  plugin列表,包括plugin下面的reports

Ø  resources

参考:

1、http://blog.csdn.net/tounaobun/article/details/8958125

2、http://blog.csdn.net/haojiahj/article/details/49964919

原文地址:https://www.cnblogs.com/shengulong/p/8295453.html