Maven之(七)pom.xml配置文件详解

左手的ㄟ右手 2022-05-22 13:26 274阅读 0赞

setting.xml主要用于配置maven的运行环境等一系列通用的属性,是全局级别的配置文件;而pom.xml主要描述了项目的maven坐标,依赖关系,开发者需要遵循的规则,缺陷管理系统,组织和licenses,以及其他所有的项目相关因素,是项目级别的配置文件。

基础配置

一个典型的pom.xml文件配置如下:

[html] view plain copy

  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.0http://maven.apache.org/xsd/maven-4.0.0.xsd"**>**
  3. <**modelVersion**>4.0.0</**modelVersion**>
  4. <**groupId**>com.winner.trade</**groupId**>
  5. <**artifactId**>trade-core</**artifactId**>
  6. <**version**>1.0.0-SNAPSHOT</**version**>
  7. <**packaging**>jar</**packaging**>
  8. <**classifier**></**classifier**>
  9. <**dependencies**>
  10. <**dependency**>
  11. <**groupId**>com.winner.trade</**groupId**>
  12. <**artifactId**>trade-test</**artifactId**>
  13. <**version**>1.0.0-SNAPSHOT</**version**>
  14. <**scope**>test</**scope**>
  15. <**optional**>false</**optional**>
  16. <**exclusions**>
  17. <**exclusion**>
  18. <**groupId**>org.slf4j</**groupId**>
  19. <**artifactId**>slf4j-api</**artifactId**>
  20. </**exclusion**>
  21. </**exclusions**>
  22. </**dependency**>
  23. </**dependencies**>
  24. <**properties**>
  25. <**file.encoding**>UTF-8</**file.encoding**>
  26. <**java.source.version**>1.5</**java.source.version**>
  27. <**java.target.version**>1.5</**java.target.version**>
  28. </**properties**>
  29. </**project**>

一般来说,上面的几个配置项对任何项目都是必不可少的,定义了项目的基本属性。

这里有必要对一个不太常用的属性classifier做一下解释,因为有时候引用某个jar包,classifier不写的话会报错。

classifier元素用来帮助定义构件输出的一些附属构件。附属构件与主构件对应,比如主构件是 kimi-app-2.0.0.jar,该项目可能还会通过使用一些插件生成 如kimi-app-2.0.0-javadoc.jar (Java文档)、 kimi-app-2.0.0-sources.jar(Java源代码) 这样两个附属构件。这时候,javadoc、sources就是这两个附属构件的classifier,这样附属构件也就拥有了自己唯一的坐标。

classifier的用途在于:

  1. maven download javadoc / sources jar包的时候,需要借助classifier指明要下载那个附属构件

  2. 引入依赖的时候,有时候仅凭groupId、artifactId、version无法唯一的确定某个构件,需要借助classifier来进一步明确目标。比如JSON-lib,有时候会同一个版本会提供多个jar包,在JDK1.5环境下是一套,在JDK1.3环境下是一套:

Center

引用它的时候就要注明JDK版本,否则maven不知道你到底需要哪一套jar包:

[html] view plain copy

  1. <**dependency**>
  2. <**groupId**>net.sf.json-lib</**groupId**>
  3. <**artifactId**>json-lib</**artifactId**>
  4. <**version**>2.4</**version**>
  5. <**classifier**>jdk15</**classifier**>
  6. </**dependency**>

构建配置

[html] view plain copy

  1. <**build**>
  2. <**finalName**>myPorjectName</**finalName**>
  3. <**directory**>${basedir}/target</**directory**>
  4. <**defaultGoal**>install</**defaultGoal**>
  5. <**filters**>
  6. <**filter**>../filter.properties</**filter**>
  7. </**filters**>
  8. <**resources**>
  9. <**resource**>
  10. <**targetPath**>resources</**targetPath**>
  11. <**filtering**>true</**filtering**>
  12. <**directory**>src/main/resources</**directory**>
  13. <**includes**>
  14. <**include**>**/*.properties</**include**>
  15. <**include**>**/*.xml</**include**>
  16. </**includes**>
  17. <**excludes**>
  18. <**exclude**>jdbc.properties</**exclude**>
  19. </**excludes**>
  20. </**resource**>
  21. </**resources**>
  22. <**testResources**>
  23. <**testResource**>
  24. <**targetPath />**
  25. <**filtering />**
  26. <**directory />**
  27. <**includes />**
  28. <**excludes />**
  29. </**testResource**>
  30. </**testResources**>
  31. <**sourceDirectory**>${basedir}\src\main\java</**sourceDirectory**>
  32. <**scriptSourceDirectory**>${basedir}\src\main\scripts
  33. </**scriptSourceDirectory**>
  34. <**testSourceDirectory**>${basedir}\src\test\java</**testSourceDirectory**>
  35. <**outputDirectory**>${basedir}\target\classes</**outputDirectory**>
  36. <**testOutputDirectory**>${basedir}\target\test-classes
  37. </**testOutputDirectory**>
  38. <**extensions**>
  39. <**extension**>
  40. <**groupId**>org.apache.maven.wagon</**groupId**>
  41. <**artifactId**>wagon-ssh</**artifactId**>
  42. <**version**>2.8</**version**>
  43. </**extension**>
  44. </**extensions**>
  45. <**plugins**>
  46. <**plugin**>
  47. <**groupId></groupId>**
  48. <**artifactId**>maven-assembly-plugin</**artifactId**>
  49. <**version**>2.5.5</**version**>
  50. <**executions**>
  51. <**execution**>
  52. <**id**>assembly</**id**>
  53. <**phase**>package</**phase**>
  54. <**goals**>
  55. <**goal**>single</**goal**>
  56. </**goals**>
  57. <**inherited**>false</**inherited**>
  58. </**execution**>
  59. </**executions**>
  60. <**configuration**>
  61. <**finalName**>${finalName}</**finalName**>
  62. <**appendAssemblyId**>false</**appendAssemblyId**>
  63. <**descriptor**>assembly.xml</**descriptor**>
  64. </**configuration**>
  65. <**extensions**>false</**extensions**>
  66. <**dependencies**>
  67. <**dependency**></**dependency**>
  68. </**dependencies**>
  69. <**inherited**>true</**inherited**>
  70. </**plugin**>
  71. </**plugins**>
  72. <**pluginManagement**>
  73. <**plugins**></**plugins**>
  74. </**pluginManagement**>
  75. </**build**>

pom里面的仓库与setting.xml里的仓库功能是一样的。主要的区别在于,pom里的仓库是个性化的。比如一家大公司里的setting文件是公用的,所有项目都用一个setting文件,但各个子项目却会引用不同的第三方库,所以就需要在pom里设置自己需要的仓库地址。

分发配置

[html] view plain copy

  1. <**distributionManagement**>
  2. <**repository**>
  3. <**uniqueVersion**>true</**uniqueVersion**>
  4. <**id**> repo-id </**id**>
  5. <**name**> repo-name</**name**>
  6. <**url**>file://${basedir}/target/deploy </**url**>
  7. <**layout />**
  8. </**repository**>
  9. <**snapshotRepository**>
  10. <**uniqueVersion />**
  11. <**id />**
  12. <**name />**
  13. <**url />**
  14. <**layout />**
  15. </**snapshotRepository**>
  16. <**site**>
  17. <**id**> site-id </**id**>
  18. <**name**> site-name</**name**>
  19. <**url**>scp://svn.baidu.com/banseon:/var/www/localhost/banseon-web </**url**>
  20. </**site**>
  21. <**downloadUrl />**
  22. <**relocation**>
  23. <**groupId />**
  24. <**artifactId />**
  25. <**version />**
  26. <**message />**
  27. </**relocation**>
  28. <**status />**
  29. </**distributionManagement**>

仓库配置

[html] view plain copy

  1. <**repositories**>
  2. <**repository**>
  3. <**releases**>
  4. <**enabled />**
  5. <**updatePolicy />**
  6. <**checksumPolicy />**
  7. </**releases**>
  8. <**snapshots**>
  9. <**enabled />**
  10. <**updatePolicy />**
  11. <**checksumPolicy />**
  12. </**snapshots**>
  13. <**id**> repo-id </**id**>
  14. <**name**> repo-name</**name**>
  15. <**url**>http://192.168.1.169:9999/repository/ </**url**>
  16. <**layout**> default</**layout**>
  17. </**repository**>
  18. </**repositories**>
  19. <**pluginRepositories**>
  20. <**pluginRepository />**
  21. </**pluginRepositories**>

profile配置

[html] view plain copy

  1. <**profiles**>
  2. <**profile**>
  3. <**activation**>
  4. <**activeByDefault**>false</**activeByDefault**>
  5. <**jdk**>1.7</**jdk**>
  6. <**os**>
  7. <**name**>Windows XP</**name**>
  8. <**family**>Windows</**family**>
  9. <**arch**>x86</**arch**>
  10. <**version**>5.1.2600</**version**>
  11. </**os**>
  12. <**property**>
  13. <**name**>mavenVersion</**name**>
  14. <**value**>2.0.3</**value**>
  15. </**property**>
  16. <**file**>
  17. <**exists**>/usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/</**exists**>
  18. <**missing**>/usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/</**missing**>
  19. </**file**>
  20. </**activation**>
  21. <**id />**
  22. <**build />**
  23. <**modules />**
  24. <**repositories />**
  25. <**pluginRepositories />**
  26. <**dependencies />**
  27. <**reporting />**
  28. <**dependencyManagement />**
  29. <**distributionManagement />**
  30. <**properties />**
  31. </**profile**>

profile配置项在setting.xml中也有,是pom.xml中profile元素的裁剪版本,包含了id,activation, repositories, pluginRepositories和 properties元素。这里的profile元素只包含这五个子元素是因为setting.xml只关心构建系统这个整体(这正是settings.xml文件的角色定位),而非单独的项目对象模型设置。如果一个settings中的profile被激活,它的值会覆盖任何其它定义在POM中或者profile.xml中的带有相同id的profile。

pom.xml中的profile可以看做pom.xml的副本,拥有与pom.xml相同的子元素与配置方法。它包含可选的activation(profile的触发器)和一系列的changes。例如test过程可能会指向不同的数据库(相对最终的deployment)或者不同的dependencies或者不同的repositories,并且是根据不同的JDK来改变的。只需要其中一个成立就可以激活profile,如果第一个条件满足了,那么后面就不会在进行匹配。

报表配置

[html] view plain copy

  1. <**reporting**>
  2. <**excludeDefaults />**
  3. <**outputDirectory />**
  4. <**plugins**>
  5. <**plugin**>
  6. <**groupId />**
  7. <**artifactId />**
  8. <**version />**
  9. <**inherited />**
  10. <**configuration**>
  11. <**links**>
  12. <**link**>http://java.sun.com/j2se/1.5.0/docs/api/**</link>**
  13. </**links**>
  14. </**configuration**>
  15. <**reportSets**>
  16. <**reportSet**>
  17. <**id**>sunlink</**id**>
  18. <**configuration />**
  19. <**inherited />**
  20. <**reports**>
  21. <**report**>javadoc</**report**>
  22. </**reports**>
  23. </**reportSet**>
  24. </**reportSets**>
  25. </**plugin**>
  26. </**plugins**>
  27. </**reporting**>

环境配置

[html] view plain copy

  1. <**issueManagement**>
  2. <**system**> jira </**system**>
  3. <**url**> http://jira.clf.com/**</url>**
  4. </**issueManagement**>
  5. <**ciManagement**>
  6. <**system />**
  7. <**url />**
  8. <**notifiers**>
  9. <**notifier**>
  10. <**type />**
  11. <**sendOnError />**
  12. <**sendOnFailure />**
  13. <**sendOnSuccess />**
  14. <**sendOnWarning />**
  15. <**address />**
  16. <**configuration />**
  17. </**notifier**>
  18. </**notifiers**>
  19. </**ciManagement**>

项目信息配置

[html] view plain copy

  1. <**name**>banseon-maven </**name**>
  2. <**url**>http://www.clf.com/ </**url**>
  3. <**description**>A maven project to study maven. </**description**>
  4. <**prerequisites**>
  5. <**maven />**
  6. </**prerequisites**>
  7. <**inceptionYear />**
  8. <**mailingLists**>
  9. <**mailingList**>
  10. <**name**> Demo </**name**>
  11. <**post**> clf@126.com</**post**>
  12. <**subscribe**> clf@126.com</**subscribe**>
  13. <**unsubscribe**> clf@126.com</**unsubscribe**>
  14. <**archive**> http:/hi.clf.com/</**archive**>
  15. </**mailingList**>
  16. </**mailingLists**>
  17. <**developers**>
  18. <**developer**>
  19. <**id**> HELLO WORLD </**id**>
  20. <**name**> banseon </**name**>
  21. <**email**> banseon@126.com</**email**>
  22. <**url />**
  23. <**roles**>
  24. <**role**> Project Manager</**role**>
  25. <**role**>Architect </**role**>
  26. </**roles**>
  27. <**organization**> demo</**organization**>
  28. <**organizationUrl**>http://hi.clf.com/ </**organizationUrl**>
  29. <**properties**>
  30. <**dept**> No </**dept**>
  31. </**properties**>
  32. <**timezone**> -5</**timezone**>
  33. </**developer**>
  34. </**developers**>
  35. <**contributors**>
  36. <**contributor**>
  37. <**name />**
  38. <**email />**
  39. <**url />**
  40. <**organization />**
  41. <**organizationUrl />**
  42. <**roles />**
  43. <**timezone />**
  44. <**properties />**
  45. </**contributor**>
  46. </**contributors**>
  47. <**licenses**>
  48. <**license**>
  49. <**name**> Apache 2 </**name**>
  50. <**url**>http://www.clf.com/LICENSE-2.0.txt </**url**>
  51. <**distribution**> repo</**distribution**>
  52. <**comments**> Abusiness-friendly OSS license </**comments**>
  53. </**license**>
  54. </**licenses**>
  55. <**scm**>
  56. <**connection**>scm:svn:http://svn.baidu.com/banseon/maven/**</connection>**
  57. <**developerConnection**>scm:svn:http://svn.baidu.com/banseon/maven/
  58. </**developerConnection**>
  59. <**tag />**
  60. <**url**> http://svn.baidu.com/banseon**</url>**
  61. </**scm**>
  62. <**organization**>
  63. <**name**> demo </**name**>
  64. <**url**> http://www.clf.com/**</url>**
  65. </**organization**>

发表评论

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

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

相关阅读