pom.xml文件元素解析

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

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

位置:Maven项目的根目录下

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

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

 

<!-- 1-->
<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">
    <!-- 2-->
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.my.mavenweb</groupId>
  <artifactId>testWeb</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>war</packaging>
    <name>testWebMaven Webapp</name>
     <url>http://maven.apache.org</url>
     <descriprion></descriprion>
     <developers></developers>
     <licenses></licenses>
     <organization></organization>
    
<!-- 3-->
   <properties> 
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
        <springversion>3.1.1.RELEASE</springversion> 
        <junitversion>3.8.1</junitversion> 
    </properties> 



<!-- 4-->
    <dependencyManagement>
      <dependencies>
        <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-framework-bom</artifactId>
           <version>${spring.version}</version>
           <type>pom</type>
           <scope>import</scope>
        </dependency>
      </dependencies>   
    </dependencyManagement>





<!-- 5-->
    <dependencies>     
              <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
      </dependency>
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
      </dependency>
      <dependency>
        <groupId>commons-fileupload</groupId>
         <artifactId>commons-fileupload</artifactId>
         <version>1.3.1</version>
      </dependency>
    </dependencies>







<!-- 6-->
  <build>
    <finalName>testWebName</finalName>
<plugins>
        <plugin>
           <groupId>org.eclipse.jetty</groupId>
           <artifactId>jetty-maven-plugin</artifactId>
           <version>9.2.2.v20140723</version>
           <executions>
                 <execution>
                 <phase>package</phase>
                 <goals>
                    <goal>run</goal>
                 </goals>
              </execution>
           </executions>
        </plugin>
      </plugins>  
</build>
  <parent></parent>
  <modules></modules>
</project>

 

pom基础元素解析

第一部分:根

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

 <!-- 1-->

<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文件添加自己的属性,如下:

 <!-- 2-->

<!-- 制定了当前pom的版本,是必不可少的-->  

<modelVersion>4.0.0</modelVersion>   


<!-- 项目坐标(groupIdartifactIdversion):定义pom文件属于哪个maven项目

        maven项目是一种模块化的概念,里面将项目划分为模块。

        groupId=反写公司网址.项目名

        artifactId=项目名-模块名

        version=0.0.1SNAPSHOP

        (第一个0表示大本版号,第二个0表示分支版本号,第三个0表示小版本号)

        (版本除了snapshop快照版本之外,还有alpha--α内测版本、beta--β公测版本、Release稳定版本、GA发布版本)

      -->

  <groupId>com.my.mavenweb</groupId>

  <artifactId>testWeb</artifactId>

   <version>0.0.1-SNAPSHOT</version>


<!-- packaging:指定了项目的打包方式,如果内容为空,默认指定打包为Jar包,此外,还要warzippom等类型-->

<packaging>war</packaging>


<!-- name:指定了项目的描述名,一般在产生对象文档的时候才会使用 -->

    <name>testWebMaven Webapp</name>


 <!--URl:指定了项目的Url -->

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

   

<!-- description:项目描述信息

     developers:开发人员信息

     licenses:开源框架的许可证信息

     organization:组织信息    

     -->

     <descriprion></descriprion>

     <developers></developers>

     <licenses></licenses>

     <organization></organization>

 

第三部分:properties

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

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

<!-- 3-->

   <properties> 

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

        <springversion>3.1.1.RELEASE</springversion> 

        <junitversion>3.8.1</junitversion> 

    </properties>

 

第四部分:dependencyManagement

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

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

 

第五部分:dependencies

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

 <!--

 dependencies:依赖信息列表

 dependency:依赖项

 groupIdartifactIdversion:依赖项的位置坐标,方便查找依赖。

 type:依赖项的类型,jar/war/zip/pom等类型

 scope:依赖范围,是指当前依赖项在哪个范围内有效。

 potional:依赖是否可选,默认为false,则项目可以继承该依赖;如果为true,则子项目必须显式引入该依赖

 exclusions:排除依赖的传递列表

 exclusion:排除依赖,例如排除传递依赖中自己不需要的依赖项

 -->

<!-- 5-->

    <dependencies>     

              <dependency>

        <groupId>org.springframework</groupId>

        <artifactId>spring-webmvc</artifactId>

      </dependency>

      <dependency>

        <groupId>junit</groupId>

        <artifactId>junit</artifactId>

        <version>3.8.1</version>

        <scope>test</scope>

      </dependency>

      <dependency>

        <groupId>commons-fileupload</groupId>

         <artifactId>commons-fileupload</artifactId>

         <version>1.3.1</version>

      </dependency>

    </dependencies>

 

第六部分:build

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

  <!--

 build:系统项目构建行为知识

  plugins:引入的插件的列表

  plugin:插件,这个标签下需要声明插件的坐标,

 -->

  <!--

   parent:通常用于子模块对父模块POM的集成

  modules:用于聚合运行多个项目,指定多个模块一起编译。

  -->

<!-- 6-->

  <build>

    <finalName>testWebName</finalName>

<plugins>

        <plugin>

           <groupId>org.eclipse.jetty</groupId>

           <artifactId>jetty-maven-plugin</artifactId>

           <version>9.2.2.v20140723</version>

           <executions>

                 <execution>

                 <phase>package</phase>

                 <goals>

                  <goal>run</goal>

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

 





  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值