Chapter 4. Building and Running a Project

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:

  • Build lifecycle
    • Default lifecycle
    • Clean lifecycle
    • Site lifecycle
  • Package-specific lifecycle
  • The Maven console
  • Building and packaging projects
  • Running hello-project

The build lifecycle

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:

  • Default
  • Clean
  • Site

The default lifecycle

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:

Lifecycle phases

Description

Plugin:goals

validate

This validates that the project is correct and contains all the necessary information to perform the build operation

-

compile

This compiles the source code

compiler:compile

test-compile

This compiles the test source code in the test destination directory

compiler:testCompile

test

This runs the test using suitable unit testing framework as configured in the pom file

surefire:test

package

This packages the compiled source code in the corresponding distributable format such as JAR, WAR, EAR, and so on

jar:jar (for JAR packaging)

install

This installs the package in the local repository, which can act as a dependency for other projects

install:install

deploy

This copies the final package to a remote repository to share with other developers and projects

deploy:deploy

The clean lifecycle

The clean lifecycle is the simplest lifecycle in Maven, and it consists of the following phases:

  • pre-clean: This phase executes the process needed before a project's clean up
  • clean: This phase removes all files built by an earlier build (the target directory)
  • post-clean: This phase executes the process required after a project's cleanup

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):

The clean lifecycle

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

The site lifecycle handles the creation of the project site documentation. The phases of a site lifecycle are shown in the following diagram:

The site lifecycle

The following table describes the site lifecycle phases in the order of execution. (reference: Apache Maven website)

Phases

Description

pre-site

This phase executes processes needed before the generation of a project site.

site

This phase generates documentation of a project site

post-site

This phase executes a process required after a site's generation and to prepare for site deployment

site-deploy

This phase deploys the generated site documentation to the specified web server

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.21.152