zoukankan      html  css  js  c++  java
  • Maven的POM简单理解

    以下引用自官方的POM介绍https://maven.apache.org/guides/introduction/introduction-to-the-pom.html

    一、什么是POM?

    项目对象模型或POM是Maven的基本工作单元。它是一个XML文件,其中包含有关Maven用于构建项目的项目和配置详细信息。它包含大多数项目的默认值。示例是构建目录,即target;这是源目录src/main/java;测试源目录src/test/java;等等。

    POM已从Maven 1中的project.xml重命名为Maven 2中的pom.xml。而不是具有包含可执行目标的maven.xml文件,目标或插件现在已在pom.xml中配置。执行任务或目标时,Maven会在当前目录中查找POM。它读取POM,获取所需的配置信息,然后执行目标。

    可以在POM中指定的一些配置是项目依赖性,可执行的插件或目标,构建配置文件等。还可以指定其他信息,如项目版本,描述,开发人员,邮件列表等。

    二、超级POM

    超级POM是Maven的默认POM。所有POM扩展超级POM,除非明确设置,这意味着超级POM中指定的配置由您为项目创建的POM继承。下面的代码片段是Maven 2.0.x的超级POM。

      1 <project>
      2   <modelVersion>4.0.0</modelVersion>
      3   <name>Maven Default Project</name>
      4  
      5   <repositories>
      6     <repository>
      7       <id>central</id>
      8       <name>Maven Repository Switchboard</name>
      9       <layout>default</layout>
     10       <url>http://repo1.maven.org/maven2</url>
     11       <snapshots>
     12         <enabled>false</enabled>
     13       </snapshots>
     14     </repository>
     15   </repositories>
     16  
     17   <pluginRepositories>
     18     <pluginRepository>
     19       <id>central</id>
     20       <name>Maven Plugin Repository</name>
     21       <url>http://repo1.maven.org/maven2</url>
     22       <layout>default</layout>
     23       <snapshots>
     24         <enabled>false</enabled>
     25       </snapshots>
     26       <releases>
     27         <updatePolicy>never</updatePolicy>
     28       </releases>
     29     </pluginRepository>
     30   </pluginRepositories>
     31  
     32   <build>
     33     <directory>target</directory>
     34     <outputDirectory>target/classes</outputDirectory>
     35     <finalName>${artifactId}-${version}</finalName>
     36     <testOutputDirectory>target/test-classes</testOutputDirectory>
     37     <sourceDirectory>src/main/java</sourceDirectory>
     38     <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>
     39     <testSourceDirectory>src/test/java</testSourceDirectory>
     40     <resources>
     41       <resource>
     42         <directory>src/main/resources</directory>
     43       </resource>
     44     </resources>
     45     <testResources>
     46       <testResource>
     47         <directory>src/test/resources</directory>
     48       </testResource>
     49     </testResources>
     50   </build>
     51  
     52   <reporting>
     53     <outputDirectory>target/site</outputDirectory>
     54   </reporting>
     55  
     56   <profiles>
     57     <profile>
     58       <id>release-profile</id>
     59  
     60       <activation>
     61         <property>
     62           <name>performRelease</name>
     63         </property>
     64       </activation>
     65  
     66       <build>
     67         <plugins>
     68           <plugin>
     69             <inherited>true</inherited>
     70             <groupId>org.apache.maven.plugins</groupId>
     71             <artifactId>maven-source-plugin</artifactId>
     72  
     73             <executions>
     74               <execution>
     75                 <id>attach-sources</id>
     76                 <goals>
     77                   <goal>jar</goal>
     78                 </goals>
     79               </execution>
     80             </executions>
     81           </plugin>
     82           <plugin>
     83             <inherited>true</inherited>
     84             <groupId>org.apache.maven.plugins</groupId>
     85             <artifactId>maven-javadoc-plugin</artifactId>
     86  
     87             <executions>
     88               <execution>
     89                 <id>attach-javadocs</id>
     90                 <goals>
     91                   <goal>jar</goal>
     92                 </goals>
     93               </execution>
     94             </executions>
     95           </plugin>
     96           <plugin>
     97             <inherited>true</inherited>
     98             <groupId>org.apache.maven.plugins</groupId>
     99             <artifactId>maven-deploy-plugin</artifactId>
    100  
    101             <configuration>
    102               <updateReleaseInfo>true</updateReleaseInfo>
    103             </configuration>
    104           </plugin>
    105         </plugins>
    106       </build>
    107     </profile>
    108   </profiles>
    109  
    110 </project>

    下面的代码片段是Maven 2.1.x的超级POM。

      1 <project>
      2   <modelVersion>4.0.0</modelVersion>
      3   <name>Maven Default Project</name>
      4  
      5   <repositories>
      6     <repository>
      7       <id>central</id>
      8       <name>Maven Repository Switchboard</name>
      9       <layout>default</layout>
     10       <url>http://repo1.maven.org/maven2</url>
     11       <snapshots>
     12         <enabled>false</enabled>
     13       </snapshots>
     14     </repository>
     15   </repositories>
     16  
     17   <pluginRepositories>
     18     <pluginRepository>
     19       <id>central</id>
     20       <name>Maven Plugin Repository</name>
     21       <url>http://repo1.maven.org/maven2</url>
     22       <layout>default</layout>
     23       <snapshots>
     24         <enabled>false</enabled>
     25       </snapshots>
     26       <releases>
     27         <updatePolicy>never</updatePolicy>
     28       </releases>
     29     </pluginRepository>
     30   </pluginRepositories>
     31  
     32   <build>
     33     <directory>${project.basedir}/target</directory>
     34     <outputDirectory>${project.build.directory}/classes</outputDirectory>
     35     <finalName>${project.artifactId}-${project.version}</finalName>
     36     <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
     37     <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
     38     <!-- TODO: MNG-3731 maven-plugin-tools-api < 2.4.4 expect this to be relative... -->
     39     <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>
     40     <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
     41     <resources>
     42       <resource>
     43         <directory>${project.basedir}/src/main/resources</directory>
     44       </resource>
     45     </resources>
     46     <testResources>
     47       <testResource>
     48         <directory>${project.basedir}/src/test/resources</directory>
     49       </testResource>
     50     </testResources>
     51    <pluginManagement>
     52        <plugins>
     53          <plugin>
     54            <artifactId>maven-antrun-plugin</artifactId>
     55            <version>1.3</version>
     56          </plugin>       
     57          <plugin>
     58            <artifactId>maven-assembly-plugin</artifactId>
     59            <version>2.2-beta-2</version>
     60          </plugin>         
     61          <plugin>
     62            <artifactId>maven-clean-plugin</artifactId>
     63            <version>2.2</version>
     64          </plugin>
     65          <plugin>
     66            <artifactId>maven-compiler-plugin</artifactId>
     67            <version>2.0.2</version>
     68          </plugin>
     69          <plugin>
     70            <artifactId>maven-dependency-plugin</artifactId>
     71            <version>2.0</version>
     72          </plugin>
     73          <plugin>
     74            <artifactId>maven-deploy-plugin</artifactId>
     75            <version>2.4</version>
     76          </plugin>
     77          <plugin>
     78            <artifactId>maven-ear-plugin</artifactId>
     79            <version>2.3.1</version>
     80          </plugin>
     81          <plugin>
     82            <artifactId>maven-ejb-plugin</artifactId>
     83            <version>2.1</version>
     84          </plugin>
     85          <plugin>
     86            <artifactId>maven-install-plugin</artifactId>
     87            <version>2.2</version>
     88          </plugin>
     89          <plugin>
     90            <artifactId>maven-jar-plugin</artifactId>
     91            <version>2.2</version>
     92          </plugin>
     93          <plugin>
     94            <artifactId>maven-javadoc-plugin</artifactId>
     95            <version>2.5</version>
     96          </plugin>
     97          <plugin>
     98            <artifactId>maven-plugin-plugin</artifactId>
     99            <version>2.4.3</version>
    100          </plugin>
    101          <plugin>
    102            <artifactId>maven-rar-plugin</artifactId>
    103            <version>2.2</version>
    104          </plugin>        
    105          <plugin>                
    106            <artifactId>maven-release-plugin</artifactId>
    107            <version>2.0-beta-8</version>
    108          </plugin>
    109          <plugin>                
    110            <artifactId>maven-resources-plugin</artifactId>
    111            <version>2.3</version>
    112          </plugin>
    113          <plugin>
    114            <artifactId>maven-site-plugin</artifactId>
    115            <version>2.0-beta-7</version>
    116          </plugin>
    117          <plugin>
    118            <artifactId>maven-source-plugin</artifactId>
    119            <version>2.0.4</version>
    120          </plugin>         
    121          <plugin>
    122             <artifactId>maven-surefire-plugin</artifactId>
    123             <version>2.4.3</version>
    124          </plugin>
    125          <plugin>
    126            <artifactId>maven-war-plugin</artifactId>
    127            <version>2.1-alpha-2</version>
    128          </plugin>
    129        </plugins>
    130      </pluginManagement>
    131   </build>
    132  
    133   <reporting>
    134     <outputDirectory>${project.build.directory}/site</outputDirectory>
    135   </reporting>
    136   <profiles>
    137     <profile>
    138       <id>release-profile</id>
    139  
    140       <activation>
    141         <property>
    142           <name>performRelease</name>
    143           <value>true</value>
    144         </property>
    145       </activation>
    146  
    147       <build>
    148         <plugins>
    149           <plugin>
    150             <inherited>true</inherited>
    151             <groupId>org.apache.maven.plugins</groupId>
    152             <artifactId>maven-source-plugin</artifactId>
    153             <executions>
    154               <execution>
    155                 <id>attach-sources</id>
    156                 <goals>
    157                   <goal>jar</goal>
    158                 </goals>
    159               </execution>
    160             </executions>
    161           </plugin>
    162           <plugin>
    163             <inherited>true</inherited>
    164             <groupId>org.apache.maven.plugins</groupId>
    165             <artifactId>maven-javadoc-plugin</artifactId>
    166             <executions>
    167               <execution>
    168                 <id>attach-javadocs</id>
    169                 <goals>
    170                   <goal>jar</goal>
    171                 </goals>
    172               </execution>
    173             </executions>
    174           </plugin>
    175           <plugin>
    176             <inherited>true</inherited>
    177             <groupId>org.apache.maven.plugins</groupId>
    178             <artifactId>maven-deploy-plugin</artifactId>
    179             <configuration>
    180               <updateReleaseInfo>true</updateReleaseInfo>
    181             </configuration>
    182           </plugin>
    183         </plugins>
    184       </build>
    185     </profile>
    186   </profiles>
    187  
    188 </project>

    三、最小的POM要求

    POM的最低要求如下:

    • project root - 项目根
    • modelVersion - 应设置为4.0.0
    • groupId - 项目组的ID。(理解为包名)
    • artifactId - 工件的ID(项目)
    • version - 指定组下的工件的版本

    以下是一个例子:

    <project>
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.mycompany.app</groupId>
      <artifactId>my-app</artifactId>
      <version>1</version>
    </project>

    POM要求配置其groupIdartifactIdversion。这三个值形成项目的完全合格的工件名称。这是以<groupId>:<artifactId>:<version>的形式。对于上面的示例,其完全限定的工件名称为“com.mycompany.app:my-app:1”。

    此外,如上所提到的什么是POM,如果没有指定的配置细节,Maven将使用默认值。这些默认值之一是打包类型。每个Maven项目都有一个打包类型。如果在POM中没有指定,那么将使用默认值“jar”。

    此外,你可以看到,在最小POM,在库中未指定。如果您使用最小POM构建项目,它将继承超级POM中的存储库配置。因此,当看到Maven的在最小POM的依赖关系,它会知道这些依赖关系将在这里http://repo.maven.apache.org/maven2这是在超级POM中指定)被下载

    总结:

    1、pom.xml文件是Maven构建的基础,而一个标准的pom.xml文件中,最基本是由groupId、artifactId、version这三个字段组成;在创建pom.xml之后,必须确定这三个属性,因为这三个属性在项目仓库是作为唯一标识的。

    2、而对于三个属性的基本理解如下:

    groupId:命名空间,比如com.jsoft.test

    artivactId:项目名称,比如testproject

    version:版本号,比如1.0

    3、以下为标准的pom.xml包含的字段:

    <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/xsd/maven-4.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>

    提示:更详细的说明,参考官方解释:http://maven.apache.org/pom.html

    4、还有一个重要的点,一般项目分模块进行开发,那么在pom.xml中也有体现,比如一个总的pom.xml管理这每一个模块的pom.xml,这种做法叫做分模块。

    以上参考:http://www.yiibai.com/maven/maven_pom.html

  • 相关阅读:
    jquery的下拉选择框
    jquery动态导航三
    jquery--动态导航二
    jquery--动态导航
    jquery--图片轮番效果
    jquery方式的价格随数量增加、删除当前行与所有行
    解决sese9 安装时多个屏幕
    利用PowerCLI不重启系统更新VMware Tools
    VMware: Deploy multiple VM’s from template with PowerCLI
    vmware converter linux p2v lvm
  • 原文地址:https://www.cnblogs.com/EasonJim/p/6799972.html
Copyright © 2011-2022 走看看