mavenmaven-3pom.xmlmaven-lifecycle

Maven: Lifecycle vs. Phase vs. Plugin vs. Goal


Relatively new developer here, even though I've been using it for a little while, I'm hoping to solidify my Maven fundamentals. Part of my problem is that I have no experience with Ant, which seems to be from where many explanations stem. I've been reading and watching tutorials, and I keep hearing the same terms:

From what I've learned, it seems that lifecycle is the broadest of the bunch, and is composed of (or completed by) phases, plugins, and/or goals.

Question: Could you provide any info on how these terms are related and the most common examples?

The more explicit and basic, the better!


Solution

  • A Maven lifecycle is an (abstract) concept that covers all steps (or better: all the steps the Maven designers decided to support) that are expected to occur in a project's development lifetime. These steps (or stages) are called phases in Maven terminology.

    A Maven plugin is a container for/supplier of goals. Code implemented in goals is the real workhorse. (Maven in its core itself is just managing plugins and executing goals). Each of a plugin's goals can be assigned/bound to any of the lifecycle phases.

    When invoking mvn <phase> Maven passes all phases (every time) and executes all goals (supplied by plugins) that have been bound to any of the phases prior and up to (and including) the given phase. If there is a phase with no goal bound to it nothing is done. But the phase is passed nevertheless.

    I.e. you can't "'insert' additional phases" into one of Maven's built-in lifecycles (clean, default, site). They are already there, always! You could develop your own lifecycle with its own phases but that's far beyond simply using Maven as it is.

    Goals can also be executed directly, which you get told when running mvn without any phase or [plugin:]goal:

    [with line breaks, [NB: notes] and shortened for readability here]

    You must specify a valid lifecycle phase or a goal in the format
    
    <plugin-prefix>:<goal> or
    
    <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>.
    
    Available lifecycle phases are:
    
    [NB: Clean lifecycle] pre-clean, clean, post-clean
    
    [NB: Default lifecycle] validate, initialize,
      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,
      pre-integration-test, integration-test, post-integration-test,
      verify, install, deploy
    
    [NB: Site lifecycle] pre-site, site, post-site, site-deploy
    
    ...
    

    See also actual output of mvn or Maven, Introduction to the Build Lifecycle, also at References below.

    References