2

When I was reading about Maven goals, phases on Stack Overflow, I came across two links in which one says:

when you execute maven you can specify a goal or a phase.

What is the difference/relation between maven goals and phases?

and the other says:

You can't call the life-cycle-phase itself but you can call the goals of the plugins which are bound to the life-cycle-phases.

Executing a specific Maven phase

Which one is right? Or am I not understanding it?

Also can some give me simple examples Maven executing lifecycle/phase/goal. And also Maven knows that it has to run phase or a goal? E.g. when I say mvn install, is it install phase or goal?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
s4194313
  • 39
  • 6

2 Answers2

8

Bombya Bo,

Think of Maven build life cycle like a fancy meal which has sequential phases:

  • starter (resources)
  • main (compile)
  • dessert (package)
  • coffee (install)
  • digestif (deploy)

A goal is the actual food being served during that phase.

  • starter: gazpacho
  • main: steak; gravy; fries
  • dessert: tiramisu
  • coffee: cappuccino

In this analogy:

  • pom.xml file is the equivalent of today's menu written on the chalk board
  • calling mvn on the command line is the equivalent of placing your order to the waiter
  • you can have more than one item of food/goal bound to a single phase (steak+gravy+fries)

If you like the house defaults, then you can just order the first N courses off the fixed menu:

"I'll take the 2 course meal"
$ mvn compile

You'll get everything up to and including the main course (ie gazpacho followed by steak+gravy+fries).

Calling a single goal is the equivalent of ordering a la carte:

"I'll take a Cobb salad plus the 2 course meal"
$ mvn javadoc:javadoc compile

If you want that goal to become a permanent addition to the menu, then add it to the pom file. That brings us back to calling:

$ mvn compile

which results in gazpacho+Cobb, followed by steak+gravy+fries.

A last point about binding a goal to a phase.

By default, each goal will run during a certain phase (Cobb salad usually served as a starter). But you can override the phase binding, which is like telling the waiter "I'll have a Cobb salad, but bring it at the same time as the main"

Hope this clarifies the intuition behind goals vs. phases.

333kenshin
  • 1,995
  • 12
  • 17
  • nice example ! pls confirm when you execute maven a goal ie mvn [goal] is that all phases before that goal will be executed ? like if i execute mvn deploy:depoly then all the phases before that goal like compile , test will be executed ? or just deploy ? – s4194313 Oct 30 '14 at 17:09
  • Calling mvn goal like deploy:deploy goal will only perform that single action ("i just want a cappuccino"). Caling mvn phase like deploy tells maven to perform all goals leading up to and including it ("I'll take 3 course meal") – 333kenshin Oct 31 '14 at 00:01
1

When you invoke a single plugin, you need to invoke a goal of that plugin. Like

mvn dependency:tree

in this case you are invoking the goal tree of the dependency plugin

On the other hand, you can invoke a phase of maven by simply doing

mvn test

In this case you are invoking not a plugin but a phase. Note that plugin goals can be bound to specific phases as well. The difference is whether you invoke the full phase (and all the preceding phases implicitly) or just a single goal of a single plugin

Here you can find all the phases of the default lifecycle (which can be modified, but i don't think you need to know about that for now), so as you can see, install is a phase

Hilikus
  • 9,954
  • 14
  • 65
  • 118