什么是 POM?

POM (Project Object Model) 项目对象模型。它是一个XML文件,其中包含有关Maven用于构建项目的项目和配置细节的信息。它包含大多数项目的默认值。例如,构建项目的目录:target;java源码文件目录: src/main/java;测试java源码文件目录: src/test/java;等等。当执行任务或目标时,Maven将在当前目录中查找POM,它读取POM。获取所需要的配置信息,然后执行目标。

POM中可以指定项目的依赖,可以执行的插件或目标,构建配置文件等。其他信息,如项目版本,说明,开发人员,邮件列表等也可以指定。

POM 结构

这是直接在POM的项目元素下列出的元素。注意modelVersion为4.0.0这是目前唯一支持Maven 2和3的POM版本,并且是必需的。

 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/xsd/maven-4.0.0.xsd">
 5   <modelVersion>4.0.0</modelVersion>
 6  
 7   <!-- The Basics -->
 8   <groupId>...</groupId>
 9   <artifactId>...</artifactId>
10   <version>...</version>
11   <packaging>...</packaging>
12   <dependencies>...</dependencies>
13   <parent>...</parent>
14   <dependencyManagement>...</dependencyManagement>
15   <modules>...</modules>
16   <properties>...</properties>
17  
18   <!-- Build Settings -->
19   <build>...</build>
20   <reporting>...</reporting>
21  
22   <!-- More Project Information -->
23   <name>...</name>
24   <description>...</description>
25   <url>...</url>
26   <inceptionYear>...</inceptionYear>
27   <licenses>...</licenses>
28   <organization>...</organization>
29   <developers>...</developers>
30   <contributors>...</contributors>
31  
32   <!-- Environment Settings -->
33   <issueManagement>...</issueManagement>
34   <ciManagement>...</ciManagement>
35   <mailingLists>...</mailingLists>
36   <scm>...</scm>
37   <prerequisites>...</prerequisites>
38   <repositories>...</repositories>
39   <pluginRepositories>...</pluginRepositories>
40   <distributionManagement>...</distributionManagement>
41   <profiles>...</profiles>
42 </project>

Maven 坐标

groupId:artifactId:version三个字段是Maven必不可少的字段,这三个字段的作用非常类似与一个地址和时间戳。这标志着存储库中特定位置,就像Maven项目的坐标系统一样。

  • groupId: 这是组织或则项目独一无二的ID值。
  • artifactId: 是项目的名称,把groupId进行却分开来。
  • version: 指项目的当前版本。
  • packaging: 如果没有指定默认是jar,我们可以设置为pom, jar, maven-plugin, ejb, war, ear, rar, par等,如果设置为war则会将项目最终打包成war包。
  • classifier: 你有时候会在项目中看到这个熟悉,这样项目显示为groupId:artifactId:packaging:classifier:version。

Maven 依赖

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                       https://maven.apache.org/xsd/maven-4.0.0.xsd">
 5   ...
 6   <dependencies>
 7     <dependency>
 8       <groupId>junit</groupId>
 9       <artifactId>junit</artifactId>
10       <version>4.0</version>
11       <type>jar</type>
12       <scope>test</scope>
13       <optional>true</optional>
14     </dependency>
15     ...
16   </dependencies>
17   ...
18 </project>
  • groupId、artifactId、version: 如上一节所述,这里可以计算出项目的坐标,并将其引入到项目依赖中。
  • classifier: 这是一个可选参数。
  • type: 依赖项目的包装类型,默认为jar。
  • scope: 该元素用来引入到当前项目的类路径(编译、测试或运行等),以及限制依赖的传递性。总共有如下五个范围:
    • compile:默认值,编译期。依赖项可以在所有类路径中使用,且会传递到子项目。
    • provided:只可以在编译和测试中使用,切不会传递给子项目。
    • runtiome:在项目测试和运行类路径中使用,不会在编译中使用。
    • test:只可以在测试中使用,不会传递子项目。
    • system:这个和provided非常类似,工件始终可以使用,并且不会存储库中查找。
  • systemPath:只有当scope为system时,需要指定JAR的绝对路径。
  • optional:可选元素,如果optional为true时,如果A依赖B,B依赖D,如果A没有显示的引入D,则A不会依赖D。

exclusions可以显示的排出依赖项中传递的依赖项目。例如maven-embedder需要依赖maven-core,但我们不希望引入maven-core依赖项目,那么我们可以显示的排除这个项目。如下:

 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                       https://maven.apache.org/xsd/maven-4.0.0.xsd">
 5   ...
 6   <dependencies>
 7     <dependency>
 8       <groupId>org.apache.maven</groupId>
 9       <artifactId>maven-embedder</artifactId>
10       <version>2.0</version>
11       <exclusions>
12         <exclusion>
13           <groupId>org.apache.maven</groupId>
14           <artifactId>maven-core</artifactId>
15         </exclusion>
16       </exclusions>
17     </dependency>
18     ...
19   </dependencies>
20   ...
21 </project>

这样你的项目中不会有maven-core依赖项。

Super POM

Super POM 是Maven默认的POM,除非明确设置,否则所有的POM都会扩展Super POM,这意味着您为项目创建的POM继承Super POM。下面的代码是Maven 2.0.x 的 Super 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>
View Code

相关文章: