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

H:\develop\workspace>mvn help:describe -Dplugin=help
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-help-plugin:2.1.1:describe (default-cli) @ standalone-pom ---
[INFO] org.apache.maven.plugins:maven-help-plugin:2.1.1

Name: Maven Help Plugin
Description: The Maven Help plugin provides goals aimed at helping to make
  sense out of the build environment. It includes the ability to view the
  effective POM and settings files, after inheritance and active profiles have
  been applied, as well as a describe a particular plugin goal to give usage
  information.
Group Id: org.apache.maven.plugins
Artifact Id: maven-help-plugin
Version: 2.1.1
Goal Prefix: help

This plugin has 9 goals:

help:active-profiles
  Description: Displays a list of the profiles which are currently active for
    this build.

help:all-profiles
  Description: Displays a list of available profiles under the current
    project.
    Note: it will list all profiles for a project. If a profile comes up with a
    status inactive then there might be a need to set profile activation
    switches/property.

help:describe
  Description: Displays a list of the attributes for a Maven Plugin and/or
    goals (aka Mojo - Maven plain Old Java Object).

help:effective-pom
  Description: Displays the effective POM as an XML for this build, with the
    active profiles factored in.

help:effective-settings
  Description: Displays the calculated settings as XML for this project,
    given any profile enhancement and the inheritance of the global settings
    into the user-level settings.

help:evaluate
  Description: Evaluates Maven expressions given by the user in an
    interactive mode.

help:expressions
  Description: Displays the supported Plugin expressions used by Maven.

help:help
  Description: Display help information on maven-help-plugin.
    Call
      mvn help:help -Ddetail=true -Dgoal=<goal-name>
    to display parameter details.

help:system
  Description: Displays a list of the platform details like system properties
    and environment variables.

For more information, run 'mvn help:describe [...] -Ddetail'

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.078s
[INFO] Finished at: Sun Jul 21 15:47:09 CST 2013
[INFO] Final Memory: 4M/7M
[INFO] ------------------------------------------------------------------------

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

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

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

H:\develop\workspace>mvn help:describe -Dplugin=help -Dfull
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-help-plugin:2.1.1:describe (default-cli) @ standalone-pom ---
[INFO] org.apache.maven.plugins:maven-help-plugin:2.1.1

Name: Maven Help Plugin
Description: The Maven Help plugin provides goals aimed at helping to make
  sense out of the build environment. It includes the ability to view the
  effective POM and settings files, after inheritance and active profiles have
  been applied, as well as a describe a particular plugin goal to give usage
  information.
Group Id: org.apache.maven.plugins
Artifact Id: maven-help-plugin
Version: 2.1.1
Goal Prefix: help

This plugin has 9 goals:

help:active-profiles
  Description: Displays a list of the profiles which are currently active for
    this build.
  Implementation: org.apache.maven.plugins.help.ActiveProfilesMojo
  Language: java

  Available parameters:

    output
      Expression: ${output}
      Optional parameter to write the output of this help in a given file,
      instead of writing to the console.
      Note: Could be a relative path.

help:all-profiles
  Description: Displays a list of available profiles under the current
    project.
    Note: it will list all profiles for a project. If a profile comes up with a
    status inactive then there might be a need to set profile activation
    switches/property.
  Implementation: org.apache.maven.plugins.help.AllProfilesMojo
  Language: java

  Available parameters:

    output
      Expression: ${output}
      Optional parameter to write the output of this help in a given file,
      instead of writing to the console.
      Note: Could be a relative path.

help:describe
  Description: Displays a list of the attributes for a Maven Plugin and/or
    goals (aka Mojo - Maven plain Old Java Object).
  Implementation: org.apache.maven.plugins.help.DescribeMojo
  Language: java

  Available parameters:

    artifactId
      Expression: ${artifactId}
      The Maven Plugin artifactId to describe.
      Note: Should be used with groupId parameter.

    cmd
      Expression: ${cmd}
      A Maven command like a single goal or a single phase following the Maven
      command line:
      mvn [options] [<goal(s)>] [<phase(s)>]

    detail (Default: false)
      Expression: ${detail}
      This flag specifies that a detailed (verbose) list of goal (Mojo)
      information should be given.

    goal
      Expression: ${goal}
      The goal name of a Mojo to describe within the specified Maven Plugin. If
      this parameter is specified, only the corresponding goal (Mojo) will be
      described, rather than the whole Plugin.

    groupId
      Expression: ${groupId}
      The Maven Plugin groupId to describe.
      Note: Should be used with artifactId parameter.

    medium (Default: true)
      Expression: ${medium}
      This flag specifies that a medium list of goal (Mojo) information should
      be given.

    minimal (Default: false)
      Expression: ${minimal}
      This flag specifies that a minimal list of goal (Mojo) information should
      be given.

    output
      Expression: ${output}
      Optional parameter to write the output of this help in a given file,
      instead of writing to the console.
      Note: Could be a relative path.

    plugin
      Expression: ${plugin}
      The Maven Plugin to describe. This must be specified in one of three
      ways:

      1.  plugin-prefix, i.e. 'help'
      2.  groupId:artifactId, i.e. 'org.apache.maven.plugins:maven-help-plugin'
      3.  groupId:artifactId:version, i.e.
        'org.apache.maven.plugins:maven-help-plugin:2.0'

    version
      Expression: ${version}
      The Maven Plugin version to describe.
      Note: Should be used with groupId/artifactId parameters.

help:effective-pom
  Description: Displays the effective POM as an XML for this build, with the
    active profiles factored in.
  Implementation: org.apache.maven.plugins.help.EffectivePomMojo
  Language: java

  Available parameters:

    output
      Expression: ${output}
      Optional parameter to write the output of this help in a given file,
      instead of writing to the console.
      Note: Could be a relative path.

help:effective-settings
  Description: Displays the calculated settings as XML for this project,
    given any profile enhancement and the inheritance of the global settings
    into the user-level settings.
  Implementation: org.apache.maven.plugins.help.EffectiveSettingsMojo
  Language: java

  Available parameters:

    output
      Expression: ${output}
      Optional parameter to write the output of this help in a given file,
      instead of writing to the console.
      Note: Could be a relative path.

    showPasswords (Default: false)
      Expression: ${showPasswords}
      For security reasons, all passwords are hidden by default. Set this to
      true to show all passwords.

help:evaluate
  Description: Evaluates Maven expressions given by the user in an
    interactive mode.
  Implementation: org.apache.maven.plugins.help.EvaluateMojo
  Language: java

  Available parameters:

    artifact
      Expression: ${artifact}
      An artifact for evaluating Maven expressions.
      Note: Should respect the Maven format, i.e.
      groupId:artifactId[:version][:classifier].

    expression
      Expression: ${expression}
      An expression to evaluate instead of prompting. Note that this must not
      include the surrounding ${...}.

help:expressions
  Description: Displays the supported Plugin expressions used by Maven.
  Implementation: org.apache.maven.plugins.help.ExpressionsMojo
  Language: java

  Available parameters:

    output
      Expression: ${output}
      Optional parameter to write the output of this help in a given file,
      instead of writing to the console.
      Note: Could be a relative path.

help:help
  Description: Display help information on maven-help-plugin.
    Call
      mvn help:help -Ddetail=true -Dgoal=<goal-name>
    to display parameter details.
  Implementation: org.apache.maven.plugins.help.HelpMojo
  Language: java

  Available parameters:

    detail (Default: false)
      Expression: ${detail}
      If true, display all settable properties for each goal.

    goal
      Expression: ${goal}
      The name of the goal for which to show help. If unspecified, all goals
      will be displayed.

    indentSize (Default: 2)
      Expression: ${indentSize}
      The number of spaces per indentation level, should be positive.

    lineLength (Default: 80)
      Expression: ${lineLength}
      The maximum length of a display line, should be positive.

help:system
  Description: Displays a list of the platform details like system properties
    and environment variables.
  Implementation: org.apache.maven.plugins.help.SystemMojo
  Language: java

  Available parameters:

    output
      Expression: ${output}
      Optional parameter to write the output of this help in a given file,
      instead of writing to the console.
      Note: Could be a relative path.


[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.235s
[INFO] Finished at: Sun Jul 21 15:50:08 CST 2013
[INFO] Final Memory: 4M/7M
[INFO] ------------------------------------------------------------------------

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

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

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

H:\develop\workspace>mvn help:describe -Dplugin=help -Dmojo=describe -Dfull
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-help-plugin:2.1.1:describe (default-cli) @ standalone-pom ---
[INFO] Mojo: 'help:describe'
help:describe
  Description: Displays a list of the attributes for a Maven Plugin and/or
    goals (aka Mojo - Maven plain Old Java Object).
  Implementation: org.apache.maven.plugins.help.DescribeMojo
  Language: java

  Available parameters:

    artifactId
      Expression: ${artifactId}
      The Maven Plugin artifactId to describe.
      Note: Should be used with groupId parameter.

    cmd
      Expression: ${cmd}
      A Maven command like a single goal or a single phase following the Maven
      command line:
      mvn [options] [<goal(s)>] [<phase(s)>]

    detail (Default: false)
      Expression: ${detail}
      This flag specifies that a detailed (verbose) list of goal (Mojo)
      information should be given.

    goal
      Expression: ${goal}
      The goal name of a Mojo to describe within the specified Maven Plugin. If
      this parameter is specified, only the corresponding goal (Mojo) will be
      described, rather than the whole Plugin.

    groupId
      Expression: ${groupId}
      The Maven Plugin groupId to describe.
      Note: Should be used with artifactId parameter.

    medium (Default: true)
      Expression: ${medium}
      This flag specifies that a medium list of goal (Mojo) information should
      be given.

    minimal (Default: false)
      Expression: ${minimal}
      This flag specifies that a minimal list of goal (Mojo) information should
      be given.

    output
      Expression: ${output}
      Optional parameter to write the output of this help in a given file,
      instead of writing to the console.
      Note: Could be a relative path.

    plugin
      Expression: ${plugin}
      The Maven Plugin to describe. This must be specified in one of three
      ways:

      1.  plugin-prefix, i.e. 'help'
      2.  groupId:artifactId, i.e. 'org.apache.maven.plugins:maven-help-plugin'
      3.  groupId:artifactId:version, i.e.
        'org.apache.maven.plugins:maven-help-plugin:2.0'

    version
      Expression: ${version}
      The Maven Plugin version to describe.
      Note: Should be used with groupId/artifactId parameters.


[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.094s
[INFO] Finished at: Sun Jul 21 15:52:43 CST 2013
[INFO] Final Memory: 4M/7M
[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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值