原文: https://howtodoinjava.com/maven/maven-pom-files/

POM(项目对象模型)是一个 XML 文件,其中包含有关项目的信息以及 Maven 用于构建项目的配置详细信息,例如源代码位置,项目依赖项等。此文件必须命名为pom.xml,并放置在项目的根文件夹下 。 执行任务或目标时,maven 读取 POM,获取所需的配置信息,然后执行目标。

  1. Table of Contents
  2. Super POM
  3. Minimal POM Configuration
  4. Default POM Configuration
  5. POM Hierarchy

超级 POM

POM 文件在它们之间保持父子关系。 您为项目创建的任何 pom 文件最终都将扩展此超级 pom 文件。 从 maven 2.2 开始,这是超级 pom 文件内容

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

值得注意的是:

  • 默认仓库是 Maven 仓库。
  • 默认执行目标是“ jar”。
  • 默认源代码位置为src/main/java
  • 默认测试代码位置为src/test/java

最小的 POM 配置

任何 Maven 项目的最小 POM 配置都非常简单,如下所示:

  1. <project>
  2. <modelVersion>4.0.0</modelVersion>
  3. <groupId>com.howtodoinjava</groupId>
  4. <artifactId>MavenExample</artifactId>
  5. <version>1.0.0</version>
  6. </project>

这个需要:

  • project
  • modelVersion – 应设置为 4.0.0
  • groupId – 项目组的 ID。
  • artifactId – 工件的 ID(项目)
  • version – 指定组下工件的版本

您需要在此最小 pom 的基础上添加项目特定的配置。 对于其余配置,如果未指定,则 maven 将使用超级 pom 文件中的默认设置。

默认 POM 配置

这取决于您选择的原型类型。 例如,对于快速启动项目,这是默认生成的pom.xml文件。

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd;
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.howtodoinjava.demo</groupId>
  5. <artifactId>MavenExamples</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <packaging>jar</packaging>
  8. <name>MavenExamples</name>
  9. <url>http://maven.apache.org</url>
  10. <properties>
  11. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  12. </properties>
  13. <dependencies>
  14. <dependency>
  15. <groupId>junit</groupId>
  16. <artifactId>junit</artifactId>
  17. <version>3.8.1</version>
  18. <scope>test</scope>
  19. </dependency>
  20. </dependencies>
  21. </project>

POM 层次结构

如前所述,POM 文件在它们之间保持父子关系。 子 POM 文件从其父 POM 文件继承所有配置元素。 这就是 Maven 坚持其设计理念的方式,即相对于配置而言是约定

项目使用的 pom 文件是通过合并项目 pom 文件,父 pom 文件(如果有)和超级 pom 文件而获得的。 此 pom 文件称为有效 pom 文件。

要获取项目使用的有效 pom 文件,请在项目的根文件夹中输入以下命令:

  1. $ mvn help:effective-pom

例如,有效的 pom 文件默认快速启动项目为:

  1. <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;
  2. <modelVersion>4.0.0</modelVersion>
  3. <groupId>com.howtodoinjava.demo</groupId>
  4. <artifactId>MavenExamples</artifactId>
  5. <version>0.0.1-SNAPSHOT</version>
  6. <name>MavenExamples</name>
  7. <url>http://maven.apache.org</url>
  8. <properties>
  9. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  10. </properties>
  11. <dependencies>
  12. <dependency>
  13. <groupId>junit</groupId>
  14. <artifactId>junit</artifactId>
  15. <version>3.8.1</version>
  16. <scope>test</scope>
  17. </dependency>
  18. </dependencies>
  19. <repositories>
  20. <repository>
  21. <snapshots>
  22. <enabled>false</enabled>
  23. </snapshots>
  24. <id>central</id>
  25. <name>Central Repository</name>
  26. <url>https://repo.maven.apache.org/maven2</url>
  27. </repository>
  28. </repositories>
  29. <pluginRepositories>
  30. <pluginRepository>
  31. <releases>
  32. <updatePolicy>never</updatePolicy>
  33. </releases>
  34. <snapshots>
  35. <enabled>false</enabled>
  36. </snapshots>
  37. <id>central</id>
  38. <name>Central Repository</name>
  39. <url>https://repo.maven.apache.org/maven2</url>
  40. </pluginRepository>
  41. </pluginRepositories>
  42. <build>
  43. <sourceDirectory>c:\temp\MavenExamples\src\main\java</sourceDirectory>
  44. <scriptSourceDirectory>c:\temp\MavenExamples\src\main\scripts</scriptSourceDirectory>
  45. <testSourceDirectory>c:\temp\MavenExamples\src\test\java</testSourceDirectory>
  46. <outputDirectory>c:\temp\MavenExamples\target\classes</outputDirectory>
  47. <testOutputDirectory>c:\temp\MavenExamples\target\test-classes</testOutputDirectory>
  48. <resources>
  49. <resource>
  50. <directory>c:\temp\MavenExamples\src\main\resources</directory>
  51. </resource>
  52. </resources>
  53. <testResources>
  54. <testResource>
  55. <directory>c:\temp\MavenExamples\src\test\resources</directory>
  56. </testResource>
  57. </testResources>
  58. <directory>c:\temp\MavenExamples\target</directory>
  59. <finalName>MavenExamples-0.0.1-SNAPSHOT</finalName>
  60. <pluginManagement>
  61. <plugins>
  62. <plugin>
  63. <artifactId>maven-antrun-plugin</artifactId>
  64. <version>1.3</version>
  65. </plugin>
  66. <plugin>
  67. <artifactId>maven-assembly-plugin</artifactId>
  68. <version>2.2-beta-5</version>
  69. </plugin>
  70. <plugin>
  71. <artifactId>maven-dependency-plugin</artifactId>
  72. <version>2.8</version>
  73. </plugin>
  74. <plugin>
  75. <artifactId>maven-release-plugin</artifactId>
  76. <version>2.3.2</version>
  77. </plugin>
  78. </plugins>
  79. </pluginManagement>
  80. <plugins>
  81. <plugin>
  82. <artifactId>maven-clean-plugin</artifactId>
  83. <version>2.5</version>
  84. <executions>
  85. <execution>
  86. <id>default-clean</id>
  87. <phase>clean</phase>
  88. <goals>
  89. <goal>clean</goal>
  90. </goals>
  91. </execution>
  92. </executions>
  93. </plugin>
  94. <plugin>
  95. <artifactId>maven-resources-plugin</artifactId>
  96. <version>2.6</version>
  97. <executions>
  98. <execution>
  99. <id>default-testResources</id>
  100. <phase>process-test-resources</phase>
  101. <goals>
  102. <goal>testResources</goal>
  103. </goals>
  104. </execution>
  105. <execution>
  106. <id>default-resources</id>
  107. <phase>process-resources</phase>
  108. <goals>
  109. <goal>resources</goal>
  110. </goals>
  111. </execution>
  112. </executions>
  113. </plugin>
  114. <plugin>
  115. <artifactId>maven-jar-plugin</artifactId>
  116. <version>2.4</version>
  117. <executions>
  118. <execution>
  119. <id>default-jar</id>
  120. <phase>package</phase>
  121. <goals>
  122. <goal>jar</goal>
  123. </goals>
  124. </execution>
  125. </executions>
  126. </plugin>
  127. <plugin>
  128. <artifactId>maven-compiler-plugin</artifactId>
  129. <version>3.1</version>
  130. <executions>
  131. <execution>
  132. <id>default-compile</id>
  133. <phase>compile</phase>
  134. <goals>
  135. <goal>compile</goal>
  136. </goals>
  137. </execution>
  138. <execution>
  139. <id>default-testCompile</id>
  140. <phase>test-compile</phase>
  141. <goals>
  142. <goal>testCompile</goal>
  143. </goals>
  144. </execution>
  145. </executions>
  146. </plugin>
  147. <plugin>
  148. <artifactId>maven-surefire-plugin</artifactId>
  149. <version>2.12.4</version>
  150. <executions>
  151. <execution>
  152. <id>default-test</id>
  153. <phase>test</phase>
  154. <goals>
  155. <goal>test</goal>
  156. </goals>
  157. </execution>
  158. </executions>
  159. </plugin>
  160. <plugin>
  161. <artifactId>maven-install-plugin</artifactId>
  162. <version>2.4</version>
  163. <executions>
  164. <execution>
  165. <id>default-install</id>
  166. <phase>install</phase>
  167. <goals>
  168. <goal>install</goal>
  169. </goals>
  170. </execution>
  171. </executions>
  172. </plugin>
  173. <plugin>
  174. <artifactId>maven-deploy-plugin</artifactId>
  175. <version>2.7</version>
  176. <executions>
  177. <execution>
  178. <id>default-deploy</id>
  179. <phase>deploy</phase>
  180. <goals>
  181. <goal>deploy</goal>
  182. </goals>
  183. </execution>
  184. </executions>
  185. </plugin>
  186. <plugin>
  187. <artifactId>maven-site-plugin</artifactId>
  188. <version>3.3</version>
  189. <executions>
  190. <execution>
  191. <id>default-site</id>
  192. <phase>site</phase>
  193. <goals>
  194. <goal>site</goal>
  195. </goals>
  196. <configuration>
  197. <outputDirectory>c:\temp\MavenExamples\target\site</outputDirectory>
  198. <reportPlugins>
  199. <reportPlugin>
  200. <groupId>org.apache.maven.plugins</groupId>
  201. <artifactId>maven-project-info-reports-plugin</artifactId>
  202. </reportPlugin>
  203. </reportPlugins>
  204. </configuration>
  205. </execution>
  206. <execution>
  207. <id>default-deploy</id>
  208. <phase>site-deploy</phase>
  209. <goals>
  210. <goal>deploy</goal>
  211. </goals>
  212. <configuration>
  213. <outputDirectory>c:\temp\MavenExamples\target\site</outputDirectory>
  214. <reportPlugins>
  215. <reportPlugin>
  216. <groupId>org.apache.maven.plugins</groupId>
  217. <artifactId>maven-project-info-reports-plugin</artifactId>
  218. </reportPlugin>
  219. </reportPlugins>
  220. </configuration>
  221. </execution>
  222. </executions>
  223. <configuration>
  224. <outputDirectory>c:\temp\MavenExamples\target\site</outputDirectory>
  225. <reportPlugins>
  226. <reportPlugin>
  227. <groupId>org.apache.maven.plugins</groupId>
  228. <artifactId>maven-project-info-reports-plugin</artifactId>
  229. </reportPlugin>
  230. </reportPlugins>
  231. </configuration>
  232. </plugin>
  233. </plugins>
  234. </build>
  235. <reporting>
  236. <outputDirectory>c:\temp\MavenExamples\target\site</outputDirectory>
  237. </reporting>
  238. </project>

将我的问题放在评论部分。

学习愉快!