1

I have got the above output by running an mvn command help:describe -Dcmd=install

If I run a maven install command then it will run all of its previous phases. validate is the first phase of the default maven build life cycle which is not attached to any of the maven goals/plugin by default. Correct me if I am wrong

The phases works if it has a plugin/goal attached to it.

Since the validate phase is not attached to any of the goals by default, does this phase execute if I have not specifically hooked any of the custom goals like below?

 <build>
    <plugins>
        <plugin>
            <artifactId>maven-clean-plugin</artifactId>
            <version>3.1.0</version>
            <executions>
               <execution>
                   <id>custom-clean</id>
                   <phase>validate</phase>
                   <goals>
                       <goal>clean</goal>
                   </goals>
               </execution>
            </executions>
        </plugin>
</build>

If it executes during the default life cycle (without the customization) then how does a phase work without a goal?

Erik
  • 503
  • 1
  • 7
  • 26

1 Answers1

3

default is one of the three available build life cycles and as the name suggests, it is taken into build consideration automatically if no other life cycle is specified. These build lifecycles are defined by a different list of build phases in a sequence, thus a build phase represents a stage in the specific lifecycle.

validate is first phase in default build life cycle.

If we talk about specific to default life cycle below are the main phases which execute in following sequence in order to complete default build life cycle: validate, compile, test, package, verify, install, deploy. Actually there are 21 phases in total in default build life cycle.

If we try to run any specific phase, it will start from validate phase and will execute till the specified phase, which directly means that whenever default build life cycle is considered for building, it will always start with validate phase, doesn't matter which phase you are specifying. A plugin goal is different than build phase, it represents a specific task which contributes to the building and managing of a project.

A plugin goal is not mandatory in order to build or complete a build life cycle, but if it is there, It may be bound to a specific build phase of underlying life cycle or it may not be bound to any build phase. If a plugin goal is not bound to any build phase, it can be executed outside of build life cycle by direct invocation, in that case there is no point of executing validate phase.

Moreover there are no plugins bound to the phases validate, initialize and verify by default and for other few phases also.

Have a look at maven life cycle bindings, which shows that which goals get run in which phase by default, For example compile phase goals will always be executed before test phase goals.

If ywe specify a phase then maven will run all phases up to that phase we specified, in order and for each phase it will run all goals attached to that phase. But if you only specify the goal then it will bind that goal to a given default phase. But when you specify the execution you can also explicitly specify the phase for that goal as well.

One more thing that if we are not specifying any phase for a goal, and goal is not bound to any default phase, it will run only that goal and nothing else.

But here is a catch, If we are trying to run only specific goal, and the build phases required to complete the task specified in goal has not executed previously, it will fail. That is where we will get error saying "Failed to execute goal..."

One good example is trying to execute jar:jar for a jar packaging, if compile phase is not yet executed, this goal run will fail because maven will not get complied code to package as jar.

For more detail look here & here

default maven lifecycle bindings

Update: Short answer: No, maven bypass a phase if there is no goal defined.

A phase without a goal is just a template which has nothing to do. Because by default mvn validate does nothing if there is no user-defined plugin goals. A build phase is comprised of plugin goals. Because validate & initialize has no default plugins bound. No goals(including default & custom/specified both) for a phase that means phase does nothing. If you are not declaring a plugin goal that is bound to the validate phase, the validate phase will do nothing.

You can relate to another scenario: when class loads static members are seeked to load, if no static member is available it skips that phase and continue.

Shailesh Saxena
  • 3,472
  • 2
  • 18
  • 28
  • 1
    Thanks for the answer. However, I would like to get a specific yes or no answer. If I don't have a goal defined in my POM for the phase like **validate** or **initialize**; do these phases execute/run by maven? If yes, is it correct to say the maven can execute a phase without a goal? If No, does it mean maven bypass a phase if there is no goal defined? I can't see maven executing a phase like **validate** without a custom plugin defined in POM(Consider default life cycle). – Abhilash Panigrahi Jan 08 '20 at 02:45
  • 1
    Yes, Shailesh. This is exactly what I wanted to confirm. A maven phase does nothing and it simply depends on the plugin to gets the work done. If a plugin is not configured then it simply bypasses the phase. Thank you for clarifying. – Abhilash Panigrahi Jan 08 '20 at 19:07