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

Jeff Levine picture Jeff Levine · Oct 28, 2014 · Viewed 50k times · Source

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:

  • Lifecycle
  • Phase
  • Plugin
  • Goal

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!

Answer

Gerold Broser picture Gerold Broser · Jun 20, 2015

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. 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 goal [with line breaks 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:

... see actual output or 'Maven, Introduction to the Build Lifecycle' at 'References' below ...

References:

If you ever wondered how Maven knows what to do without any goal binding in the POM there's a link to default-bindings.xml at the end which is located in <Your Maven installation>/lib/maven-core-x.y.z.jar/META-INF/plexus/default-bindings.xml.

The phases for the built-in lifecycles (clean, default, site) are declared in <Your Maven installation>/lib/maven-core-x.y.z.jar/META-INF/plexus/components.xml under .../<component>/<role>org.apache.maven.lifecycle.Lifecycle.