Maven Goals and Phases tutorial

In this tutorial, you will learn about Maven Goals and Phases and how they are related to each other and the Maven Build Lifecycle. So without further ado,  let’s dive deep into the topic!

Maven Goals and Phases

Goals

A goal is a Java method performing a particular task that contributes to the building of a project. You can access goals by maven plugins that may contain one or more goals. For example, the Jar plugin has five goals:

  • help
  • jar
  • sign
  • sign-verify
  • test-jar

Now if we have a look at the most important goals here, the jar goal creates a jar file for your project main classes, and test-jar creates a jar file for your project test classes. A goal can be invoked directly if it’s not bound to any build phase or even otherwise. Note that a goal may be bound to zero or more build phases. You can execute a goal directly by providing the plugin name along with the name of the goal.

mvn <plugin>:<goal>

Now let’s run the following command that will execute the jar goal from the jar plugin.

mvn jar:jar

After executing this command, you can clearly see that jar will be empty as we have not executed the compile goal yet.

Now let’s run the following command to execute the compile goal from the compiler plugin.

mvn compiler:compile

After executing this command, you can clearly see that the source code has been compiled, and now you are all set to execute the jar goal.

 

After executing the jar goal again, you can see that the jar file has been created in the target folder.

Phases

A Maven phase represents a stage in the Maven build lifecycle. Each phase is responsible for a specific task. A phase can contain a single goal or multiple goals. Note that there can be a phase with no goal at all.  Here you can have a look at some phases and the goals bound to them.

The table shown above contains the phases and associated goals for jar packaging. When you will execute a phase through the command line, the phases defined before that phase will also be executed automatically. Here if you will run the deploy phase,  the previous phases that include process-resources, compile, process-test-resources, test-compile, test, package, and install will also be executed.

Let’s see this through an example by executing the install phase through the following command.

mvn install

Now if you look at how this phase executes, you can clearly see that at first the compile phase was executed followed by the test, package, and install phases respectively (naming the important ones only).

Build Lifecycle

In Maven, the process for building and distributing a project is based on the concept of a build lifecycle. If you want to build a maven project, you can do that easily by knowing only a few Maven commands and the POM will ensure that everything works perfectly fine. There are three built-in build lifecycles.

  • Default
  • Clean
  • Site

A Build Lifecycle is basically an execution of an ordered sequence of phases. Each of the above build lifecycles has a different list of build phases.

Default Lifecycle

The default lifecycle handles project deployment and consists of twenty-three phases in total. The image below contains the names and descriptions of the phases for the default lifecycle. These phases will run sequentially to complete the execution of a  default lifecycle. During the execution of a default lifecycle, Maven will go through different phases that include validation of the project, initialization of the build state, compilation of the sources, execution of tests, packaging of binaries, execution of integration test, installation, and then finally deployment of the package.

Clean Lifecycle

The clean lifecycle handles project cleaning and consists of three phases. The following image contains the names and descriptions of the phases. During the execution of the clean lifecycle, Maven will first execute some processes prior to cleaning, remove the previous build files and then execute post-cleaning processes.

Site Lifecycle

The Site Lifecycle handles the creation of the project’s website and consists of four phases. The following image contains the names and descriptions of the phases. During the execution of the site lifecycle, Maven will first execute some processes before the project’s website generation, then generate the project’s website documentation. After that maven will execute processes to finalize the website, prepare it for deployment, and then finally deploy it to a web server.

Note that each of these phases can have one, multiple, or no goals as discussed earlier.

Conclusion

With this, we have come to the end of our tutorial. In this tutorial, you learned about maven goals and phases through examples. Stay tuned for some more informative tutorials coming ahead and feel free to leave any feedback in the comments section.

Happy learning!