项目管理工具maven之POM(项目对象模型)

       经过将近一周的学习,虽然深感收获颇丰,但更多的还是觉得需要学习的还有很多很多。真是路漫漫其修远兮。接下去就把本周的学习内容总结一下。

POM

        POM全称是Project Object Model,即项目对象模型。pom.xmlmaven的项目描述文件。pom.xml文件以xml的形式描述项目的信息,包括项目名称、版本、项目id、项目的依赖关系、编译环境、持续集成、项目团队、贡献管理、生成报表等等。

基本配置

<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.seandeng</groupId>

  <artifactId>myapp</artifactId>

  <version>1.0</version>

</project>

modelVersion 描述这个POM文件是遵从哪个版本的项目描述符,目前只能是4.0.0

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

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

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

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

文件结构

<project>

  <modelVersion>4.0.0</modelVersion>

  <groupId>...</groupId>

  <artifactId>...</artifactId>

  <version>...</version>

  <packaging>...</packaging>  //打包机制,如pom,jar,maven-plugin,ejb,war,ear,rar,par

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

POM很重要的3个关系

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

依赖关系

<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

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

继承关系

父项目配置

<project>

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.seandeng</groupId>

  <artifactId>my-parent</artifactId>

  <version>2.0</version>

  <packaging>pom</packaging>

</project>

packaging 类型,定义值为 pom用于定义为parent和合成多个项目。当然我们创建的maven项目的pom都继承mavensuper pom如果想看项目(父或子)的完全的pom结构,可以运行:

mvn help:effective-pom

就可以了。

子项目配置

<project>

   <modelVersion>4.0.0</modelVersion>

   <groupId>com.seandeng</groupId>

   <artifactId>my-child-project</artifactId>

   <parent>

        <groupId>com.seandeng</groupId>

        <artifactId>my-parent</artifactId>

        <version>2.0</version>

        <relativePath>../my-parent</relativePath>

    </parent>

</project>

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

合成关系

一个项目有多个模块。 如下的定义:

<project>

   <modelVersion>4.0.0</modelVersion>

   <groupId>com.seandeng</groupId>

   <artifactId>my-parent</artifactId>

   <version>2.0</version>

   <modules>

       <module>my-child-project1<module>

       <module>my-child-project2<module>

   </modules>

</project>

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

dependencies

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

scope:compile(编译范围), provided(已提供范围), runtime(运行时范围), test(测试范围), system(系统范围)

exclusions如果X需要A,A包含B依赖,那么X可以声明不要B依赖,只要在exclusions中声明exclusion.

Properties

POM片段:

<properties>

    <hibernate.annotations.version>3.3.0.ga</hibernate.annotations.version>

</properties>

 

<dependency>

<groupId>org.hibernate</groupId>

<artifactId>hibernate-annotations</artifactId>

<version>${hibernate.annotations.version}</version>

</dependency>

特殊的属性引用片段:

<parent> 

… …

<version>1.0-SNAPSHOT</version> 

</parent>

<dependencies> 

<dependency> 

    … …

    <version>${project.version}</version>//这个值是一个属性引用,指向了POMproject/version的值

     </dependency>

</dependencies>

dependencyManagement

        Maven还我们提供了一个dependencyManagement元素,用来提供了一种方式来统一依赖版本号。dependencyManagement元素一 般用在顶层的父POM。使用pom.xml中的dependencyManagement元素能让你在子项目中引用一个依赖而不用显式的列出版本号。 Maven会沿着父子层次向上走,直到找到一个拥有dependencyManagement元素的项目,然后它就会使用在这个 dependencyManagement元素中指定的版本号,这样就解决了修改依赖版本号不完全的问题。示例如下:

顶层的父POM片段:

  <dependencyManagement>

    <dependencies>

      <dependency>

        <groupId>org.springframework</groupId>

        <artifactId>spring</artifactId>

        <version>2.0.7</version>

      </dependency>

    </dependencies>

  </dependencyManagement>

子项目POM片段:

  <dependencies>

    <dependency>

      <groupId>org.springframework</groupId>

      <artifactId>spring</artifactId>

    </dependency>

  </dependencies>

Repositories

Repositories:个性化的指定仓库地址。

<repositories> 
    <repository> 
      <releases> 
        <enabled>false</enabled> 
        <updatePolicy>always</updatePolicy> 
        <checksumPolicy>warn</checksumPolicy> 
      </releases> 
      <snapshots> 
        <enabled>true</enabled> 
        <updatePolicy>never</updatePolicy> 
        <checksumPolicy>fail</checksumPolicy> 
      </snapshots> 
      <id>codehausSnapshots</id> 
      <name>Codehaus Snapshots</name> 
      <url>http://snapshots.maven.codehaus.org/maven2</url> 
      <layout>default</layout> 
    </repository> 
  </repositories> 

releasesnapshots:是artifact的两种policiespom可以选择那种政策有效。 
updatePolicy:
说明更新发生的频率always 或者 never 或者 daily(默认的)或者 interval:XX是分钟数) 

checksumPolicy:当Maven的部署文件到仓库中,它也部署了相应的校验和文件。您可以选择忽略,失败,或缺少或不正确的校验和警告。

layoutmaven1.xmaven2有不同的layout,所以可以声明为default或者是legacy(遗留方式maven1.x)。

Profile

        MavenProfile元素可以为一个特殊的环境自定义一个特殊的构建,使得不同环境间构建的可移植性成为可能。比如要使用 production profile来运行mvn install,你需要在命令行传入-Pproduction参数,这里productionprofileid。要验证production profile覆盖了默认的Compiler插件配置,可以像这样以开启调试输入(-X) 的方式运行Maven

它包含可选的activationprofile的触发器)和一系列的changes。例如test过程可能会指向不同的数据库(相对最终的deployment)或者不同的dependencies或者不同的repositories,并且是根据不同的JDK来改变的。那么结构如下: 
  <profiles> 

    <profile> 
      <id>test</id> 
      <activation>...</activation> 
      <build>...</build> 
      <modules>...</modules> 
      <repositories>...</repositories> 
      <pluginRepositories>...</pluginRepositories> 
      <dependencies>...</dependencies> 
      <reporting>...</reporting> 
      <dependencyManagement>...</dependencyManagement> 
      <distributionManagement>...</distributionManagement> 
    </profile> 
  </profiles> 
Activation 触发这个profile的条件配置如下例:(只需要其中一个成立就可以激活profile,如果第一个条件满足了,那么后面就不会在进行匹配。 
    <profile> 
      <id>test</id> 
      <activation> 
        <activeByDefault>false</activeByDefault> 
        <jdk>1.5</jdk> 
        <os> 
          <name>Windows XP</name> 
          <family>Windows</family> 
          <arch>x86</arch> 
          <version>5.1.2600</version> 
        </os> 
        <file> 
          <exists>${basedir}/file2.properties</exists> 
          <missing>${basedir}/file1.properties</missing> 
        </file> 
      </activation> 

猜你喜欢

转载自seandeng888.iteye.com/blog/2116174