美文网首页
[Maven] 随便聊聊 Maven 中的几个概念

[Maven] 随便聊聊 Maven 中的几个概念

作者: jyjz2008 | 来源:发表于2020-12-26 22:12 被阅读0次

    参考资料

    1. Maven 官网入口
    2. Maven 官网提供的 lifecycle 的介绍

    四大概念

    Maven 中有四个概念经常出现

    • 生命周期(lifecycle)
    • 阶段(phase)
    • 插件(plugin)
    • 目标(goal)

    下面分别介绍

    lifecycle 与 phase

    Introduction to the Build Lifecycle 一文介绍了 lifecyclephase 的内容。
    lifecycle 分为以下三种

    1. default
    2. clean
    3. site

    原文如下

    There are three built-in build lifecycles: default, clean and site. The default lifecycle handles your project deployment, the clean lifecycle handles project cleaning, while the site lifecycle handles the creation of your project's site documentation.

    三大 lifecycle

    Clean Lifecycle

    https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference 所述,clean lifecycle 中有3个 phase,具体如下

    Phase Description
    pre-clean execute processes needed prior to the actual project cleaning
    clean remove all files generated by the previous build
    post-clean execute processes needed to finalize the project cleaning

    这里强调一下,clean lifecycle 中刚好有一个 phase 也叫 clean(maven-clean-plugin 的插件前缀是 clean, 它有一个 goal 也叫 clean)。
    在命令行输入

    mvn clean
    

    时,这个 clean 是一个 phase(这个 phase 属于 clean lifecycle)
    在命令行输入

    mvn clean:clean
    

    时,: 前的 clean 表示 maven-clean-plugin(clean 是这个插件的插件前缀)
    : 后的 clean 表示 maven-clean-plugin 的名为 cleangoal

    Default Lifecycle

    default lifecycle 中共有23个 phase,这里只列举其中的几个,完整的列表请参考 https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference

    • compile (compile the source code of the project.)
    • test-compile (compile the test source code into the test destination directory)
    • test (run tests using a suitable unit testing framework. These tests should not require the code be packaged or deployed.)

    请注意 maven-compiler-plugin 有一个 goal 也叫 compile,不要将两者弄混。
    在命令行输入

    mvn compile
    

    时,compiledefault lifecycle 的一个 phase
    在命令行输入

    mvn compiler:compile
    

    时,compiler 表示 maven-compiler-plugin(compilermaven-compiler-plugin 的插件前缀),而 compile 则是 maven-compiler-plugin 的一个 goal

    Site Lifecycle

    site lifecycle 中总共有4个phase,详情请参考 https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference

    这里举几个生活中的例子,便于理解 lifecyclephase

    生活中的 lifecycle 和 phase

    月相

    平时我们能看到月相(lunar phase)的变化,其实可以把从初一到月底的月相变化看成一个 lifecycle

    游戏

    在玩不能存档的闯关游戏时,我们每次都需要从第1关开始。假设某个闯关游戏有10关,我们就可以把这10关看作10个phase,而整个游戏就可以看作1个 lifecycle

    学生时代

    不少人的学生时代刚好是小学、初中、高中、大学这四些阶段。那么这四个阶段就可以看成4个 phase,而学生时代则可以看成1个lifecycle

    plugin 和 goal

    一个 plugin 可以做若干件事情,例如 maven-compiler-plugin 既可以编译 source code,也可以编译 test source code。所以一个 plugin 可以有若干个 goal
    可以将补习班看做一个 plugin,它有

    1. 小学补习班
    2. 初中补习班
    3. 高中补习班
    4. 贩卖焦虑

    这四个goal。其中,前三个 goal 分别和学生时代这个 lifecycle 中的如下 phase 绑定

    1. 小学
    2. 初中
    3. 高中

    回到 Maven,我们在命令行输入

    mvn compiler:help
    

    就会看到类似下图的效果

    maven-compiler-plugin 有 3 个 goal
    在图中可以看到 maven-compiler-plugin 有如下的 3 个 goal
    1. compile (这里 compile 是一个 goal,请不要和名为 compilephase 弄混)
    2. help
    3. testCompile

    在命令行输入如下命令

    mvn help:describe -Dcmd=compile
    

    就会看到

    1. compile 这个 phase 是与 maven-compiler-plugincompile 这个 goal 绑定的(请注意,有1个 phase 就叫 compile, 有1个goal也叫compile,两者刚好同名)
    2. default lifecycle 中虽然有23个phase,但是(在pom文件的packagingjar的情况下)只有8个phase有(默认的)与goal的绑定关系(具体的绑定关系可以在下图中看到)
      mvn help:describe -Dcmd=compile 的结果

    具体的绑定关系也可以参考 https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Built-in_Lifecycle_Bindings 的描述

    相关文章

      网友评论

          本文标题:[Maven] 随便聊聊 Maven 中的几个概念

          本文链接:https://www.haomeiwen.com/subject/jrcnnktx.html