pom.xml详解



一、什么是POM

Project Object Model,项目对象模型。通过xml格式保存的pom.xml文件。作用类似ant的build.xml文件,功能更强大。该文件用于管理:源代码、配置文件、开发者的信息和角色、问题追踪系统、组织信息、项目授权、项目的url、项目的依赖关系等等。

一个完整的pom.xml文件,放置在项目的根目录下。

[html] view plain copy print ?
  1. <project xmlns="http://maven.apache.org/POM/4.0.0"  
  2.   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0  
  4.                       http://maven.apache.org/maven-v4_0_0.xsd">  
  5.   <modelVersion>4.0.0modelVersion>  
  6.   >  
  7.   <groupId>groupId>  
  8.   <artifactId>artifactId>  
  9.   <version>version>  
  10.   <packaging>packaging>  
  11.   <dependencies>dependencies>  
  12.   <parent>parent>  
  13.   <dependencyManagement>dependencyManagement>  
  14.   <modules>modules>  
  15.   <properties>properties>  
  16.   >  
  17.   <build>build>  
  18.   <reporting>reporting>  
  19.   >  
  20.   <name>name>  
  21.   <description>description>  
  22.   <url>url>  
  23.   <inceptionYear>inceptionYear>  
  24.   <licenses>licenses>  
  25.   <organization>organization>  
  26.   <developers>developers>  
  27.   <contributors>contributors>  
  28.   >  
  29.   <issueManagement>issueManagement>  
  30.   <ciManagement>ciManagement>  
  31.   <mailingLists>mailingLists>  
  32.   <scm>scm>  
  33.   <prerequisites>prerequisites>  
  34.   <repositories>repositories>  
  35.   <pluginRepositories>pluginRepositories>  
  36.   <distributionManagement>distributionManagement>  
  37.   <profiles>profiles>  
  38. project>  

  4.0.0
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  

二、基本设置

1、maven的协作相关属性

[html] view plain copy print ?
  1. <project xmlns="http://maven.apache.org/POM/4.0.0"  
  2.   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0  
  4.                       http://maven.apache.org/maven-v4_0_0.xsd">  
  5.   <modelVersion>4.0.0modelVersion>  
  6.   <groupId>org.codehaus.mojogroupId>  
  7.   <artifactId>my-projectartifactId>  
  8.   <version>1.0version>  
  9.   <packaging>warpackaging>  
  10. project>  

  4.0.0
  org.codehaus.mojo
  my-project
  1.0
  war
  1. groupId : 组织标识,例如:org.codehaus.mojo,在M2_REPO目录下,将是: org/codehaus/mojo目录。
  2. artifactId : 项目名称,例如:my-project,在M2_REPO目录下,将是:org/codehaus/mojo/my-project目录。
  3. version : 版本号,例如:1.0,在M2_REPO目录下,将是:org/codehaus/mojo/my-project/1.0目录。
  4. packaging : 打包的格式,可以为:pom , jar , maven-plugin , ejb , war , ear , rar , par

2、POM之间的关系

主要用于POM文件的复用。

a)依赖关系:依赖关系列表(dependency list)是POM的重要部分

[html] view plain copy print ?
  1. <dependencies>  
  2.    <dependency>  
  3.      <groupId>junitgroupId>  
  4.      <artifactId>junitartifactId>  
  5.      <version>4.0version>  
  6.      <scope>testscope>  
  7.    dependency>  
  8.    …  
  9.  dependencies>  
 
    
      junit
      junit
      4.0
      test
    
  1. groupId , artifactId , version :
  2. scope : compile(default),provided,runtime,test,system
  3. exclusions

b)继承关系:继承其他pom.xml配置的机制。

比如父pom.xml:

[html] view plain copy print ?
  1. <project>  
  2.   [...]  
  3.   <dependencies>  
  4.     <dependency>  
  5.       <groupId>junitgroupId>  
  6.       <artifactId>junitartifactId>  
  7.       <version>4.4version>  
  8.       <scope>testscope>  
  9.     dependency>  
  10.   dependencies>  
  11.   [...]  
  12. project>  

  [...]
  
    
      junit
      junit
      4.4
      test
    
  
  [...]
在子pom.xml文件继承它的依赖(还可以继承其他的:developers and contributors、plugin lists、reports lists、plugin executions with matching ids、plugin configuration):
[html] view plain copy print ?
  1. [...]  
  2. <parent>  
  3. <groupId>com.devzuz.mvnbook.proficiogroupId>  
  4.   <artifactId>proficioartifactId>  
  5.   <version>1.0-SNAPSHOTversion>  
  6. parent>  
  7. [...]  
[...]

com.devzuz.mvnbook.proficio
  proficio
  1.0-SNAPSHOT

[...]
在这种机制下,maven还提供了一个类似java.lang.Object的顶级父pom.xml文件:
[html] view plain copy print ?
  1. <project>  
  2.   <modelVersion>4.0.0modelVersion>  
  3.   <name>Maven Default Projectname>  
  4.   <repositories>  
  5.     <repository>  
  6.       <id>centralid>  
  7.       <name>Maven Repository Switchboardname>  
  8.       <layout>defaultlayout>  
  9.       <url>http://repo1.maven.org/maven2url>  
  10.       <snapshots>  
  11.         <enabled>falseenabled>  
  12.       snapshots>  
  13.     repository>  
  14.   repositories>  
  15.   <pluginRepositories>  
  16.     <pluginRepository>  
  17.       <id>centralid>  
  18.       <name>Maven Plugin Repositoryname>  
  19.       <url>http://repo1.maven.org/maven2url>  
  20.       <layout>defaultlayout>  
  21.       <snapshots>  
  22.         <enabled>falseenabled>  
  23.       snapshots>  
  24.       <releases>  
  25.         <updatePolicy>neverupdatePolicy>  
  26.       releases>  
  27.     pluginRepository>  
  28.   pluginRepositories>  
  29.   <build>  
  30.     <directory>targetdirectory>  
  31.     <outputDirectory>target/classesoutputDirectory>  
  32.     <finalName>${project.artifactId}-${project.version}finalName>  
  33.     <testOutputDirectory>target/test-classestestOutputDirectory>  
  34.     <sourceDirectory>src/main/javasourceDirectory>  
  35.     <scriptSourceDirectory>src/main/scriptsscriptSourceDirectory>  
  36.     <testSourceDirectory>src/test/javatestSourceDirectory>  
  37.     <resources>  
  38.       <resource>  
  39.         <directory>src/main/resourcesdirectory>  
  40.       resource>  
  41.     resources>  
  42.     <testResources>  
  43.       <testResource>  
  44.         <directory>src/test/resourcesdirectory>  
  45.       testResource>  
  46.     testResources>  
  47.     <pluginManagement>  
  48.        <plugins>  
  49.          <plugin>  
  50.            <artifactId>maven-antrun-pluginartifactId>  
  51.            <version>1.1version>  
  52.          plugin>        
  53.          <plugin>  
  54.            <artifactId>maven-assembly-pluginartifactId>  
  55.            <version>2.2-beta-2version>  
  56.          plugin>  
  57.          <plugin>  
  58.            <artifactId>maven-clean-pluginartifactId>  
  59.            <version>2.2version>  
  60.          plugin>  
  61.          <plugin>  
  62.            <artifactId>maven-compiler-pluginartifactId>  
  63.            <version>2.0.2version>  
  64.          plugin>  
  65.          <plugin>  
  66.            <artifactId>maven-dependency-pluginartifactId>  
  67.            <version>2.0version>  
  68.          plugin>  
  69.          <plugin>  
  70.            <artifactId>maven-deploy-pluginartifactId>  
  71.            <version>2.3version>  
  72.          plugin>  
  73.          <plugin>  
  74.            <artifactId>maven-ear-pluginartifactId>  
  75.            <version>2.3.1version>  
  76.          plugin>  
  77.          <plugin>  
  78.            <artifactId>maven-ejb-pluginartifactId>  
  79.            <version>2.1version>  
  80.          plugin>  
  81.          <plugin>  
  82.            <artifactId>maven-install-pluginartifactId>  
  83.            <version>2.2version>  
  84.          plugin>  
  85.          <plugin>  
  86.            <artifactId>maven-jar-pluginartifactId>  
  87.            <version>2.2version>  
  88.          plugin>  
  89.          <plugin>  
  90.            <artifactId>maven-javadoc-pluginartifactId>  
  91.            <version>2.4version>  
  92.          plugin>  
  93.          <plugin>  
  94.            <artifactId>maven-plugin-pluginartifactId>  
  95.            <version>2.4.1version>  
  96.          plugin>  
  97.          <plugin>  
  98.            <artifactId>maven-rar-pluginartifactId>  
  99.            <version>2.2version>  
  100.          plugin>  
  101.          <plugin>                 
  102.            <artifactId>maven-release-pluginartifactId>  
  103.            <version>2.0-beta-7version>  
  104.          plugin>  
  105.          <plugin>                 
  106.            <artifactId>maven-resources-pluginartifactId>  
  107.            <version>2.2version>  
  108.          plugin>  
  109.          <plugin>  
  110.            <artifactId>maven-site-pluginartifactId>  
  111.            <version>2.0-beta-6version>  
  112.          plugin>  
  113.          <plugin>  
  114.            <artifactId>maven-source-pluginartifactId>  
  115.            <version>2.0.4version>  
  116.          plugin>           
  117.          <plugin>  
  118.             <artifactId>maven-surefire-pluginartifactId>  
  119.             <version>2.4.2version>  
  120.          plugin>  
  121.          <plugin>  
  122.            <artifactId>maven-war-pluginartifactId>  
  123.            <version>2.1-alpha-1version>  
  124.          plugin>  
  125.        plugins>  
  126.      pluginManagement>  
  127.   build>  
  128.   <reporting>  
  129.     <outputDirectory>target/siteoutputDirectory>  
  130.   reporting>  
  131.   <profiles>  
  132.     <profile>  
  133.       <id>release-profileid>  
  134.       <activation>  
  135.         <property>  
  136.           <name>performReleasename>  
  137.           <value>truevalue>  
  138.         property>  
  139.       activation>  
  140.       <build>  
  141.         <plugins>  
  142.           <plugin>  
  143.             <inherited>trueinherited>  
  144.             <groupId>org.apache.maven.pluginsgroupId>  
  145.             <artifactId>maven-source-pluginartifactId>  
  146.             <executions>  
  147.               <execution>  
  148.                 <id>attach-sourcesid>  
  149.                 <goals>  
  150.                   <goal>jargoal>  
  151.                 goals>  
  152.               execution>  
  153.             executions>  
  154.           plugin>  
  155.           <plugin>  
  156.             <inherited>trueinherited>  
  157.             <groupId>org.apache.maven.pluginsgroupId>  
  158.             <artifactId>maven-javadoc-pluginartifactId>  
  159.             <executions>  
  160.               <execution>  
  161.                 <id>attach-javadocsid>  
  162.                 <goals>  
  163.                   <goal>jargoal>  
  164.                 goals>  
  165.               execution>  
  166.             executions>  
  167.           plugin>  
  168.           <plugin>  
  169.             <inherited>trueinherited>  
  170.             <groupId>org.apache.maven.pluginsgroupId>  
  171.             <artifactId>maven-deploy-pluginartifactId>  
  172.             <configuration>  
  173.               <updateReleaseInfo>trueupdateReleaseInfo>  
  174.             configuration>  
  175.           plugin>  
  176.         plugins>  
  177.       build>  
  178.     profile>  
  179.   profiles>  
  180. project>  

  4.0.0
  Maven Default Project
  
    
      central
      Maven Repository Switchboard
      default
      http://repo1.maven.org/maven2
      
        false
      
    
  
  
    
      central
      Maven Plugin Repository
      http://repo1.maven.org/maven2
      default
      
        false
      
      
        never
      
    
  
  
    target
    target/classes
    ${project.artifactId}-${project.version}
    target/test-classes
    src/main/java
    src/main/scripts
    src/test/java
    
      
        src/main/resources
      
    
    
      
        src/test/resources
      
    
    
       
         
           maven-antrun-plugin
           1.1
               
         
           maven-assembly-plugin
           2.2-beta-2
         
         
           maven-clean-plugin
           2.2
         
         
           maven-compiler-plugin
           2.0.2
         
         
           maven-dependency-plugin
           2.0
         
         
           maven-deploy-plugin
           2.3
         
         
           maven-ear-plugin
           2.3.1
         
         
           maven-ejb-plugin
           2.1
         
         
           maven-install-plugin
           2.2
         
         
           maven-jar-plugin
           2.2
         
         
           maven-javadoc-plugin
           2.4
         
         
           maven-plugin-plugin
           2.4.1
         
         
           maven-rar-plugin
           2.2
         
                        
           maven-release-plugin
           2.0-beta-7
         
                        
           maven-resources-plugin
           2.2
         
         
           maven-site-plugin
           2.0-beta-6
         
         
           maven-source-plugin
           2.0.4
                  
         
            maven-surefire-plugin
            2.4.2
         
         
           maven-war-plugin
           2.1-alpha-1
         
       
     
  
  
    target/site
  
  
    
      release-profile
      
        
          performRelease
          true
        
      
      
        
          
            true
            org.apache.maven.plugins
            maven-source-plugin
            
              
                attach-sources
                
                  jar
                
              
            
          
          
            true
            org.apache.maven.plugins
            maven-javadoc-plugin
            
              
                attach-javadocs
                
                  jar
                
              
            
          
          
            true
            org.apache.maven.plugins
            maven-deploy-plugin
            
              true
            
          
        
      
    
  
可以通过下面命令查看当前pom.xml受到超pom.xml文件的影响:
mvn help:effective-pom

c)聚合关系:用于将多个maven项目聚合为一个大的项目。

[html] view plain copy print ?
  1. <project xmlns="http://maven.apache.org/POM/4.0.0"  
  2.   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0  
  4.                       http://maven.apache.org/maven-v4_0_0.xsd">  
  5.   <modelVersion>4.0.0modelVersion>  
  6.   <groupId>org.codehaus.mojogroupId>  
  7.   <artifactId>my-parentartifactId>  
  8.   <version>2.0version>  
  9.   <modules>  
  10.     <module>my-project<module>  
  11.   modules>  
  12. project>  

  4.0.0
  org.codehaus.mojo
  my-parent
  2.0
  
    my-project
  

3、属性

maven的属性,是值的占位符,类似EL,类似ant的属性,比如${X},可用于pom文件任何赋值的位置。有以下分类:

  1. env.X:操作系统环境变量,比如${env.PATH}
  2. project.x:pom文件中的属性,比如:1.0,引用方式:${project.version}
  3. settings.x:settings.xml文件中的属性,比如:false,引用方式:${settings.offline}
  4. Java System Properties:java.lang.System.getProperties()中的属性,比如java.home,引用方式:${java.home}
  5. 自定义:在pom文件中可以:c:/apps/cargo-installs,引用方式:${installDir}

4、构建设置

构建有两种build标签:

[html] view plain copy print ?
  1. <project xmlns="http://maven.apache.org/POM/4.0.0"  
  2.   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0  
  4.                       http://maven.apache.org/maven-v4_0_0.xsd">  
  5.   …  
  6.   >  
  7.   <build>build>  
  8.   <profiles>  
  9.     <profile>  
  10.       >  
  11.       <build>build>  
  12.     profile>  
  13.   profiles>  
  14. project>  

  
  
    
      
      
    
  

build中的主要标签:Resources和Plugins。

Resources:用于排除或包含某些资源文件

[html] view plain copy print ?
  1. <resources>  
  2.   <resource>  
  3.     <targetPath>META-INF/plexustargetPath>  
  4.     <filtering>falsefiltering>  
  5.     <directory>${basedir}/src/main/plexusdirectory>  
  6.     <includes>  
  7.       <include>configuration.xmlinclude>  
  8.     includes>  
  9.     <excludes>  
  10.       <exclude>**/*.propertiesexclude>  
  11.     excludes>  
  12.   resource>  
  13. resources>  
    
      
        META-INF/plexus
        false
        ${basedir}/src/main/plexus
        
          configuration.xml
        
        
          **/*.properties
        
      
    
Plugins:设置构建的插件
[html] view plain copy print ?
  1. <build>  
  2.    …  
  3.    <plugins>  
  4.      <plugin>  
  5.        <groupId>org.apache.maven.pluginsgroupId>  
  6.        <artifactId>maven-jar-pluginartifactId>  
  7.        <version>2.0version>  
  8.        <extensions>falseextensions>  
  9.        <inherited>trueinherited>  
  10.        <configuration>  
  11.          <classifier>testclassifier>  
  12.        configuration>  
  13.        <dependencies>dependencies>  
  14.        <executions>executions>  
  15.      plugin>  
 
      
        org.apache.maven.plugins
        maven-jar-plugin
        2.0
        false
        true
        
          test
        
        
        
      






你可能感兴趣的:(开发工具,架构)