Maven(1)之pom文件详解

小灰灰 2022-05-31 14:14 580阅读 1赞

一、什么是POM
Project Object Model,项目对象模型。通过xml格式保存的pom.xml文件。作用类似ant的build.xml文件,功能更强大。该文件用于管理:源代码、配置文件、开发者的信息和角色、问题追踪系统、组织信息、项目授权、项目的url、项目的依赖关系等等。
一个完整的pom.xml文件,放置在项目的根目录下。

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

二、基本设置
1、maven的协作相关属性

  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/maven-v4_0_0.xsd">
  2. <modelVersion>4.0.0</modelVersion>
  3. <groupId>org.codehaus.mojo</groupId>
  4. <artifactId>my-project</artifactId>
  5. <version>1.0</version>
  6. <packaging>war</packaging>
  7. </project>

groupId : 组织标识,例如:org.codehaus.mojo,在M2_REPO目录下,将是: org/codehaus/mojo目录。
artifactId : 项目名称,例如:my-project,在M2_REPO目录下,将是:org/codehaus/mojo/my-project目录。
version : 版本号,例如:1.0,在M2_REPO目录下,将是:org/codehaus/mojo/my-project/1.0目录。
packaging : 打包的格式,可以为:pom , jar , maven-plugin , ejb , war , ear , rar , par
2、POM之间的关系
主要用于POM文件的复用。
a)依赖关系:依赖关系列表(dependency list)是POM的重要部分

  1. <dependencies>
  2. <dependency>
  3. <groupId>junit</groupId>
  4. <artifactId>junit</artifactId>
  5. <version>4.0</version>
  6. <scope>test</scope>
  7. </dependency>
  8. </dependencies>

groupId , artifactId , version :
scope : compile(default),provided,runtime,test,system
exclusions
b)继承关系:继承其他pom.xml配置的机制。
比如父pom.xml:

  1. <project>
  2. [...]
  3. <dependencies>
  4. <dependency>
  5. <groupId>junit</groupId>
  6. <artifactId>junit</artifactId>
  7. <version>4.4</version>
  8. <scope>test</scope>
  9. </dependency>
  10. </dependencies>
  11. [...]
  12. </project>

在子pom.xml文件继承它的依赖(还可以继承其他的:developers and contributors、plugin lists、reports lists、plugin executions with matching ids、plugin configuration):
[html] view plain copy

  1. [...]
  2. <parent>
  3. <groupId>com.devzuz.mvnbook.proficio</groupId>
  4. <artifactId>proficio</artifactId>
  5. <version>1.0-SNAPSHOT</version>
  6. </parent>
  7. [...]

在这种机制下,maven还提供了一个类似java.lang.Object的顶级父pom.xml文件:

  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>${project.artifactId}-${project.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. <pluginManagement>
  48. <plugins>
  49. <plugin>
  50. <artifactId>maven-antrun-plugin</artifactId>
  51. <version>1.1</version>
  52. </plugin>
  53. <plugin>
  54. <artifactId>maven-assembly-plugin</artifactId>
  55. <version>2.2-beta-2</version>
  56. </plugin>
  57. <plugin>
  58. <artifactId>maven-clean-plugin</artifactId>
  59. <version>2.2</version>
  60. </plugin>
  61. <plugin>
  62. <artifactId>maven-compiler-plugin</artifactId>
  63. <version>2.0.2</version>
  64. </plugin>
  65. <plugin>
  66. <artifactId>maven-dependency-plugin</artifactId>
  67. <version>2.0</version>
  68. </plugin>
  69. <plugin>
  70. <artifactId>maven-deploy-plugin</artifactId>
  71. <version>2.3</version>
  72. </plugin>
  73. <plugin>
  74. <artifactId>maven-ear-plugin</artifactId>
  75. <version>2.3.1</version>
  76. </plugin>
  77. <plugin>
  78. <artifactId>maven-ejb-plugin</artifactId>
  79. <version>2.1</version>
  80. </plugin>
  81. <plugin>
  82. <artifactId>maven-install-plugin</artifactId>
  83. <version>2.2</version>
  84. </plugin>
  85. <plugin>
  86. <artifactId>maven-jar-plugin</artifactId>
  87. <version>2.2</version>
  88. </plugin>
  89. <plugin>
  90. <artifactId>maven-javadoc-plugin</artifactId>
  91. <version>2.4</version>
  92. </plugin>
  93. <plugin>
  94. <artifactId>maven-plugin-plugin</artifactId>
  95. <version>2.4.1</version>
  96. </plugin>
  97. <plugin>
  98. <artifactId>maven-rar-plugin</artifactId>
  99. <version>2.2</version>
  100. </plugin>
  101. <plugin>
  102. <artifactId>maven-release-plugin</artifactId>
  103. <version>2.0-beta-7</version>
  104. </plugin>
  105. <plugin>
  106. <artifactId>maven-resources-plugin</artifactId>
  107. <version>2.2</version>
  108. </plugin>
  109. <plugin>
  110. <artifactId>maven-site-plugin</artifactId>
  111. <version>2.0-beta-6</version>
  112. </plugin>
  113. <plugin>
  114. <artifactId>maven-source-plugin</artifactId>
  115. <version>2.0.4</version>
  116. </plugin>
  117. <plugin>
  118. <artifactId>maven-surefire-plugin</artifactId>
  119. <version>2.4.2</version>
  120. </plugin>
  121. <plugin>
  122. <artifactId>maven-war-plugin</artifactId>
  123. <version>2.1-alpha-1</version>
  124. </plugin>
  125. </plugins>
  126. </pluginManagement>
  127. </build>
  128. <reporting>
  129. <outputDirectory>target/site</outputDirectory>
  130. </reporting>
  131. <profiles>
  132. <profile>
  133. <id>release-profile</id>
  134. <activation>
  135. <property>
  136. <name>performRelease</name>
  137. <value>true</value>
  138. </property>
  139. </activation>
  140. <build>
  141. <plugins>
  142. <plugin>
  143. <inherited>true</inherited>
  144. <groupId>org.apache.maven.plugins</groupId>
  145. <artifactId>maven-source-plugin</artifactId>
  146. <executions>
  147. <execution>
  148. <id>attach-sources</id>
  149. <goals>
  150. <goal>jar</goal>
  151. </goals>
  152. </execution>
  153. </executions>
  154. </plugin>
  155. <plugin>
  156. <inherited>true</inherited>
  157. <groupId>org.apache.maven.plugins</groupId>
  158. <artifactId>maven-javadoc-plugin</artifactId>
  159. <executions>
  160. <execution>
  161. <id>attach-javadocs</id>
  162. <goals>
  163. <goal>jar</goal>
  164. </goals>
  165. </execution>
  166. </executions>
  167. </plugin>
  168. <plugin>
  169. <inherited>true</inherited>
  170. <groupId>org.apache.maven.plugins</groupId>
  171. <artifactId>maven-deploy-plugin</artifactId>
  172. <configuration>
  173. <updateReleaseInfo>true</updateReleaseInfo>
  174. </configuration>
  175. </plugin>
  176. </plugins>
  177. </build>
  178. </profile>
  179. </profiles>
  180. </project>

可以通过下面命令查看当前pom.xml受到超pom.xml文件的影响:
mvn help:effective-pom
c)聚合关系:用于将多个maven项目聚合为一个大的项目。

  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/maven-v4_0_0.xsd">
  2. <modelVersion>4.0.0</modelVersion>
  3. <groupId>org.codehaus.mojo</groupId>
  4. <artifactId>my-parent</artifactId>
  5. <version>2.0</version>
  6. <modules>
  7. <module>my-project<module>
  8. </modules>
  9. </project>

3、属性
maven的属性,是值的占位符,类似EL,类似ant的属性,比如${X},可用于pom文件任何赋值的位置。有以下分类:

env.X:操作系统环境变量,比如env.PATHproject.x:pom文件中的属性,比如:1.0,引用方式: e n v . P A T H p r o j e c t . x : p o m 文 件 中 的 属 性 , 比 如 : 1.0 , 引 用 方 式 : {project.version}
settings.x:settings.xml文件中的属性,比如:false,引用方式:settings.offlineJavaSystemProperties:java.lang.System.getProperties()中的属性,比如java.home,引用方式: s e t t i n g s . o f f l i n e J a v a S y s t e m P r o p e r t i e s : j a v a . l a n g . S y s t e m . g e t P r o p e r t i e s ( ) 中 的 属 性 , 比 如 j a v a . h o m e , 引 用 方 式 : {java.home}
自定义:在pom文件中可以:c:/apps/cargo-installs,引用方式:${installDir}
4、构建设置
构建有两种build标签:

  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/maven-v4_0_0.xsd">
  2. <!– "Project Build" contains more elements than just the BaseBuild set –>
  3. <build></build>
  4. <profiles>
  5. <profile>
  6. <!– "Profile Build" contains a subset of "Project Build"s elements –>
  7. <build></build>
  8. </profile>
  9. </profiles>
  10. </project>

build中的主要标签:Resources和Plugins。

Resources:用于排除或包含某些资源文件

  1. <resources>
  2. <resource>
  3. <targetPath>META-INF/plexus</targetPath>
  4. <filtering>false</filtering>
  5. <directory>${basedir}/src/main/plexus</directory>
  6. <includes>
  7. <include>configuration.xml</include>
  8. </includes>
  9. <excludes>
  10. <exclude>**/*.properties</exclude>
  11. </excludes>
  12. </resource>
  13. </resources>

Plugins:设置构建的插件

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.apache.maven.plugins</groupId>
  5. <artifactId>maven-jar-plugin</artifactId>
  6. <version>2.0</version>
  7. <extensions>false</extensions>
  8. <inherited>true</inherited>
  9. <configuration>
  10. <classifier>test</classifier>
  11. </configuration>
  12. <dependencies></dependencies>
  13. <executions></executions>
  14. </plugin>

发表评论

表情:
评论列表 (有 0 条评论,580人围观)

还没有评论,来说两句吧...

相关阅读