zoukankan      html  css  js  c++  java
  • pom.xml详解

    本文转载自http://blog.csdn.net/zhuxinhua/article/details/5788546

    快速察看

    <project>
      <modelVersion>4.0.0</modelVersion>
    <!--maven2.0必须是这样写,现在是maven2唯一支持的版本-->
      <!-- 基础设置 -->
      <groupId>...</groupId>
      <artifactId>...</artifactId>
      <version>...</version>
      <packaging>...</packaging>
    
      <name>...</name>
    
      <url>...</url>
      <dependencies>...</dependencies>
      <parent>...</parent>
      <dependencyManagement>...</dependencyManagement>
      <modules>...</modules>
      <properties>...</properties>
    
      <!--构建设置 -->
      <build>...</build>
      <reporting>...</reporting>
    
      <!-- 更多项目信息 -->
      <name>...</name>
      <description>...</description>
      <url>...</url>
      <inceptionYear>...</inceptionYear>
      <licenses>...</licenses>
      <organization>...</organization>
      <developers>...</developers>
      <contributors>...</contributors>
    
      <!-- 环境设置-->
      <issueManagement>...</issueManagement>
      <ciManagement>...</ciManagement>
      <mailingLists>...</mailingLists> 
      <scm>...</scm>
      <prerequisites>...</prerequisites>
      <repositories>...</repositories>
      <pluginRepositories>...</pluginRepositories>
      <distributionManagement>...</distributionManagement>
      <profiles>...</profiles>
    </project>

    基本内容:

    pom包括了所有的项目信息

    groupId:项目或组织的唯一标识,并且配置时生成路径也是由此生成。如org.myproject.mojo生成的相对路径为:/org/myproject/mojo

    artifactId:项目的通用名称

    version:项目的版本

    packaging:打包机制。例如pom,jar,maven-plugin,ejb,war,ear,rar,par

    name:用户描述项目的名称,无关紧要的东西,可选。

    url:应该是只写明开发团队的网站,无关紧要,可选。

    classifer:分类,可选。

    其中,groupId+artifactId+version+packaging组成了项目的唯一坐标,一般情况下,前面三个就可以组成唯一坐标。

    POM关系:主要为依赖、继承、合成。

    dependency属性介绍

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.1</version>
    </dependency>

    type:表示组件的类型,默认为jar。

    scope:表示当前组件的依赖范围。

    optional:设置依赖是否可选,默认false,子项目默认都继承。如果设置为true,子项目必须显示引入。与dependencyManagement里定义的依赖类似。

    exclusions:如果X需要A,A包含B依赖,则可以用此属性声明exclusion。

    exclusion:将B从依赖树中删除。

    如上图中的三个属性为必要属性。

    groupId+artifactId+version组成了该组件在maven仓库里的准确路径。假如仓库路径为D:apache-maven-3.5.0 epository。则如上配置后,对应的文件位置应该在D:apache-maven-3.5.0 epositoryorgapachecommonscommons-lang33.1文件夹下。

    如果一个工程是parent或aggregation(即mutil-module的),则packaging必须设置为pom。

    子工程从父工程继承的包括:dependencies,developers,contributors,plugin lists,reports lists,plugin execution with matching ids,plugin configuration

    一个maven项目想要继承另一个maven项目,可以在pom.xml文件中做如下配置

    <parent> 
        <groupId>org.codehaus.mojo</groupId> 
        <artifactId>my-parent</artifactId> 
        <version>2.0</version> 
        <relativePath>../my-parent</relativePath> 
    </parent>

    其中relativePath属性是可选的,maven在搜索本地仓库之前会优先搜索这个地址。上面其余三个属性可以唯一确定一个父工程。

    dependencyManagement:用来帮助管理children的dependencies。

    <dependencyManagement>
        <dependencies>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.2</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    如上图在父工程中定义后,子工程只能引用groupId和artifactId,version则由父工程控制。

    这样做的优点是可以集中管理依赖的详情。

    modules:父工程(或聚合工程)表现它有哪些子模块。顺序是不重要的,maven会根据依赖关系来拓扑排序

    <modules>
          <module>e3.manager-dao</module>
          <module>e3.manager-interface</module>
          <module>e3.manager-pojo</module>
          <module>e3.manager-service</module>
          <module>e3.manager-web</module>
    </modules>

    properties:为pom定义一些常量,在pom的其他地方可以直接引用。

    <properties>
        <junit.version>4.12</junit.version>
    </properties>

    使用方式:${junit.version}

    也可以获取pom里定义的其他属性,如${project.version} 获取项目版本。如${project.artifactId} 获取项目名。

    build以下属性:

    finalName:指定去掉后缀的工程名。${project.artifactId}

    directory:指定build target目标的目录,默认为根目录下的target

    filters:定义指定filter属性的位置。

    <filters> 
        <filter>src/main/filters/filter-${env}.properties</filter> 
    </filters> 

    resource:描述工程中资源的位置。

    <resource> 
        <targetPath>META-INF/plexus</targetPath> 
        <filtering>false</filtering> 
        <directory>${basedir}/src/main/plexus</directory> 
        <includes> 
            <include>configuration.xml</include> 
        </includes> 
        <excludes> 
            <exclude>**/*.properties</exclude> 
        </excludes> 
    </resource>

    其中:targetPath指定build资源到哪个目录。

      filtering:指定filter文件值在这个resource中是否有效。例如这里指定的是在configuration.xml文件中无效。

      directory:指定属性文件的目录,build过程需要找到它,并将其放到targetPath目录下。默认的directory是src/main/resources

      includes:指定包含文件的patterns,符合样式,并且在directory目录下的文件,将会包含进project的资源文件。

      excludes:指定不包含在内的patterns,如果与includes有冲突,以excludes为主。

    testResources:包含测试资源元素,定义内容与resources类似,不同的是默认测试资源路径是src/test/resources,并且测试资源是不部署的。

    plugins配置(plugins是在build标签内):

    <plugin> 
        <groupId>org.apache.maven.plugins</groupId> 
        <artifactId>maven-jar-plugin</artifactId> 
        <version>2.0</version> 
        <extensions>false</extensions> 
        <inherited>true</inherited> 
        <configuration> 
            <classifier>test</classifier> 
        </configuration> 
        <dependencies>...</dependencies> 
        <executions>...</executions> 
    </plugin>

    extensions:决定是否load这个plugin的extensions,默认为true。

    inherited:是否让子工程集成,默认为true。

    configuration:通常用于私有不开源的plugin,也可以指定字符集,类似<encoding>UTF-8</encoding>。

    pluginManagement:类似denpendencyManagement,只是后者用于管理项目jar包依赖,前者用于管理plugin。pluginManagement与plugin区别是前者列出来,让子工程决定是否引用。

  • 相关阅读:
    Best Time to Buy and Sell Stock
    Remove Nth Node From End of List
    Unique Paths
    Swap Nodes in Pairs
    Convert Sorted Array to Binary Search Tree
    Populating Next Right Pointers in Each Node
    Maximum Subarray
    Climbing Stairs
    Unique Binary Search Trees
    Remove Duplicates from Sorted Array
  • 原文地址:https://www.cnblogs.com/yxth/p/7122314.html
Copyright © 2011-2022 走看看