pom.xml文件元素解析

概念:pom.xml---Project Object Model,是一种项目对象模型,通过xml格式保存,在maven中的作用类似于ant的build.xml文件,但是功能更加强大。

作用:该文件用于管理:源代码、配置文件、开发者的信息和角色、问题追踪系统、组织信息、项目授权、项目的url、项目的依赖关系等等。

位置:Maven项目的根目录下

结构:一个完整的pom文件大概包括6个部分,如下所示,其中后面四个部分是主要的,分别是properties、dependencyManagement、dependencies、build等四个元素标签,下面对其一一解说。

先贴一个常规的pom.xml文件配置,可以跳过不看这个,主要看下面的基础解析部分。

 



    
  4.0.0
  com.my.mavenweb
  testWeb
   0.0.1-SNAPSHOT
   war
    testWebMaven Webapp
     http://maven.apache.org
     
     
     
     
    

    
        UTF-8 
        3.1.1.RELEASE 
        3.8.1 
     




    
      
        
           org.springframework
           spring-framework-bom
           ${spring.version}
           pom
           import
        
         
    






         
              
        org.springframework
        spring-webmvc
      
      
        junit
        junit
        3.8.1
        test
      
      
        commons-fileupload
         commons-fileupload
         1.3.1
      
    








  
    testWebName

        
           org.eclipse.jetty
           jetty-maven-plugin
           9.2.2.v20140723
           
                 
                 package
                 
                    run
                 
              
           
        
        

  
  

 

pom基础元素解析

第一部分:根

      这是对Project添加一些根元素的约束信息

 

<projectxmlns="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.0http://maven.apache.org/maven-v4_0_0.xsd">

 

第二部分:pom属性

这是对当前pom文件添加自己的属性,如下:

 

  

<modelVersion>4.0.0modelVersion>   


  <groupId>com.my.mavenwebgroupId>

  <artifactId>testWebartifactId>

   <version>0.0.1-SNAPSHOTversion>


<packaging>warpackaging>


    <name>testWebMaven Webappname>


 

     <url>http://maven.apache.orgurl>

   

     <descriprion>descriprion>

     <developers>developers>

     <licenses>licenses>

     <organization>organization>

 

第三部分:properties

properties列表用于声明一些软件包的版本。当工程比较繁杂庞大的时候,则可以通过这个配置比较清晰的指导依赖包的版本,方便我们做一些气的处理。

如下所示:生命了源文件的编码格式为UTF-8,spring框架依赖包的版本为3.1.1.RELEASE,Junit的版本为3.8.1。

   <properties> 

        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> 

        <springversion>3.1.1.RELEASEspringversion> 

        <junitversion>3.8.1junitversion> 

    properties>

 

第四部分:dependencyManagement

dependencyManagement属性列表,用于依赖管理,声明使用指定版本的包,重在声明依赖,可以不引入实际的依赖项。

又叫做多模块依赖项版本控制:主要是针对父模块列出依赖项进行管理,使得下面需要某个父模块的模块直接继承该依赖,  不用进行传递依赖这么麻烦,但是该标签不会引入到执行当中,只起到一个说明作用。

 

第五部分:dependencies

dependencies属性表示在依赖列表当中加入项目中所要使用的依赖包,需要加入坐标,还可以加入其他的属性。

 

    <dependencies>     

              <dependency>

        <groupId>org.springframeworkgroupId>

        <artifactId>spring-webmvcartifactId>

      dependency>

      <dependency>

        <groupId>junitgroupId>

        <artifactId>junitartifactId>

        <version>3.8.1version>

        <scope>testscope>

      dependency>

      <dependency>

        <groupId>commons-fileuploadgroupId>

         <artifactId>commons-fileuploadartifactId>

         <version>1.3.1version>

      dependency>

    dependencies>

 

第六部分:build

build元素标签用于引入项目中需要用到的插件

  

  

  <build>

    <finalName>testWebNamefinalName>

<plugins>

        <plugin>

           <groupId>org.eclipse.jettygroupId>

           <artifactId>jetty-maven-pluginartifactId>

           <version>9.2.2.v20140723version>

           <executions>

                 <execution>

                 <phase>packagephase>

                 <goals>

                  <goal>rungoal>

                 goals>

              execution>

           executions>

        plugin>

      plugins>  

build>

  <parent>parent>

  <modules>modules>

project>

 

 

dependencyManagement和dependencies的区别


         1)      dependencies:自动引入声明在dependencies里的所有依赖,并默认被所有的子项目继承。如果项目中不写依赖项,则会从父项目继承(属性全部继承)声明在父项目dependencies里的依赖项。         

        2)      dependencyManagement里只是声明依赖,并不实现引入,因此子项目需要显式的声明需要的依赖。如果不在子项目中声明依赖,是不会从父项目中继承的;只有在子项目中写了该依赖项,并且没有指定具体版本,才会从父项目中继承该项,并且version和scope都读取自父pom;如果子项目中指定了版本号,那么会使用子项目中指定的jar版本。同时dependencyManagement让子项目引用依赖,而不用显式的列出版本号。Maven会沿着父子层次向上走,直到找到一个拥有dependencyManagement元素的项目,然后它就会使用在这个dependencyManagement元素中指定的版本号,实现所有子项目使用的依赖项为同一版本。

       3)      dependencyManagement中的 dependencies 并不影响项目的依赖项;而独立dependencies元素则影响项目的依赖项。只有当外层的dependencies元素中没有指明版本信息时,dependencyManagement中的 dependencies 元素才起作用。一个是项目依赖,一个是maven项目多模块情况时作依赖管理控制的。

       4)      pluginManagement和plugins 区别相似,只不过它们用于管理plugin。

 





你可能感兴趣的:(【SSM】)