zoukankan      html  css  js  c++  java
  • 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>  

    二、基本设置

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

    在子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. [...]  

    在这种机制下,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>  

    可以通过下面命令查看当前pom.xml受到超pom.xml文件的影响:
    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>  


    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>  

    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>  

    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
  • 相关阅读:
    LeetCode 24. Swap Nodes in Pairs (两两交换链表中的节点)
    LeetCode 1041. Robot Bounded In Circle (困于环中的机器人)
    LeetCode 1037. Valid Boomerang (有效的回旋镖)
    LeetCode 1108. Defanging an IP Address (IP 地址无效化)
    LeetCode 704. Binary Search (二分查找)
    LeetCode 744. Find Smallest Letter Greater Than Target (寻找比目标字母大的最小字母)
    LeetCode 852. Peak Index in a Mountain Array (山脉数组的峰顶索引)
    LeetCode 817. Linked List Components (链表组件)
    LeetCode 1019. Next Greater Node In Linked List (链表中的下一个更大节点)
    29. Divide Two Integers
  • 原文地址:https://www.cnblogs.com/yasepix/p/6565007.html
Copyright © 2011-2022 走看看