Maven POM

  |   0 评论   |   0 浏览

什么是POM

POM( Project Object Model,项目对象模型 ) ,顾名思义它就是描述项目的一种模型,是Maven的基本工作单元。
该模型采用的是XML文件形式,我们在XML文件中编写项目的配置信息,Maven根据这些信息构建项目。
执行任务或目标时,Maven会在当前目录中查找并读取POM,获取所需的配置信息,然后执行目标。

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

 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  <groupId>org.codehaus.mojo</groupId><!-- 公司或者组织的唯一标志 -->
 8  <artifactId>my-project</artifactId> <!-- 项目的唯一ID,一个groupId下面可能多个项目 -->
 9  <version>1.0</version><!--项目版本号-->
10</project>

上面的三个元素groupId:artifactId:version,称之为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/xsd/maven-4.0.0.xsd">
5  ...
6  <packaging>war</packaging>
7  ...
8</project>

指定项目的打包方式,默认值是jar

关系配置

包括:依赖关系、继承和聚合(多模块)

依赖关系

 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.12</version>
11      <type>jar</type>
12      <scope>test</scope>
13      <optional>true</optional>
14    </dependency>
15    ...
16  </dependencies>
17  ...
18</project>

一个项目通常会依赖很多其他项目,在dependencies元素中指定依赖项目。

继承关系

 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  <modelVersion>4.0.0</modelVersion>
 6 
 7  <groupId>org.codehaus.mojo</groupId>
 8  <artifactId>my-parent</artifactId>
 9  <version>2.0</version>
10  <packaging>pom</packaging>
11</project>

作为被继承的POM文件,<packaging>元素的值需指定为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                      https://maven.apache.org/xsd/maven-4.0.0.xsd">
 5  <modelVersion>4.0.0</modelVersion>
 6 
 7  <parent>
 8    <groupId>org.codehaus.mojo</groupId>
 9    <artifactId>my-parent</artifactId>
10    <version>2.0</version>
11    <relativePath>../my-parent</relativePath>
12  </parent>
13 
14  <artifactId>my-project</artifactId>
15</project>

POM文件通过<parent>元素指定其继承的父文件。

Super POM
Super POMMaven 的基础 POM,所有的 POM 都会继承Super POM
Super POM的存放位置:$MAVEN_HOME/lib/maven-model-builder-version.jar/org/apache/maven/model/pom-4.0.0.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                      https://maven.apache.org/xsd/maven-4.0.0.xsd">
 5  <modelVersion>4.0.0</modelVersion>
 6 
 7  <groupId>org.codehaus.mojo</groupId>
 8  <artifactId>my-parent</artifactId>
 9  <version>2.0</version>
10  <packaging>pom</packaging>
11 
12  <modules>
13    <module>my-project</module>
14    <module>another-project</module>
15    <module>third-project/pom-example.xml</module>
16  </modules>
17</project>

属性

 1<project>
 2  ...
 3  <properties>
 4    <mavenVersion>3.0</mavenVersion>
 5  </properties>
 6 
 7  <dependencies>
 8    <dependency>
 9      <groupId>org.apache.maven</groupId>
10      <artifactId>maven-artifact</artifactId>
11      <version>${mavenVersion}</version>
12    </dependency>
13    <dependency>
14      <groupId>org.apache.maven</groupId>
15      <artifactId>maven-core</artifactId>
16      <version>${mavenVersion}</version>
17    </dependency>
18  </dependencies>
19  ...
20</project>

<properties>中可以配置变量,在其它元素中使用${X}形式进行引用。

构建信息

与构建相关的两个标签分别是<build><reporting>

build

POM文件中有两种<build>

  • <project>下的<build>,在全局起作用
  • <profiles>下的<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                      https://maven.apache.org/xsd/maven-4.0.0.xsd">
 5  ...
 6  <!-- "Project Build" contains more elements than just the BaseBuild set -->
 7  <build>...</build>
 8 
 9  <profiles>
10    <profile>
11      <!-- "Profile Build" contains a subset of "Project Build"s elements -->
12      <build>...</build>
13    </profile>
14  </profiles>
15</project>

基本build元素

基本build元素既可以在<project>中配置,也可以在<profiles>配置。

1<build>
2  <defaultGoal>install</defaultGoal>
3  <directory>${basedir}/target</directory>
4  <finalName>${artifactId}-${version}</finalName>
5  <filters>
6    <filter>filters/filter1.properties</filter>
7  </filters>
8  ...
9</build>

Resources配置
用于包含或排除某些资源文件。

 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  <build>
 6    ...
 7    <resources>
 8      <resource>
 9        <targetPath>META-INF/plexus</targetPath>
10        <filtering>false</filtering>
11        <directory>${basedir}/src/main/plexus</directory>
12        <includes>
13          <include>configuration.xml</include>
14        </includes>
15        <excludes>
16          <exclude>**/*.properties</exclude>
17        </excludes>
18      </resource>
19    </resources>
20    <testResources>
21      ...
22    </testResources>
23    ...
24  </build>

Plugins配置
用于指定使用的插件。

 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  <build>
 6    ...
 7    <plugins>
 8      <plugin>
 9        <groupId>org.apache.maven.plugins</groupId>
10        <artifactId>maven-jar-plugin</artifactId>
11        <version>2.6</version>
12        <extensions>false</extensions>
13        <inherited>true</inherited>
14        <configuration>
15          <classifier>test</classifier>
16        </configuration>
17        <dependencies>...</dependencies>
18        <executions>...</executions>
19      </plugin>
20    </plugins>
21  </build>
22</project>

Plugin Management配置
Plugin Management的配置是为了继承,子POM可以继承父POMPlugin Management的配置。
父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                      https://maven.apache.org/xsd/maven-4.0.0.xsd">
 5  ...
 6  <build>
 7    ...
 8    <pluginManagement>
 9      <plugins>
10        <plugin>
11          <groupId>org.apache.maven.plugins</groupId>
12          <artifactId>maven-jar-plugin</artifactId>
13          <version>2.6</version>
14          <executions>
15            <execution>
16              <id>pre-process-classes</id>
17              <phase>compile</phase>
18              <goals>
19                <goal>jar</goal>
20              </goals>
21              <configuration>
22                <classifier>pre-process</classifier>
23              </configuration>
24            </execution>
25          </executions>
26        </plugin>
27      </plugins>
28    </pluginManagement>
29    ...
30  </build>
31</project>

在子POM中我们只需要配置<groupId><artifactId>

 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  <build>
 7    ...
 8    <plugins>
 9      <plugin>
10        <groupId>org.apache.maven.plugins</groupId>
11        <artifactId>maven-jar-plugin</artifactId>
12      </plugin>
13    </plugins>
14    ...
15  </build>
16</project>

其它build元素

Profile Build包含了基本的build元素,而Project Build还包含两个特殊的元素,即各种<...Directory>和
Directories

 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  <build>
 7    <sourceDirectory>${basedir}/src/main/java</sourceDirectory>
 8    <scriptSourceDirectory>${basedir}/src/main/scripts</scriptSourceDirectory>
 9    <testSourceDirectory>${basedir}/src/test/java</testSourceDirectory>
10    <outputDirectory>${basedir}/target/classes</outputDirectory>
11    <testOutputDirectory>${basedir}/target/test-classes</testOutputDirectory>
12    ...
13  </build>
14</project>

Extensions

 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  <build>
 7    ...
 8    <extensions>
 9      <extension>
10        <groupId>org.apache.maven.wagon</groupId>
11        <artifactId>wagon-ftp</artifactId>
12        <version>1.0-alpha-3</version>
13      </extension>
14    </extensions>
15    ...
16  </build>
17</project>

reporting

<reporting>中的配置作用于Mavensite阶段,用于生成报表。。<reporting>中也可以配置插件<plugins>,并通过一个<plugin><reportSet>为该插件配置参数。

 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  <reporting>
 7    <outputDirectory>${basedir}/target/site</outputDirectory>
 8    <plugins>
 9      <plugin>
10        <artifactId>maven-project-info-reports-plugin</artifactId>
11        <version>2.0.1</version>
12        <reportSets>
13          <reportSet></reportSet>
14        </reportSets>
15      </plugin>
16    </plugins>
17  </reporting>
18  ...
19</project>

更多项目信息

配置许可、开发者、开发机构组织等信息。

许可信息

1<licenses>
2  <license>
3    <name>Apache License, Version 2.0</name>
4    <url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
5    <distribution>repo</distribution>
6    <comments>A business-friendly OSS license</comments>
7  </license>
8</licenses>

组织信息

1  <organization>
2    <name>Codehaus Mojo</name>
3    <url>http://mojo.codehaus.org</url>
4  </organization>

开发者信息

 1  <developers>
 2    <developer>
 3      <id>jdoe</id>
 4      <name>John Doe</name>
 5      <email>jdoe@example.com</email>
 6      <url>http://www.example.com/jdoe</url>
 7      <organization>ACME</organization>
 8      <organizationUrl>http://www.example.com</organizationUrl>
 9      <roles>
10        <role>architect</role>
11        <role>developer</role>
12      </roles>
13      <timezone>America/New_York</timezone>
14      <properties>
15        <picUrl>http://www.example.com/jdoe/pic</picUrl>
16      </properties>
17    </developer>
18  </developers>

贡献者信息

 1  <contributors>
 2    <contributor>
 3      <name>Noelle</name>
 4      <email>some.name@gmail.com</email>
 5      <url>http://noellemarie.com</url>
 6      <organization>Noelle Marie</organization>
 7      <organizationUrl>http://noellemarie.com</organizationUrl>
 8      <roles>
 9        <role>tester</role>
10      </roles>
11      <timezone>America/Vancouver</timezone>
12      <properties>
13        <gtalk>some.name@gmail.com</gtalk>
14      </properties>
15    </contributor>
16  </contributors>

环境配置

Issue Management

 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  <issueManagement>
 7    <system>Bugzilla</system>
 8    <url>http://127.0.0.1/bugzilla/</url>
 9  </issueManagement>
10  ...
11</project>

Continuous Integration Management

 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  <ciManagement>
 7    <system>continuum</system>
 8    <url>http://127.0.0.1:8080/continuum</url>
 9    <notifiers>
10      <notifier>
11        <type>mail</type>
12        <sendOnError>true</sendOnError>
13        <sendOnFailure>true</sendOnFailure>
14        <sendOnSuccess>false</sendOnSuccess>
15        <sendOnWarning>false</sendOnWarning>
16        <configuration><address>continuum@127.0.0.1</address></configuration>
17      </notifier>
18    </notifiers>
19  </ciManagement>
20  ...
21</project>

Mailing Lists

 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  <mailingLists>
 7    <mailingList>
 8      <name>User List</name>
 9      <subscribe>user-subscribe@127.0.0.1</subscribe>
10      <unsubscribe>user-unsubscribe@127.0.0.1</unsubscribe>
11      <post>user@127.0.0.1</post>
12      <archive>http://127.0.0.1/user/</archive>
13      <otherArchives>
14        <otherArchive>http://base.google.com/base/1/127.0.0.1</otherArchive>
15      </otherArchives>
16    </mailingList>
17  </mailingLists>
18  ...
19</project>

SCM

 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  <scm>
 7    <connection>scm:svn:http://127.0.0.1/svn/my-project</connection>
 8    <developerConnection>scm:svn:https://127.0.0.1/svn/my-project</developerConnection>
 9    <tag>HEAD</tag>
10    <url>http://127.0.0.1/websvn/my-project</url>
11  </scm>
12  ...
13</project>

Prerequisites

 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  <prerequisites>
 7    <maven>2.0.6</maven>
 8  </prerequisites>
 9  ...
10</project>

仓库 Repositories

 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  <repositories>
 7    <repository>
 8      <releases>
 9        <enabled>false</enabled>
10        <updatePolicy>always</updatePolicy>
11        <checksumPolicy>warn</checksumPolicy>
12      </releases>
13      <snapshots>
14        <enabled>true</enabled>
15        <updatePolicy>never</updatePolicy>
16        <checksumPolicy>fail</checksumPolicy>
17      </snapshots>
18      <name>Nexus Snapshots</name>
19      <id>snapshots-repo</id>
20      <url>https://oss.sonatype.org/content/repositories/snapshots</url>
21      <layout>default</layout>
22    </repository>
23  </repositories>
24  <pluginRepositories>
25    ...
26  </pluginRepositories>
27  ...
28</project>

Distribution Management

 1<project xmlns="http://maven.apache.org/POM/4.0.0"
 2  xmlns:xsi="http://www.w3.org/001/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  <distributionManagement>
 7    ...
 8    <downloadUrl>http://mojo.codehaus.org/my-project</downloadUrl>
 9    <status>deployed</status>
10  </distributionManagement>
11  ...
12</project>

Profiles

Profiles POM 4.0的新特性,用于在不同的环境下应用不同的配置。

 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  <profiles>
 7    <profile>
 8      <id>test</id>
 9      <activation>...</activation> <!--配置profile在什么情况下生效-->
10      <build>...</build>
11      <modules>...</modules>
12      <repositories>...</repositories>
13      <pluginRepositories>...</pluginRepositories>
14      <dependencies>...</dependencies>
15      <reporting>...</reporting>
16      <dependencyManagement>...</dependencyManagement>
17      <distributionManagement>...</distributionManagement>
18    </profile>
19  </profiles>
20</project>

相关资料

Introduction to the POM
POM Reference