pom.xml 介绍

一、什么是POM

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

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

  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.0</modelVersion>  
  6.   <!– The Basics –>  
  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.   <!– Build Settings –>  
  17.   <build></build>  
  18.   <reporting></reporting>  
  19.   <!– More Project Information –>  
  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.   <!– Environment Settings –>  
  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>  
<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <!– The Basics –>
  <groupId>…</groupId>
  <artifactId>…</artifactId>
  <version>…</version>
  <packaging>…</packaging>
  <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>

二、基本设置

1、maven的协作相关属性

  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.0</modelVersion>  
  6.   <groupId>org.codehaus.mojo</groupId>  
  7.   <artifactId>my-project</artifactId>  
  8.   <version>1.0</version>  
  9.   <packaging>war</packaging>  
  10. </project>  
<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>my-project</artifactId>
  <version>1.0</version>
  <packaging>war</packaging>
</project>
  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的重要部分

  1. <dependencies>  
  2.    <dependency>  
  3.      <groupId>junit</groupId>  
  4.      <artifactId>junit</artifactId>  
  5.      <version>4.0</version>  
  6.      <scope>test</scope>  
  7.    </dependency>  
  8.    …  
  9.  </dependencies>  
 <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.0</version>
      <scope>test</scope>
    </dependency>
    …
  </dependencies>
  1. groupId , artifactId , version :
  2. scope : compile(default),provided,runtime,test,system
  3. exclusions

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

比如父pom.xml:

  1. <project>  
  2.   [...]  
  3.   <dependencies>  
  4.     <dependency>  
  5.       <groupId>junit</groupId>  
  6.       <artifactId>junit</artifactId>  
  7.       <version>4.4</version>  
  8.       <scope>test</scope>  
  9.     </dependency>  
  10.   </dependencies>  
  11.   [...]  
  12. </project>  
<project>
  [...]
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.4</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  [...]
</project>
在子pom.xml文件继承它的依赖(还可以继承其他的:developers and contributors、plugin lists、reports lists、plugin executions with matching ids、plugin configuration):
  1. [...]  
  2. <parent>  
  3. <groupId>com.devzuz.mvnbook.proficio</groupId>  
  4.   <artifactId>proficio</artifactId>  
  5.   <version>1.0-SNAPSHOT</version>  
  6. </parent>  
  7. [...]  
[...]
<parent>
<groupId>com.devzuz.mvnbook.proficio</groupId>
  <artifactId>proficio</artifactId>
  <version>1.0-SNAPSHOT</version>
</parent>
[...]
在这种机制下,maven还提供了一个类似java.lang.Object的顶级父pom.xml文件:
  1. <project>  
  2.   <modelVersion>4.0.0</modelVersion>  
  3.   <name>Maven Default Project</name>  
  4.   <repositories>  
  5.     <repository>  
  6.       <id>central</id>  
  7.       <name>Maven Repository Switchboard</name>  
  8.       <layout>default</layout>  
  9.       <url>http://repo1.maven.org/maven2</url>  
  10.       <snapshots>  
  11.         <enabled>false</enabled>  
  12.       </snapshots>  
  13.     </repository>  
  14.   </repositories>  
  15.   <pluginRepositories>  
  16.     <pluginRepository>  
  17.       <id>central</id>  
  18.       <name>Maven Plugin Repository</name>  
  19.       <url>http://repo1.maven.org/maven2</url>  
  20.       <layout>default</layout>  
  21.       <snapshots>  
  22.         <enabled>false</enabled>  
  23.       </snapshots>  
  24.       <releases>  
  25.         <updatePolicy>never</updatePolicy>  
  26.       </releases>  
  27.     </pluginRepository>  
  28.   </pluginRepositories>  
  29.   <build>  
  30.     <directory>target</directory>  
  31.     <outputDirectory>target/classes</outputDirectory>  
  32.     <finalName>${project.artifactId}-${project.version}</finalName>  
  33.     <testOutputDirectory>target/test-classes</testOutputDirectory>  
  34.     <sourceDirectory>src/main/java</sourceDirectory>  
  35.     <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>  
  36.     <testSourceDirectory>src/test/java</testSourceDirectory>  
  37.     <resources>  
  38.       <resource>  
  39.         <directory>src/main/resources</directory>  
  40.       </resource>  
  41.     </resources>  
  42.     <testResources>  
  43.       <testResource>  
  44.         <directory>src/test/resources</directory>  
  45.       </testResource>  
  46.     </testResources>  
  47.     <pluginManagement>  
  48.        <plugins>  
  49.          <plugin>  
  50.            <artifactId>maven-antrun-plugin</artifactId>  
  51.            <version>1.1</version>  
  52.          </plugin>        
  53.          <plugin>  
  54.            <artifactId>maven-assembly-plugin</artifactId>  
  55.            <version>2.2-beta-2</version>  
  56.          </plugin>  
  57.          <plugin>  
  58.            <artifactId>maven-clean-plugin</artifactId>  
  59.            <version>2.2</version>  
  60.          </plugin>  
  61.          <plugin>  
  62.            <artifactId>maven-compiler-plugin</artifactId>  
  63.            <version>2.0.2</version>  
  64.          </plugin>  
  65.          <plugin>  
  66.            <artifactId>maven-dependency-plugin</artifactId>  
  67.            <version>2.0</version>  
  68.          </plugin>  
  69.          <plugin>  
  70.            <artifactId>maven-deploy-plugin</artifactId>  
  71.            <version>2.3</version>  
  72.          </plugin>  
  73.          <plugin>  
  74.            <artifactId>maven-ear-plugin</artifactId>  
  75.            <version>2.3.1</version>  
  76.          </plugin>  
  77.          <plugin>  
  78.            <artifactId>maven-ejb-plugin</artifactId>  
  79.            <version>2.1</version>  
  80.          </plugin>  
  81.          <plugin>  
  82.            <artifactId>maven-install-plugin</artifactId>  
  83.            <version>2.2</version>  
  84.          </plugin>  
  85.          <plugin>  
  86.            <artifactId>maven-jar-plugin</artifactId>  
  87.            <version>2.2</version>  
  88.          </plugin>  
  89.          <plugin>  
  90.            <artifactId>maven-javadoc-plugin</artifactId>  
  91.            <version>2.4</version>  
  92.          </plugin>  
  93.          <plugin>  
  94.            <artifactId>maven-plugin-plugin</artifactId>  
  95.            <version>2.4.1</version>  
  96.          </plugin>  
  97.          <plugin>  
  98.            <artifactId>maven-rar-plugin</artifactId>  
  99.            <version>2.2</version>  
  100.          </plugin>  
  101.          <plugin>                 
  102.            <artifactId>maven-release-plugin</artifactId>  
  103.            <version>2.0-beta-7</version>  
  104.          </plugin>  
  105.          <plugin>                 
  106.            <artifactId>maven-resources-plugin</artifactId>  
  107.            <version>2.2</version>  
  108.          </plugin>  
  109.          <plugin>  
  110.            <artifactId>maven-site-plugin</artifactId>  
  111.            <version>2.0-beta-6</version>  
  112.          </plugin>  
  113.          <plugin>  
  114.            <artifactId>maven-source-plugin</artifactId>  
  115.            <version>2.0.4</version>  
  116.          </plugin>           
  117.          <plugin>  
  118.             <artifactId>maven-surefire-plugin</artifactId>  
  119.             <version>2.4.2</version>  
  120.          </plugin>  
  121.          <plugin>  
  122.            <artifactId>maven-war-plugin</artifactId>  
  123.            <version>2.1-alpha-1</version>  
  124.          </plugin>  
  125.        </plugins>  
  126.      </pluginManagement>  
  127.   </build>  
  128.   <reporting>  
  129.     <outputDirectory>target/site</outputDirectory>  
  130.   </reporting>  
  131.   <profiles>  
  132.     <profile>  
  133.       <id>release-profile</id>  
  134.       <activation>  
  135.         <property>  
  136.           <name>performRelease</name>  
  137.           <value>true</value>  
  138.         </property>  
  139.       </activation>  
  140.       <build>  
  141.         <plugins>  
  142.           <plugin>  
  143.             <inherited>true</inherited>  
  144.             <groupId>org.apache.maven.plugins</groupId>  
  145.             <artifactId>maven-source-plugin</artifactId>  
  146.             <executions>  
  147.               <execution>  
  148.                 <id>attach-sources</id>  
  149.                 <goals>  
  150.                   <goal>jar</goal>  
  151.                 </goals>  
  152.               </execution>  
  153.             </executions>  
  154.           </plugin>  
  155.           <plugin>  
  156.             <inherited>true</inherited>  
  157.             <groupId>org.apache.maven.plugins</groupId>  
  158.             <artifactId>maven-javadoc-plugin</artifactId>  
  159.             <executions>  
  160.               <execution>  
  161.                 <id>attach-javadocs</id>  
  162.                 <goals>  
  163.                   <goal>jar</goal>  
  164.                 </goals>  
  165.               </execution>  
  166.             </executions>  
  167.           </plugin>  
  168.           <plugin>  
  169.             <inherited>true</inherited>  
  170.             <groupId>org.apache.maven.plugins</groupId>  
  171.             <artifactId>maven-deploy-plugin</artifactId>  
  172.             <configuration>  
  173.               <updateReleaseInfo>true</updateReleaseInfo>  
  174.             </configuration>  
  175.           </plugin>  
  176.         </plugins>  
  177.       </build>  
  178.     </profile>  
  179.   </profiles>  
  180. </project>  
<project>
  <modelVersion>4.0.0</modelVersion>
  <name>Maven Default Project</name>
  <repositories>
    <repository>
      <id>central</id>
      <name>Maven Repository Switchboard</name>
      <layout>default</layout>
      <url>http://repo1.maven.org/maven2</url>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <id>central</id>
      <name>Maven Plugin Repository</name>
      <url>http://repo1.maven.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
    </pluginRepository>
  </pluginRepositories>
  <build>
    <directory>target</directory>
    <outputDirectory>target/classes</outputDirectory>
    <finalName>${project.artifactId}-${project.version}</finalName>
    <testOutputDirectory>target/test-classes</testOutputDirectory>
    <sourceDirectory>src/main/java</sourceDirectory>
    <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>
    <testSourceDirectory>src/test/java</testSourceDirectory>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>src/test/resources</directory>
      </testResource>
    </testResources>
    <pluginManagement>
       <plugins>
         <plugin>
           <artifactId>maven-antrun-plugin</artifactId>
           <version>1.1</version>
         </plugin>      
         <plugin>
           <artifactId>maven-assembly-plugin</artifactId>
           <version>2.2-beta-2</version>
         </plugin>
         <plugin>
           <artifactId>maven-clean-plugin</artifactId>
           <version>2.2</version>
         </plugin>
         <plugin>
           <artifactId>maven-compiler-plugin</artifactId>
           <version>2.0.2</version>
         </plugin>
         <plugin>
           <artifactId>maven-dependency-plugin</artifactId>
           <version>2.0</version>
         </plugin>
         <plugin>
           <artifactId>maven-deploy-plugin</artifactId>
           <version>2.3</version>
         </plugin>
         <plugin>
           <artifactId>maven-ear-plugin</artifactId>
           <version>2.3.1</version>
         </plugin>
         <plugin>
           <artifactId>maven-ejb-plugin</artifactId>
           <version>2.1</version>
         </plugin>
         <plugin>
           <artifactId>maven-install-plugin</artifactId>
           <version>2.2</version>
         </plugin>
         <plugin>
           <artifactId>maven-jar-plugin</artifactId>
           <version>2.2</version>
         </plugin>
         <plugin>
           <artifactId>maven-javadoc-plugin</artifactId>
           <version>2.4</version>
         </plugin>
         <plugin>
           <artifactId>maven-plugin-plugin</artifactId>
           <version>2.4.1</version>
         </plugin>
         <plugin>
           <artifactId>maven-rar-plugin</artifactId>
           <version>2.2</version>
         </plugin>
         <plugin>               
           <artifactId>maven-release-plugin</artifactId>
           <version>2.0-beta-7</version>
         </plugin>
         <plugin>               
           <artifactId>maven-resources-plugin</artifactId>
           <version>2.2</version>
         </plugin>
         <plugin>
           <artifactId>maven-site-plugin</artifactId>
           <version>2.0-beta-6</version>
         </plugin>
         <plugin>
           <artifactId>maven-source-plugin</artifactId>
           <version>2.0.4</version>
         </plugin>         
         <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.4.2</version>
         </plugin>
         <plugin>
           <artifactId>maven-war-plugin</artifactId>
           <version>2.1-alpha-1</version>
         </plugin>
       </plugins>
     </pluginManagement>
  </build>
  <reporting>
    <outputDirectory>target/site</outputDirectory>
  </reporting>
  <profiles>
    <profile>
      <id>release-profile</id>
      <activation>
        <property>
          <name>performRelease</name>
          <value>true</value>
        </property>
      </activation>
      <build>
        <plugins>
          <plugin>
            <inherited>true</inherited>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <executions>
              <execution>
                <id>attach-sources</id>
                <goals>
                  <goal>jar</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <inherited>true</inherited>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <executions>
              <execution>
                <id>attach-javadocs</id>
                <goals>
                  <goal>jar</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <inherited>true</inherited>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-deploy-plugin</artifactId>
            <configuration>
              <updateReleaseInfo>true</updateReleaseInfo>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
</project>
可以通过下面命令查看当前pom.xml受到超pom.xml文件的影响:
mvn help:effective-pom

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

  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.0</modelVersion>  
  6.   <groupId>org.codehaus.mojo</groupId>  
  7.   <artifactId>my-parent</artifactId>  
  8.   <version>2.0</version>  
  9.   <modules>  
  10.     <module>my-project<module>  
  11.   </modules>  
  12. </project>  
<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>my-parent</artifactId>
  <version>2.0</version>
  <modules>
    <module>my-project<module>
  </modules>
</project>

3、属性

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

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

4、构建设置

构建有两种build标签:

  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.   <!– "Project Build" contains more elements than just the BaseBuild set –>  
  7.   <build></build>  
  8.   <profiles>  
  9.     <profile>  
  10.       <!– "Profile Build" contains a subset of "Project Build"s elements –>  
  11.       <build></build>  
  12.     </profile>  
  13.   </profiles>  
  14. </project>  
<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/maven-v4_0_0.xsd">
  …
  <!– "Project Build" contains more elements than just the BaseBuild set –>
  <build>…</build>
  <profiles>
    <profile>
      <!– "Profile Build" contains a subset of "Project Build"s elements –>
      <build>…</build>
    </profile>
  </profiles>
</project>

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

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

  1. <resources>  
  2.   <resource>  
  3.     <targetPath>META-INF/plexus</targetPath>  
  4.     <filtering>false</filtering>  
  5.     <directory>${basedir}/src/main/plexus</directory>  
  6.     <includes>  
  7.       <include>configuration.xml</include>  
  8.     </includes>  
  9.     <excludes>  
  10.       <exclude>**/*.properties</exclude>  
  11.     </excludes>  
  12.   </resource>  
  13. </resources>  
    <resources>
      <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>
    </resources>
Plugins:设置构建的插件
  1. <build>  
  2.    …  
  3.    <plugins>  
  4.      <plugin>  
  5.        <groupId>org.apache.maven.plugins</groupId>  
  6.        <artifactId>maven-jar-plugin</artifactId>  
  7.        <version>2.0</version>  
  8.        <extensions>false</extensions>  
  9.        <inherited>true</inherited>  
  10.        <configuration>  
  11.          <classifier>test</classifier>  
  12.        </configuration>  
  13.        <dependencies></dependencies>  
  14.        <executions></executions>  
  15.      </plugin>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值