MAVEN pom.xml 解读

来源:互联网 发布:解压缩软件 安卓 编辑:程序博客网 时间:2024/04/30 16:31
POM全称是Project Object  Model,即项目对象模型。pom.xml是maven的项目描述文件,它类似与antx的project.xml文件。pom.xml文件以xml的 形式描述项目的信息,包括项目名称、版本、项目id、项目的依赖关系、编译环境、持续集成、项目团队、贡献管理、生成报表等等。总之,它包含了所有的项目 信息


1. pom.xml的基本配置

<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>  <groupId>org.codehaus.mojo</groupId>  <artifactId>my-project</artifactId>  <version>1.0</version></project>

modelVersion          描述这个POM文件是遵从哪个版本的项目描述符。 

groupId                     针对一个项目的普遍唯一识别符。通常用一个完全正确的包的名字来与其他项目的类似名字来进行区分(比如:org.apache.maven)。   

artifactId                   在给定groupID 的group里面为artifact 指定的标识符是唯一的 , artifact 代表的是被制作或者被一个project应用的组件(产出物)。

version                     当前项目产生的artifact的版本

以上4个元素缺一不可,其中groupId, artifactId, version描述依赖的项目唯一标志。


2. pom.xml文件结构

<project>  <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>

3. POM很重要的3个关系


POM有3个很重要的关系:依赖、继承、合成。 


3.1. 依赖关系 


<dependencies>    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>4.0</version>      <type>jar</type>      <scope>test</scope>      <optional>true</optional>    </dependency>    ...</dependencies>


如果想依赖一个maven库中没有的一个jar包,方法很简单,就是先将此jar包使用以下的命令安装到本地maven库中:

mvn install:install-file -Dfile=my.jar -DgroupId=mygroup -DartifactId=myartifactId -Dversion=1

再把依赖关系加进去即可。 


3.2. 继承关系 


另一个强大的变化, maven带来的是项目继承。


3.2.1. 定义父项目 

<project>  <modelVersion>4.0.0</modelVersion>  <groupId>com.mygroup </groupId>  <artifactId>my-parent</artifactId>  <version>2.0</version>  <packaging>pom</packaging></project>
packaging 类型,定义值为 pom用于定义为parent和合成多个项目。 当然我们创建的maven项目的pom都继承maven的super pom, 如果想看项目(父或子)的完全的pom结构,可以运行:  
mvn help:effective-pom

就可以了。


3.2.2. 子项目配置 

<project>   <modelVersion>4.0.0</modelVersion>   <groupId>com.mygroup </groupId>   <artifactId>my-child-project</artifactId>   <parent>        <groupId>com.mygroup </groupId>        <artifactId>my-parent</artifactId>        <version>2.0</version>        <relativePath>../my-parent</relativePath>    </parent></project>

relativePath可以不需要,但是用于指明parent的目录,用于快速查询。  


3.3. 合成关系 


一个项目有多个模块,也叫做多重模块,或者合成项目。    如下的定义:

<project>   <modelVersion>4.0.0</modelVersion>   <groupId>com.mygroup </groupId>   <artifactId>my-parent</artifactId>   <version>2.0</version>   <modules>       <module>my-child-project1<module>       <module>my-child-project2<module>   </modules></project>

其中module 描述的是子项目的相对路径 。

原创粉丝点击