maven pom.xml文件 简单解析

来源:互联网 发布:怎么对比两列数据图表 编辑:程序博客网 时间:2024/06/05 15:25
1.POM代表项目对象模型。
它是 Maven 中工作的基本单位,这是一个 XML 文件。它始终保存在该项目基本目录中的 pom.xml 文件。
POM 包含的项目是使用 Maven 来构建的,它用来包含各种配置信息。
POM 也包含了目标和插件。在执行任务或目标时,Maven 会使用当前目录中的 POM。它读取POM得到所需要的配置信息,然后执行目标。部分的配置可以在 POM 使用如下:
  • project dependencies
  • plugins
  • goals
  • build profiles
  • project version
  • developers
  • mailing list

创建一个POM之前,应该要先决定项目组(groupId),它的名字(artifactId)和版本,因为这些属性在项目仓库是唯一标识的。



2.每个项目只有一个POM文件。
  • 所有的 POM 文件要项目元素必须有三个必填字段: groupIdartifactIdversion
  • 在库中的项目符号是:groupId:artifactId:version
  • pom.xml 的根元素是 project,它有三个主要的子节点。
节点描述groupId这是项目组的编号,这在组织或项目中通常是独一无二的。 例如,一家银行集团com.company.bank拥有所有银行相关项目。artifactId这是项目的ID。这通常是项目的名称。 例如,consumer-banking。 除了groupId之外,artifactId还定义了artifact在存储库中的位置。version这是项目的版本。与groupId一起使用,artifact在存储库中用于将版本彼此分离。 例如:com.company.bank:consumer-banking:1.0com.company.bank:consumer-banking:1.1





3.buid Plugins

[html] view plain copy
  1. <build>  
  2.     ...  
  3.     <plugins>  
  4.         <plugin>  
  5.             <groupId>org.apache.maven.plugins</groupId>  
  6.             <artifactId>maven-jar-plugin</artifactId>  
  7.             <version>2.0</version>  
  8.             <extensions>false</extensions>  
  9.             <inherited>true</inherited>  
  10.             <configuration>  
  11.                 <classifier>test</classifier>  
  12.             </configuration>  
  13.             <dependencies>...</dependencies>  
  14.             <executions>...</executions>  
  15.         </plugin>  
  16.     </plugins>  
  17. </build>  
除了groupId:artifactId:version标准坐标,plugin还需要如下属性:
1、extensions:true/false,是否加载plugin的extensions,默认为false;
2、inherited:true/false,这个plugin是否应用到该POM的孩子POM,默认true;
3、configuration:配置该plugin期望得到的properies,如上面的例子,我们为maven-jar-plugin的Mojo设置了classifier属性;

如果你的POM有一个parent,它可以从parent的build/plugins或者pluginManagement集成plugin配置。

为了阐述继承后的关系,考虑如果parent POM中存在如下plugin:

[html] view plain copy
  1. <plugin>  
  2.     <groupId>my.group</groupId>  
  3.     <artifactId>my-plugin</artifactId>  
  4.     <configuration>  
  5.         <items>  
  6.             <item>parent-1</item>  
  7.             <item>parent-2</item>  
  8.         </items>  
  9.         <properties>  
  10.             <parentKey>parent</parentKey>  
  11.         </properties>  
  12.     </configuration>  
  13. </plugin>  

然后在继承的孩子POM中做如下配置:

[html] view plain copy
  1. <pre name="code" class="html"><plugin>  
  2.     <groupId>my.group</groupId>  
  3.     <artifactId>my-plugin</artifactId>  
  4.     <configuration>  
  5.         <items>  
  6.             <item>child-1</item>  
  7.         </items>  
  8.         <properties>  
  9.             <childKey>child</childKey>  
  10.         </properties>  
  11.     </configuration>  
  12. </plugin></pre>  

这样孩子POM和parent POM中都存在groupId为my.group的plugin,Maven默认的行为将是根据属性名称将两个plugin的configuration的内容进行合并。如果孩子POM中有一个属性,则该属性是有效的,如果孩子POM中没有一个属性,但parent POM中存在,则parent中的属性是有效的。

根据这些规则,上面的例子在Maven中将得到:

[html] view plain copy
  1. <pre name="code" class="html"><plugin>  
  2.     <groupId>my.group</groupId>  
  3.     <artifactId>my-plugin</artifactId>  
  4.     <configuration>  
  5.         <items>  
  6.             <item>child-1</item>  
  7.         </items>  
  8.         <properties>  
  9.             <childKey>child</childKey>  
  10.             <parentKey>parent</parentKey>  
  11.         </properties>  
  12.     </configuration>  
  13. </plugin></pre>  

通过在configuration元素中增加combine.children和combine.self属性,孩子POM可以控制Maven怎么合并plugin的configuration。

假定这儿是孩子POM的configuration:

[html] view plain copy
  1. <pre name="code" class="html"><configuration>  
  2.     <items combine.children="append">  
  3.         <!-- combine.children="merge" is the default -->  
  4.         <item>child-1</item>  
  5.     </items>  
  6.     <properties combine.self="override">  
  7.         <!-- combine.self="merge" is the default -->  
  8.         <childKey>child</childKey>  
  9.     </properties>  
  10. </configuration></pre>  

则,现在合并后的效果如下:

[html] view plain copy
  1. <pre name="code" class="html"><configuration>  
  2.     <items combine.children="append">  
  3.         <item>parent-1</item>  
  4.         <item>parent-2</item>  
  5.         <item>child-1</item>  
  6.     </items>  
  7.     <properties combine.self="override">  
  8.         <childKey>child</childKey>  
  9.     </properties>  
  10. </configuration></pre>  

combine.children="append"表示父POM和子POM的属性合并起来;

combine.self="override"表示子POM的属性完全覆盖父POM的。

4、dependencies:同base build中的dependencies有同样的结构和功能,但这里是作为plugin的依赖,而不是项目的依赖。
5、executions:plugin可以有多个目标,每一个目标都可以有一个分开的配置,甚至可以绑定一个plugin的目标到一个不同的阶段。executions配置一个plugin的目标的execution。

假定一项绑定antrun:run目标到verify阶段,我们希望任务响应build文件夹,同时避免传递配置到他的孩子POM。你将得到一个execution:

[html] view plain copy
  1. <pre name="code" class="html"><build>  
  2.     <plugins>  
  3.         <plugin>  
  4.             <artifactId>maven-antrun-plugin</artifactId>  
  5.             <version>1.1</version>  
  6.             <executions>  
  7.                 <execution>  
  8.                     <id>echodir</id>  
  9.                     <goals>  
  10.                         <goal>run</goal>  
  11.                     </goals>  
  12.                     <phase>verify</phase>  
  13.                     <inherited>false</inherited>  
  14.                     <configuration>  
  15.                         <tasks>  
  16.                             <echo>Build Dir: ${project.build.directory}</echo>  
  17.                         </tasks>  
  18.                     </configuration>  
  19.                 </execution>  
  20.             </executions>  
  21.         </plugin>  
  22.     </plugins>  
  23. </build></pre>  

id:标识,用于和其他execution区分。当这个阶段执行时,它将以这个形式展示:[plugin:goal execution: id]。在这里为: [antrun:run execution: echodir];

goals:一个plugin的execution的目标列表;

phase:目标执行的阶段,具体值看Maven的生命周期列表;

inherited:是否继承;

configuration:在指定的目标下的配置。