Introduction to the POM

原文:https://maven.apache.org/guides/introduction/introduction-to-the-pom.html

Introduction to the POM

  • What is a POM?
  • Super POM
  • Minimal POM
  • Project Inheritance
    • Example 1
    • Example 2
  • Project Aggregation
    • Example 3
    • Example 4
  • Project Inheritance vs Project Aggregation
    • Example 5
  • Project Interpolation and Variables
    • Available Variables

What is a POM?

A Project Object Model or POM is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used by Maven to build the project. It contains default values for most projects. Examples for this is the build directory, which is target; the source directory, which is src/main/java; the test source directory, which is src/test/java; and so on.

The POM was renamed from project.xml in Maven 1 to pom.xml in Maven 2. Instead of having a maven.xml file that contains the goals that can be executed, the goals or plugins are now configured in the pom.xml. When executing a task or goal, Maven looks for the POM in the current directory. It reads the POM, gets the needed configuration information, then executes the goal.

Some of the configuration that can be specified in the POM are the project dependencies, the plugins or goals that can be executed, the build profiles, and so on. Other information such as the project version, description, developers, mailing lists and such can also be specified.

[top]

Super POM

The Super POM is Maven's default POM. All POMs extend the Super POM unless explicitly set, meaning the configuration specified in the Super POM is inherited by the POMs you created for your projects. The snippet below is the Super POM for Maven 2.0.x.

  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>

The snippet below is the Super POM for Maven 2.1.x.

  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>

[top]

Minimal POM

The minimum requirement for a POM are the following:

  • project root
  • modelVersion - should be set to 4.0.0
  • groupId - the id of the project's group.
  • artifactId - the id of the artifact (project)
  • version - the version of the artifact under the specified group

Here's an example:

  1. <project>
  2. <modelVersion>4.0.0</modelVersion>
  3. <groupId>com.mycompany.app</groupId>
  4. <artifactId>my-app</artifactId>
  5. <version>1</version>
  6. </project>

A POM requires that its groupId, artifactId, and version be configured. These three values form the project's fully qualified artifact name. This is in the form of <groupId>:<artifactId>:<version>. As for the example above, its fully qualified artifact name is "com.mycompany.app:my-app:1".

Also, as mentioned in the first section, if the configuration details are not specified, Maven will use their defaults. One of these default values is the packaging type. Every Maven project has a packaging type. If it is not specified in the POM, then the default value "jar" would be used.

Furthermore, as you can see that in the minimal POM, the repositories were not specified. If you build your project using the minimal POM, it would inherit therepositories configuration in the Super POM. Therefore when Maven sees the dependencies in the minimal POM, it would know that these dependencies will be downloaded from http://repo.maven.apache.org/maven2 which was specified in the Super POM.

[top]

Project Inheritance

Elements in the POM that are merged are the following:

  • dependencies
  • developers and contributors
  • plugin lists (including reports)
  • plugin executions with matching ids
  • plugin configuration
  • resources

The Super POM is one example of project inheritance, however you can also introduce your own parent POMs by specifying the parent element in the POM, as demonstrated in the following examples.

Example 1

The Scenario

As an example, let us reuse our previous artifact, com.mycompany.app:my-app:1. And let us introduce another artifact, com.mycompany.app:my-module:1.

  1. <project>
  2. <modelVersion>4.0.0</modelVersion>
  3. <groupId>com.mycompany.app</groupId>
  4. <artifactId>my-module</artifactId>
  5. <version>1</version>
  6. </project>

And let us specify their directory structure as the following:

  1. .
  2. |-- my-module
  3. | `-- pom.xml
  4. `-- pom.xml

Note: my-module/pom.xml is the POM of com.mycompany.app:my-module:1 while pom.xml is the POM of com.mycompany.app:my-app:1

The Solution

Now, if we were to turn com.mycompany.app:my-app:1 into a parent artifact of com.mycompany.app:my-module:1,we will have to modify com.mycompany.app:my-module:1's POM to the following configuration:

com.mycompany.app:my-module:1's POM

  1. <project>
  2. <parent>
  3. <groupId>com.mycompany.app</groupId>
  4. <artifactId>my-app</artifactId>
  5. <version>1</version>
  6. </parent>
  7. <modelVersion>4.0.0</modelVersion>
  8. <groupId>com.mycompany.app</groupId>
  9. <artifactId>my-module</artifactId>
  10. <version>1</version>
  11. </project>

Notice that we now have an added section, the parent section. This section allows us to specify which artifact is the parent of our POM. And we do so by specifying the fully qualified artifact name of the parent POM. With this setup, our module can now inherit some of the properties of our parent POM.

Alternatively, if we want the groupId and / or the version of your modules to be the same as their parents, you can remove the groupId and / or the version identity of your module in its POM.

  1. <project>
  2. <parent>
  3. <groupId>com.mycompany.app</groupId>
  4. <artifactId>my-app</artifactId>
  5. <version>1</version>
  6. </parent>
  7. <modelVersion>4.0.0</modelVersion>
  8. <artifactId>my-module</artifactId>
  9. </project>

This allows the module to inherit the groupId and / or the version of its parent POM.

[top]

Example 2

The Scenario

However, that would work if the parent project was already installed in our local repository or was in that specific directory structure (parent pom.xml is one directory higher than that of the module's pom.xml).

But what if the parent is not yet installed and if the directory structure is

  1. .
  2. |-- my-module
  3. | `-- pom.xml
  4. `-- parent
  5. `-- pom.xml
The Solution

To address this directory structure (or any other directory structure), we would have to add the <relativePath> element to our parent section.

  1. <project>
  2. <parent>
  3. <groupId>com.mycompany.app</groupId>
  4. <artifactId>my-app</artifactId>
  5. <version>1</version>
  6. <relativePath>../parent/pom.xml</relativePath>
  7. </parent>
  8. <modelVersion>4.0.0</modelVersion>
  9. <artifactId>my-module</artifactId>
  10. </project>

As the name suggests, it's the relative path from the module's pom.xml to the parent's pom.xml.

Project Aggregation

Project Aggregation is similar to Project Inheritance. But instead of specifying the parent POM from the module, it specifies the modules from the parent POM. By doing so, the parent project now knows its modules, and if a Maven command is invoked against the parent project, that Maven command will then be executed to the parent's modules as well. To do Project Aggregation, you must do the following:

  • Change the parent POMs packaging to the value "pom" .
  • Specify in the parent POM the directories of its modules (children POMs)

[top]

Example 3

The Scenario

Given the previous original artifact POMs, and directory structure,

com.mycompany.app:my-app:1's POM

  1. <project>
  2. <modelVersion>4.0.0</modelVersion>
  3. <groupId>com.mycompany.app</groupId>
  4. <artifactId>my-app</artifactId>
  5. <version>1</version>
  6. </project>

com.mycompany.app:my-module:1's POM

  1. <project>
  2. <modelVersion>4.0.0</modelVersion>
  3. <groupId>com.mycompany.app</groupId>
  4. <artifactId>my-module</artifactId>
  5. <version>1</version>
  6. </project>

directory structure

  1. .
  2. |-- my-module
  3. | `-- pom.xml
  4. `-- pom.xml
The Solution

If we are to aggregate my-module into my-app, we would only have to modify my-app.

  1. <project>
  2. <modelVersion>4.0.0</modelVersion>
  3. <groupId>com.mycompany.app</groupId>
  4. <artifactId>my-app</artifactId>
  5. <version>1</version>
  6. <packaging>pom</packaging>
  7.  
  8. <modules>
  9. <module>my-module</module>
  10. </modules>
  11. </project>

In the revised com.mycompany.app:my-app:1, the packaging section and the modules sections were added. For the packaging, its value was set to "pom", and for the modules section, we have the element <module>my-module</module>. The value of <module> is the relative path from the com.mycompany.app:my-app:1 to com.mycompany.app:my-module:1's POM (by practice, we use the module's artifactId as the module directory's name).

Now, whenever a Maven command processes com.mycompany.app:my-app:1, that same Maven command would be ran against com.mycompany.app:my-module:1 as well. Furthermore, some commands (goals specifically) handle project aggregation differently.

[top]

Example 4

The Scenario

But what if we change the directory structure to the following:

  1. .
  2. |-- my-module
  3. | `-- pom.xml
  4. `-- parent
  5. `-- pom.xml

How would the parent pom specify its modules?

The Solution

The answer? - the same way as Example 3, by specifying the path to the module.

  1. <project>
  2. <modelVersion>4.0.0</modelVersion>
  3. <groupId>com.mycompany.app</groupId>
  4. <artifactId>my-app</artifactId>
  5. <version>1</version>
  6. <packaging>pom</packaging>
  7.  
  8. <modules>
  9. <module>../my-module</module>
  10. </modules>
  11. </project>

Project Inheritance vs Project Aggregation

If you have several Maven projects, and they all have similar configurations, you can refactor your projects by pulling out those similar configurations and making a parent project. Thus, all you have to do is to let your Maven projects inherit that parent project, and those configurations would then be applied to all of them.

And if you have a group of projects that are built or processed together, you can create a parent project and have that parent project declare those projects as its modules. By doing so, you'd only have to build the parent and the rest will follow.

But of course, you can have both Project Inheritance and Project Aggregation. Meaning, you can have your modules specify a parent project, and at the same time, have that parent project specify those Maven projects as its modules. You'd just have to apply all three rules:

  • Specify in every child POM who their parent POM is.
  • Change the parent POMs packaging to the value "pom" .
  • Specify in the parent POM the directories of its modules (children POMs)

[top]

Example 5

The Scenario

Given the previous original artifact POMs again,

com.mycompany.app:my-app:1's POM

  1. <project>
  2. <modelVersion>4.0.0</modelVersion>
  3. <groupId>com.mycompany.app</groupId>
  4. <artifactId>my-app</artifactId>
  5. <version>1</version>
  6. </project>

com.mycompany.app:my-module:1's POM

  1. <project>
  2. <modelVersion>4.0.0</modelVersion>
  3. <groupId>com.mycompany.app</groupId>
  4. <artifactId>my-module</artifactId>
  5. <version>1</version>
  6. </project>

and this directory structure

  1. .
  2. |-- my-module
  3. | `-- pom.xml
  4. `-- parent
  5. `-- pom.xml
The Solution

To do both project inheritance and aggregation, you only have to apply all three rules.

com.mycompany.app:my-app:1's POM

  1. <project>
  2. <modelVersion>4.0.0</modelVersion>
  3. <groupId>com.mycompany.app</groupId>
  4. <artifactId>my-app</artifactId>
  5. <version>1</version>
  6. <packaging>pom</packaging>
  7.  
  8. <modules>
  9. <module>../my-module</module>
  10. </modules>
  11. </project>

com.mycompany.app:my-module:1's POM

  1. <project>
  2. <parent>
  3. <groupId>com.mycompany.app</groupId>
  4. <artifactId>my-app</artifactId>
  5. <version>1</version>
  6. <relativePath>../parent/pom.xml</relativePath>
  7. </parent>
  8. <modelVersion>4.0.0</modelVersion>
  9. <artifactId>my-module</artifactId>
  10. </project>

NOTE: Profile inheritance the same inheritance strategy as used for the POM itself.

[top]

Project Interpolation and Variables

One of the practices that Maven encourages is don't repeat yourself. However, there are circumstances where you will need to use the same value in several different locations. To assist in ensuring the value is only specified once, Maven allows you to use both your own and pre-defined variables in the POM.

For example, to access the project.version variable, you would reference it like so:

  1. <version>${project.version}</version>

One factor to note is that these variables are processed after inheritance as outlined above. This means that if a parent project uses a variable, then its definition in the child, not the parent, will be the one eventually used.

Available Variables

Project Model Variables

Any field of the model that is a single value element can be referenced as a variable. For example, ${project.groupId}, ${project.version},${project.build.sourceDirectory} and so on. Refer to the POM reference to see a full list of properties.

These variables are all referenced by the prefix "project.". You may also see references with pom. as the prefix, or the prefix omitted entirely - these forms are now deprecated and should not be used.

Special Variables
project.basedir The directory that the current project resides in.
project.baseUri The directory that the current project resides in, represented as an URI. Since Maven 2.1.0
maven.build.timestamp The timestamp that denotes the start of the build. Since Maven 2.1.0-M1

The format of the build timestamp can be customized by declaring the property maven.build.timestamp.format as shown in the example below:

  1. <project>
  2. ...
  3. <properties>
  4. <maven.build.timestamp.format>yyyy-MM-dd'T'HH:mm:ss'Z'</maven.build.timestamp.format>
  5. </properties>
  6. ...
  7. </project>

The format pattern has to comply with the rules given in the API documentation for SimpleDateFormat. If the property is not present, the format defaults to the value already given in the example.

Properties

You are also able to reference any properties defined in the project as a variable. Consider the following example:

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

你可能感兴趣的:(Introduction to the POM)