MAVEN基础

一、MAVEN生命周期(lifecycle,phase)

官网介绍:http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

(1)MAVEN有三套独立的生命周期,分别是:

  • Clean Lifecycle 在进行真正的构建之前进行一些清理工作
  • Default Lifecycle 构建的核心部分,编译,测试,打包,部署等等。
  • Site Lifecycle 生成项目报告,站点,发布站点

每一套生命周期是相互独立的。

(2)clean的生命周期 共分为三个阶段

  • pre-clean 执行一些需要在clean之前完成的工作
  • clean 移除所有上一次构建生成的文件
  • post-clean 执行一些需要在clean之后立刻完成的工作

(3)site的生命周期 共分为四个阶段

  • pre-site 执行一些需要在生成站点文档之前完成的工作
  • site 生成项目的站点文档
  • post-site 执行一些需要在生成站点文档之后完成的工作,并且为部署做准备
  • site-deploy 将生成的站点文档部署到特定的服务器上

(4)Maven的最重要的Default生命周期,绝大部分工作都发生在这个生命周期中

  • validate
  • generate-sources
  • process-sources
  • generate-resources
  • process-resources 复制并处理资源文件,至目标目录,准备打包。
  • compile 编译项目的源代码。
  • process-classes
  • generate-test-sources
  • process-test-sources
  • generate-test-resources
  • process-test-resources 复制并处理资源文件,至目标测试目录。
  • test-compile 编译测试源代码。
  • process-test-classes
  • test 使用合适的单元测试框架运行测试。这些测试代码不会被打包或部署。
  • prepare-package
  • package 接受编译好的代码,打包成可发布的格式,如 JAR 。
  • pre-integration-test
  • integration-test
  • post-integration-test
  • verify
  • install 将包安装至本地仓库,以让其它项目依赖。
  • deploy 将最终的包复制到远程的仓库,以让其它开发人员与项目共享。

(5)lifecycle:生命周期,这是maven最高级别的的控制单元,它是一系列的phase组成,也就是说,一个生命周期,就是一个大任务的总称,不管它里面分成多少个子任务,反正就是运行一个lifecycle,就是交待了一个任务,运行完后,就得到了一个结果,中间的过程,是phase完成的,自己可以定义自己的lifecycle,包含自己想要的phase

(6)phase:可以理解为任务单元,lifecycle是总任务,phase就是总任务分出来的一个个子任务,但是这些子任务是被规格化的,它可以同时被多个lifecycle所包含,一个lifecycle可以包含任意个phase,phase的执行是按顺序的,一个phase可以绑定很多个goal,至少为一个,没有goal的phase是没有意义的.

(7)goal: 这是执行任务的最小单元,它可以绑定到任意个phase中,一个phase有一个或多个goal,goal也是按顺序执行的,一个phase被执行时,绑定到phase里的goal会按绑定的时间被顺序执行,不管phase己经绑定了多少个goal,你自己定义的goal都可以继续绑到phase中

(8)mojo: lifecycle与phase与goal都是概念上的东西,mojo才是做具体事情的,可以简单理解mojo为goal的实现类,它继承于AbstractMojo,有一个execute方法,goal等的定义都是通过在mojo里定义一些注释的anotation来实现的,maven会在打包时,自动根据这些anotation生成一些xml文件,放在plugin的jar包里


二、内置变量

  • ${basedir} 项目根目录
  • ${project.build.directory} 构建目录,缺省为target
  • ${project.build.outputDirectory} 构建过程输出目录,缺省为target/classes
  • ${project.build.finalName} 产出物名称,缺省为${project.artifactId}-${project.version}
  • ${project.packaging} 打包类型,缺省为jar
  • ${project.xxx} 当前pom文件的任意节点的内容

三、常用命令

(1)查看插件的介绍信息,显示插件的goal及介绍

mvn help:describe -Dplugin=插件名称

例:mvn help:describe -Dplugin=help

[plain]  view plain copy
  1. H:\develop\workspace>mvn help:describe -Dplugin=help  
  2. [INFO] Scanning for projects...  
  3. [INFO]  
  4. [INFO] ------------------------------------------------------------------------  
  5. [INFO] Building Maven Stub Project (No POM) 1  
  6. [INFO] ------------------------------------------------------------------------  
  7. [INFO]  
  8. [INFO] --- maven-help-plugin:2.1.1:describe (default-cli) @ standalone-pom ---  
  9. [INFO] org.apache.maven.plugins:maven-help-plugin:2.1.1  
  10.   
  11. Name: Maven Help Plugin  
  12. Description: The Maven Help plugin provides goals aimed at helping to make  
  13.   sense out of the build environment. It includes the ability to view the  
  14.   effective POM and settings files, after inheritance and active profiles have  
  15.   been applied, as well as a describe a particular plugin goal to give usage  
  16.   information.  
  17. Group Id: org.apache.maven.plugins  
  18. Artifact Id: maven-help-plugin  
  19. Version: 2.1.1  
  20. Goal Prefix: help  
  21.   
  22. This plugin has 9 goals:  
  23.   
  24. help:active-profiles  
  25.   Description: Displays a list of the profiles which are currently active for  
  26.     this build.  
  27.   
  28. help:all-profiles  
  29.   Description: Displays a list of available profiles under the current  
  30.     project.  
  31.     Note: it will list all profiles for a project. If a profile comes up with a  
  32.     status inactive then there might be a need to set profile activation  
  33.     switches/property.  
  34.   
  35. help:describe  
  36.   Description: Displays a list of the attributes for a Maven Plugin and/or  
  37.     goals (aka Mojo - Maven plain Old Java Object).  
  38.   
  39. help:effective-pom  
  40.   Description: Displays the effective POM as an XML for this build, with the  
  41.     active profiles factored in.  
  42.   
  43. help:effective-settings  
  44.   Description: Displays the calculated settings as XML for this project,  
  45.     given any profile enhancement and the inheritance of the global settings  
  46.     into the user-level settings.  
  47.   
  48. help:evaluate  
  49.   Description: Evaluates Maven expressions given by the user in an  
  50.     interactive mode.  
  51.   
  52. help:expressions  
  53.   Description: Displays the supported Plugin expressions used by Maven.  
  54.   
  55. help:help  
  56.   Description: Display help information on maven-help-plugin.  
  57.     Call  
  58.       mvn help:help -Ddetail=true -Dgoal=<goal-name>  
  59.     to display parameter details.  
  60.   
  61. help:system  
  62.   Description: Displays a list of the platform details like system properties  
  63.     and environment variables.  
  64.   
  65. For more information, run 'mvn help:describe [...] -Ddetail'  
  66.   
  67. [INFO] ------------------------------------------------------------------------  
  68. [INFO] BUILD SUCCESS  
  69. [INFO] ------------------------------------------------------------------------  
  70. [INFO] Total time: 1.078s  
  71. [INFO] Finished at: Sun Jul 21 15:47:09 CST 2013  
  72. [INFO] Final Memory: 4M/7M  
  73. [INFO] ------------------------------------------------------------------------  

(2)使用Help 插件输出完整的带有参数的目标列

mvn help:describe -Dplugin=插件名称 -Dfull

例:mvn help:describe -Dplugin=help -Dfull

[plain]  view plain copy
  1. H:\develop\workspace>mvn help:describe -Dplugin=help -Dfull  
  2. [INFO] Scanning for projects...  
  3. [INFO]  
  4. [INFO] ------------------------------------------------------------------------  
  5. [INFO] Building Maven Stub Project (No POM) 1  
  6. [INFO] ------------------------------------------------------------------------  
  7. [INFO]  
  8. [INFO] --- maven-help-plugin:2.1.1:describe (default-cli) @ standalone-pom ---  
  9. [INFO] org.apache.maven.plugins:maven-help-plugin:2.1.1  
  10.   
  11. Name: Maven Help Plugin  
  12. Description: The Maven Help plugin provides goals aimed at helping to make  
  13.   sense out of the build environment. It includes the ability to view the  
  14.   effective POM and settings files, after inheritance and active profiles have  
  15.   been applied, as well as a describe a particular plugin goal to give usage  
  16.   information.  
  17. Group Id: org.apache.maven.plugins  
  18. Artifact Id: maven-help-plugin  
  19. Version: 2.1.1  
  20. Goal Prefix: help  
  21.   
  22. This plugin has 9 goals:  
  23.   
  24. help:active-profiles  
  25.   Description: Displays a list of the profiles which are currently active for  
  26.     this build.  
  27.   Implementation: org.apache.maven.plugins.help.ActiveProfilesMojo  
  28.   Language: java  
  29.   
  30.   Available parameters:  
  31.   
  32.     output  
  33.       Expression: ${output}  
  34.       Optional parameter to write the output of this help in a given file,  
  35.       instead of writing to the console.  
  36.       Note: Could be a relative path.  
  37.   
  38. help:all-profiles  
  39.   Description: Displays a list of available profiles under the current  
  40.     project.  
  41.     Note: it will list all profiles for a project. If a profile comes up with a  
  42.     status inactive then there might be a need to set profile activation  
  43.     switches/property.  
  44.   Implementation: org.apache.maven.plugins.help.AllProfilesMojo  
  45.   Language: java  
  46.   
  47.   Available parameters:  
  48.   
  49.     output  
  50.       Expression: ${output}  
  51.       Optional parameter to write the output of this help in a given file,  
  52.       instead of writing to the console.  
  53.       Note: Could be a relative path.  
  54.   
  55. help:describe  
  56.   Description: Displays a list of the attributes for a Maven Plugin and/or  
  57.     goals (aka Mojo - Maven plain Old Java Object).  
  58.   Implementation: org.apache.maven.plugins.help.DescribeMojo  
  59.   Language: java  
  60.   
  61.   Available parameters:  
  62.   
  63.     artifactId  
  64.       Expression: ${artifactId}  
  65.       The Maven Plugin artifactId to describe.  
  66.       Note: Should be used with groupId parameter.  
  67.   
  68.     cmd  
  69.       Expression: ${cmd}  
  70.       A Maven command like a single goal or a single phase following the Maven  
  71.       command line:  
  72.       mvn [options] [<goal(s)>] [<phase(s)>]  
  73.   
  74.     detail (Default: false)  
  75.       Expression: ${detail}  
  76.       This flag specifies that a detailed (verbose) list of goal (Mojo)  
  77.       information should be given.  
  78.   
  79.     goal  
  80.       Expression: ${goal}  
  81.       The goal name of a Mojo to describe within the specified Maven Plugin. If  
  82.       this parameter is specified, only the corresponding goal (Mojo) will be  
  83.       described, rather than the whole Plugin.  
  84.   
  85.     groupId  
  86.       Expression: ${groupId}  
  87.       The Maven Plugin groupId to describe.  
  88.       Note: Should be used with artifactId parameter.  
  89.   
  90.     medium (Default: true)  
  91.       Expression: ${medium}  
  92.       This flag specifies that a medium list of goal (Mojo) information should  
  93.       be given.  
  94.   
  95.     minimal (Default: false)  
  96.       Expression: ${minimal}  
  97.       This flag specifies that a minimal list of goal (Mojo) information should  
  98.       be given.  
  99.   
  100.     output  
  101.       Expression: ${output}  
  102.       Optional parameter to write the output of this help in a given file,  
  103.       instead of writing to the console.  
  104.       Note: Could be a relative path.  
  105.   
  106.     plugin  
  107.       Expression: ${plugin}  
  108.       The Maven Plugin to describe. This must be specified in one of three  
  109.       ways:  
  110.   
  111.       1.  plugin-prefix, i.e. 'help'  
  112.       2.  groupId:artifactId, i.e. 'org.apache.maven.plugins:maven-help-plugin'  
  113.       3.  groupId:artifactId:version, i.e.  
  114.         'org.apache.maven.plugins:maven-help-plugin:2.0'  
  115.   
  116.     version  
  117.       Expression: ${version}  
  118.       The Maven Plugin version to describe.  
  119.       Note: Should be used with groupId/artifactId parameters.  
  120.   
  121. help:effective-pom  
  122.   Description: Displays the effective POM as an XML for this build, with the  
  123.     active profiles factored in.  
  124.   Implementation: org.apache.maven.plugins.help.EffectivePomMojo  
  125.   Language: java  
  126.   
  127.   Available parameters:  
  128.   
  129.     output  
  130.       Expression: ${output}  
  131.       Optional parameter to write the output of this help in a given file,  
  132.       instead of writing to the console.  
  133.       Note: Could be a relative path.  
  134.   
  135. help:effective-settings  
  136.   Description: Displays the calculated settings as XML for this project,  
  137.     given any profile enhancement and the inheritance of the global settings  
  138.     into the user-level settings.  
  139.   Implementation: org.apache.maven.plugins.help.EffectiveSettingsMojo  
  140.   Language: java  
  141.   
  142.   Available parameters:  
  143.   
  144.     output  
  145.       Expression: ${output}  
  146.       Optional parameter to write the output of this help in a given file,  
  147.       instead of writing to the console.  
  148.       Note: Could be a relative path.  
  149.   
  150.     showPasswords (Default: false)  
  151.       Expression: ${showPasswords}  
  152.       For security reasons, all passwords are hidden by default. Set this to  
  153.       true to show all passwords.  
  154.   
  155. help:evaluate  
  156.   Description: Evaluates Maven expressions given by the user in an  
  157.     interactive mode.  
  158.   Implementation: org.apache.maven.plugins.help.EvaluateMojo  
  159.   Language: java  
  160.   
  161.   Available parameters:  
  162.   
  163.     artifact  
  164.       Expression: ${artifact}  
  165.       An artifact for evaluating Maven expressions.  
  166.       Note: Should respect the Maven format, i.e.  
  167.       groupId:artifactId[:version][:classifier].  
  168.   
  169.     expression  
  170.       Expression: ${expression}  
  171.       An expression to evaluate instead of prompting. Note that this must not  
  172.       include the surrounding ${...}.  
  173.   
  174. help:expressions  
  175.   Description: Displays the supported Plugin expressions used by Maven.  
  176.   Implementation: org.apache.maven.plugins.help.ExpressionsMojo  
  177.   Language: java  
  178.   
  179.   Available parameters:  
  180.   
  181.     output  
  182.       Expression: ${output}  
  183.       Optional parameter to write the output of this help in a given file,  
  184.       instead of writing to the console.  
  185.       Note: Could be a relative path.  
  186.   
  187. help:help  
  188.   Description: Display help information on maven-help-plugin.  
  189.     Call  
  190.       mvn help:help -Ddetail=true -Dgoal=<goal-name>  
  191.     to display parameter details.  
  192.   Implementation: org.apache.maven.plugins.help.HelpMojo  
  193.   Language: java  
  194.   
  195.   Available parameters:  
  196.   
  197.     detail (Default: false)  
  198.       Expression: ${detail}  
  199.       If true, display all settable properties for each goal.  
  200.   
  201.     goal  
  202.       Expression: ${goal}  
  203.       The name of the goal for which to show help. If unspecified, all goals  
  204.       will be displayed.  
  205.   
  206.     indentSize (Default: 2)  
  207.       Expression: ${indentSize}  
  208.       The number of spaces per indentation level, should be positive.  
  209.   
  210.     lineLength (Default: 80)  
  211.       Expression: ${lineLength}  
  212.       The maximum length of a display line, should be positive.  
  213.   
  214. help:system  
  215.   Description: Displays a list of the platform details like system properties  
  216.     and environment variables.  
  217.   Implementation: org.apache.maven.plugins.help.SystemMojo  
  218.   Language: java  
  219.   
  220.   Available parameters:  
  221.   
  222.     output  
  223.       Expression: ${output}  
  224.       Optional parameter to write the output of this help in a given file,  
  225.       instead of writing to the console.  
  226.       Note: Could be a relative path.  
  227.   
  228.   
  229. [INFO] ------------------------------------------------------------------------  
  230. [INFO] BUILD SUCCESS  
  231. [INFO] ------------------------------------------------------------------------  
  232. [INFO] Total time: 1.235s  
  233. [INFO] Finished at: Sun Jul 21 15:50:08 CST 2013  
  234. [INFO] Final Memory: 4M/7M  
  235. [INFO] ------------------------------------------------------------------------  

(3)获取单个目标的信息,设置  mojo 参数和  plugin 参数

mvn help:describe -Dplugin=插件名称 -Dmojo=goal目标 -Dfull

mvn help:describe -Dplugin=help -Dmojo=describe -Dfull

[plain]  view plain copy
  1. H:\develop\workspace>mvn help:describe -Dplugin=help -Dmojo=describe -Dfull  
  2. [INFO] Scanning for projects...  
  3. [INFO]  
  4. [INFO] ------------------------------------------------------------------------  
  5. [INFO] Building Maven Stub Project (No POM) 1  
  6. [INFO] ------------------------------------------------------------------------  
  7. [INFO]  
  8. [INFO] --- maven-help-plugin:2.1.1:describe (default-cli) @ standalone-pom ---  
  9. [INFO] Mojo: 'help:describe'  
  10. help:describe  
  11.   Description: Displays a list of the attributes for a Maven Plugin and/or  
  12.     goals (aka Mojo - Maven plain Old Java Object).  
  13.   Implementation: org.apache.maven.plugins.help.DescribeMojo  
  14.   Language: java  
  15.   
  16.   Available parameters:  
  17.   
  18.     artifactId  
  19.       Expression: ${artifactId}  
  20.       The Maven Plugin artifactId to describe.  
  21.       Note: Should be used with groupId parameter.  
  22.   
  23.     cmd  
  24.       Expression: ${cmd}  
  25.       A Maven command like a single goal or a single phase following the Maven  
  26.       command line:  
  27.       mvn [options] [<goal(s)>] [<phase(s)>]  
  28.   
  29.     detail (Default: false)  
  30.       Expression: ${detail}  
  31.       This flag specifies that a detailed (verbose) list of goal (Mojo)  
  32.       information should be given.  
  33.   
  34.     goal  
  35.       Expression: ${goal}  
  36.       The goal name of a Mojo to describe within the specified Maven Plugin. If  
  37.       this parameter is specified, only the corresponding goal (Mojo) will be  
  38.       described, rather than the whole Plugin.  
  39.   
  40.     groupId  
  41.       Expression: ${groupId}  
  42.       The Maven Plugin groupId to describe.  
  43.       Note: Should be used with artifactId parameter.  
  44.   
  45.     medium (Default: true)  
  46.       Expression: ${medium}  
  47.       This flag specifies that a medium list of goal (Mojo) information should  
  48.       be given.  
  49.   
  50.     minimal (Default: false)  
  51.       Expression: ${minimal}  
  52.       This flag specifies that a minimal list of goal (Mojo) information should  
  53.       be given.  
  54.   
  55.     output  
  56.       Expression: ${output}  
  57.       Optional parameter to write the output of this help in a given file,  
  58.       instead of writing to the console.  
  59.       Note: Could be a relative path.  
  60.   
  61.     plugin  
  62.       Expression: ${plugin}  
  63.       The Maven Plugin to describe. This must be specified in one of three  
  64.       ways:  
  65.   
  66.       1.  plugin-prefix, i.e. 'help'  
  67.       2.  groupId:artifactId, i.e. 'org.apache.maven.plugins:maven-help-plugin'  
  68.       3.  groupId:artifactId:version, i.e.  
  69.         'org.apache.maven.plugins:maven-help-plugin:2.0'  
  70.   
  71.     version  
  72.       Expression: ${version}  
  73.       The Maven Plugin version to describe.  
  74.       Note: Should be used with groupId/artifactId parameters.  
  75.   
  76.   
  77. [INFO] ------------------------------------------------------------------------  
  78. [INFO] BUILD SUCCESS  
  79. [INFO] ------------------------------------------------------------------------  
  80. [INFO] Total time: 1.094s  
  81. [INFO] Finished at: Sun Jul 21 15:52:43 CST 2013  
  82. [INFO] Final Memory: 4M/7M  
  83. [INFO] ------------------------------------------------------------------------  

(4)mvn dependency:tree 打印整个依赖树

(5)发布第三方jar到本地仓库

mvn install:install-file -DgroupId=com -DartifactId=client -Dversion=0.1.0 -Dpackaging=jar -Dfile=d:\client-0.1.0.jar 

(6)当pom文件名不是pom.xml时,通过参数指定pom文件的路径

mvn -f pom路径

例:mvn -f d:/pom_test.xml

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值