Congratulations! You are halfway through the book. As discussed in earlier chapters, Maven follows convention over configuration; this implies there is a default build mechanism in place. The build mechanism, often termed as the build lifecycle, forms a sequence of steps grouped together in phases (also known as
stages). Each phase is accompanied with a set of goals that define the unit of task. In this chapter, we will look at three standard lifecycles—clean, default, and site—and get acquainted with other common lifecycles. You will also get to know about building and running the hello-project
, which was created in Chapter 3, Creating and Importing Projects. This chapter covers the following sections:
Building a Maven project results in the execution of set goals grouped in phases. Though there is a default build cycle of Maven, it can be customized to suit our needs; that's the beauty Maven inherits. To ascertain, it is essential to have knowledge of the build's lifecycle. Essentially, the following are the three standard lifecycles in Maven:
The default lifecycle handles the build of the project and its deployment. It is the primary lifecycle of Maven and is also known as the build lifecycle. In general, it provides the build process model for Java applications. There are 23 phases for the default lifecycle that starts with validation and ends with deploy. For details on all 23 phases, please refer to http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference.
However, here we will see some of the phases and the default associated goals that need attention for common application development, which are as follows:
The clean lifecycle is the simplest lifecycle in Maven, and it consists of the following phases:
Out of these phases, the one that gathers our interest is the clean phase. The Maven "clean:clean
" goal is bound to the clean phase. It cleans the project's build
(usually target
) directory. Executing any one phase result in execution of all phases up to it and the phase itself, for example, a call of a clean phase would execute the first pre-clean phase and then the clean phase; similarly, a call of post-clean results in the calling of pre-clean, clean, and post-clean phases. The following diagram illustrates the execution of the clean lifecycle phases (reference: the Apache Maven site):
We can bind other goals to the phases of the clean lifecycle. Suppose we want to echo some message on the pre-clean phase; we can achieve this by binding the maven-antrun-plugin:run
goal to this phase, which can be done as follows:
</project> ........... <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <id>precleanid</id> <phase>pre-clean</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>Hello am in pre-clean phase</echo> </tasks> </configuration> </execution>
The site lifecycle handles the creation of the project site documentation. The phases of a site lifecycle are shown in the following diagram:
The following table describes the site lifecycle phases in the order of execution. (reference: Apache Maven website)
Executing any one phase results in the execution of all phases up to it and the phase itself. For example, calling post-site results in the execution of pre-site, site, and post-site phases. Similar to the clean lifecycle, we can bind other goals to the site's lifecycle.
18.116.21.152