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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM