Easy Tutorial
❮ Maven Tutorial Maven Deployment Automation ❯

Maven Build Lifecycle

The Maven Build Lifecycle defines the process of building and releasing a project.

A typical Maven build lifecycle consists of the following sequence of phases:

Phase Process Description
Validate Validate the project Ensures the project is correct and all necessary information is available
Compile Perform compilation Source code compilation is done in this phase
Test Test Runs tests using an appropriate unit testing framework, such as JUnit
Package Package Creates JAR/WAR packages as defined in the pom.xml
Verify Verify Checks the results of integration tests to ensure quality standards are met
Install Install Installs the packaged project into the local repository for use by other projects
Deploy Deploy Copies the final project package to the remote repository for sharing with other developers and projects

To complete the default lifecycle, these phases (including other lifecycle phases not listed above) are executed in sequence.

Maven has the following three standard lifecycles:

Build Phases Composed of Plugin Goals

A plugin goal represents a specific task (finer than a build phase) that aids in project building and management. These goals can be bound to multiple phases or unbound. Unbound goals can be executed outside the build lifecycle by direct invocation. The execution order of these goals depends on the order of goal invocation and build phases.

For example, consider the following command:

clean and package are build phases, dependency:copy-dependencies is a goal

mvn clean dependency:copy-dependencies package

Here, the clean phase will be executed first, followed by the dependency:copy-dependencies goal, and finally the package phase.


Clean Lifecycle

When we execute the mvn post-clean command, Maven invokes the clean lifecycle, which includes the following phases:

In mvn clean, clean refers to the above clean phase. In a lifecycle, when a phase is run, all preceding phases are also run. Therefore, if mvn clean is executed, the following two lifecycle phases will be run:

pre-clean, clean

If we run mvn post-clean, the following three lifecycle phases will be run:

pre-clean, clean, post-clean

We can modify the behavior of this part by defining goals in any of the clean lifecycle phases.

In the following example, we add the maven-antrun-plugin:run goal to the pre-clean, clean, and post-clean phases. This allows us to display text messages at various stages of the clean lifecycle.

We have created a pom.xml file in the C:\MVN\project directory.

<project xmlns="http://maven.apache.org/POM/4.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
   http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.companyname.projectgroup</groupId>
<artifactId>project</artifactId>
<version>1.0</version>
<build>
<plugins>
   <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-antrun-plugin</artifactId>
   <version>1.1</version>
   <executions>
      <execution>
         <id>id.pre-clean</id>
<execution>
   <phase>pre-clean</phase>
   <goals>
      <goal>run</goal>
   </goals>
   <configuration>
      <tasks>
         <echo>pre-clean phase</echo>
      </tasks>
   </configuration>
</execution>
<execution>
   <id>id.clean</id>
   <phase>clean</phase>
   <goals>
      <goal>run</goal>
   </goals>
   <configuration>
      <tasks>
         <echo>clean phase</echo>
      </tasks>
   </configuration>
</execution>
<execution>
   <id>id.post-clean</id>
   <phase>post-clean</phase>
   <goals>
      <goal>run</goal>
   </goals>
   <configuration>
      <tasks>
         <echo>post-clean phase</echo>
      </tasks>
   </configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

Now open the command console, navigate to the directory containing the pom.xml, and execute the following mvn command.

C:\MVN\project>mvn post-clean

Maven will start processing and display all phases of the clean lifecycle.

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------
[INFO] Building Unnamed - com.companyname.projectgroup:project:jar:1.0
[INFO]    task-segment: [post-clean]
[INFO] ------------------------------------------------------------------
[INFO] [antrun:run {execution: id.pre-clean}]
[INFO] Executing tasks
     [echo] pre-clean phase
[INFO] Executed tasks
[INFO] [clean:clean {execution: default-clean}]
[INFO] [antrun:run {execution: id.clean}]
[INFO] Executing tasks
     [echo] clean phase
[INFO] Executed tasks
[INFO] [antrun:run {execution: id.post-clean}]
[INFO] Executing tasks
     [echo] post-clean phase
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------
[INFO] Total time: < 1 second
[INFO] Finished at: Sat Jul 07 13:38:59 IST 2012
[INFO] Final Memory: 4M/44M
[INFO] ------------------------------------------------------------------

You can try modifying the mvn clean command to display pre-clean and clean, and perform no actions in the post-clean phase.


Default (Build) Lifecycle

This is Maven's primary lifecycle, used for building applications, and includes the following 23 phases:

Lifecycle Phase Description
validate Validates that the project is correct and all necessary information is available to complete the build process.
initialize Initializes the build state, such as setting property values.
generate-sources Generates any source code included in the compilation phase.
process-sources Processes the source code, for example, by filtering any values.
generate-resources Generates resource files that will be included in the project package.
process-resources Copies and processes resources to the target directory, preparing for the packaging phase.
compile Compiles the project's source code.
process-classes Processes the compiled files, such as optimizing Java class files.
generate-test-sources Generates any test source code included in the compilation phase.
process-test-sources Processes the test source code, for example, by filtering any values.
generate-test-resources Creates resource files for testing.
process-test-resources Copies and processes test resources to the target directory.
test-compile Compiles test source code to the test target directory.
process-test-classes Processes the compiled test source files.
test Runs tests using an appropriate unit testing framework (JUnit being one of them).
prepare-package Performs any necessary operations before the actual packaging.
package Packs the compiled code into a distributable format, such as JAR, WAR, or EAR files.
pre-integration-test Performs necessary actions before integration tests, such as setting up the environment.
integration-test Processes and deploys the project to an environment where integration tests can be run.
post-integration-test Performs necessary actions after integration tests, such as cleaning up the environment.
verify Runs any checks to verify the package is valid and meets quality criteria.
install Installs the project package into the local repository, making it available for other local projects.
deploy Copies the final project package to a remote repository for sharing with other developers and projects.

Some important concepts related to the Maven lifecycle are:

When a phase is called via a Maven command, such as mvn compile, all phases up to and including that phase are executed.

Different Maven goals will be bound to different Maven lifecycle phases depending on the packaging type (JAR/WAR/EAR).

In the example below, we add the maven-antrun-plugin:run goal to some phases of the Build lifecycle. This allows us to display lifecycle text information.

We have updated the pom.xml file in the C:\MVN\project directory.

&lt;project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.companyname.projectgroup</groupId>
<artifactId>project</artifactId>
<version>1.0</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
```xml
<artifactId>maven-antrun-plugin</artifactId>
<version>1.1</version>
<executions>
   <execution>
      <id>id.validate</id>
      <phase>validate</phase>
      <goals>
         <goal>run</goal>
      </goals>
      <configuration>
         <tasks>
            <echo>validate phase</echo>
         </tasks>
      </configuration>
   </execution>
   <execution>
      <id>id.compile</id>
      <phase>compile</phase>
      <goals>
         <goal>run</goal>
      </goals>
      <configuration>
         <tasks>
            <echo>compile phase</echo>
         </tasks>
      </configuration>
   </execution>
   <execution>
      <id>id.test</id>
      <phase>test</phase>
      <goals>
         <goal>run</goal>
      </goals>
      <configuration>
         <tasks>
            <echo>test phase</echo>
         </tasks>
      </configuration>
   </execution>
   <execution>
         <id>id.package</id>
         <phase>package</phase>
         <goals>
            <goal>run</goal>
         </goals>
         <configuration>
         <tasks>
            <echo>package phase</echo>
         </tasks>
      </configuration>
   </execution>
   <execution>
      <id>id.deploy</id>
      <phase>deploy</phase>
      <goals>
         <goal>run</goal>
      </goals>
      <configuration>
      <tasks>
         <echo>deploy phase</echo>
      </tasks>
      </configuration>
   </execution>
</executions>
</plugin>
</plugins>
</build>
</project>

Now open the command console, navigate to the directory where the pom.xml is located, and execute the following mvn command.

C:\MVN\project>mvn compile

Maven will start processing and display the phases of the build lifecycle up to the compile phase.

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------
[INFO] Building Unnamed - com.companyname.projectgroup:project:jar:1.0
[INFO]    task-segment: [compile]
[INFO] ------------------------------------------------------------------
[INFO] [antrun:run {execution: id.validate}]
[INFO] Executing tasks
     [echo] validate phase
[INFO] Executed tasks
[INFO] [resources:resources {execution: default-resources}]

[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Skip non-existing resourceDirectory C:\MVN\project\src\main\resources [INFO] [compiler:compile {execution: default-compile}] [INFO] Nothing to compile - all classes are up to date [INFO] [antrun:run {execution: id.compile}] [INFO] Executing tasks [echo] compile phase [INFO] Executed tasks [INFO] ------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------ [INFO] Total time: 2 seconds [INFO] Finished at: Sat Jul 07 20:18:25 IST 2012 [INFO] Final Memory: 7M/64M [INFO] ------------------------------------------------------------------

Command Line Invocation

In the development environment, use the following command to build and install the project into the local repository.

mvn install

This command executes the default lifecycle phases (validate, compile, package, etc.) before the install phase. We only need to invoke the last phase, which is install in this case.

In the build environment, use the following invocation to cleanly build and deploy the project to the shared repository.

mvn clean deploy

This command can also be used in a multi-module scenario, where the project contains multiple sub-projects. Maven will execute the clean command on each sub-project and then execute the deploy command.


Site Lifecycle

The Maven Site plugin is generally used to create new report documents, deploy sites, etc.

The site and site-deploy phases are often used to generate and publish the Maven site, which is a powerful feature of Maven. Managers appreciate it, as documentation and statistics are generated automatically, making it visually appealing.

In the following example, we will add the maven-antrun-plugin:run goal to all phases of the Site lifecycle. This allows us to display text information for all lifecycle phases.

We have updated the pom.xml file in the C:\MVN\project directory.

&lt;project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.companyname.projectgroup</groupId>
<artifactId>project</artifactId>
<version>1.0</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.1</version>
   <executions>
      <execution>
         <id>id.pre-site</id>
         <phase>pre-site</phase>
         <goals>
```xml
<goal>run</goal>
</goals>
<configuration>
   <tasks>
      <echo>pre-site phase</echo>
   </tasks>
</configuration>
</execution>
<execution>
   <id>id.site</id>
   <phase>site</phase>
   <goals>
   <goal>run</goal>
   </goals>
   <configuration>
      <tasks>
         <echo>site phase</echo>
      </tasks>
   </configuration>
</execution>
<execution>
   <id>id.post-site</id>
   <phase>post-site</phase>
   <goals>
      <goal>run</goal>
   </goals>
   <configuration>
      <tasks>
         <echo>post-site phase</echo>
      </tasks>
   </configuration>
</execution>
<execution>
   <id>id.site-deploy</id>
   <phase>site-deploy</phase>
   <goals>
      <goal>run</goal>
   </goals>
   <configuration>
      <tasks>
         <echo>site-deploy phase</echo>
      </tasks>
   </configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

Now open the command console, navigate to the directory containing the pom.xml, and execute the following mvn command.

C:\MVN\project>mvn site

Maven will start processing and display the phases of the site lifecycle up to the site phase.

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------
[INFO] Building Unnamed - com.companyname.projectgroup:project:jar:1.0
[INFO]    task-segment: [site]
[INFO] ------------------------------------------------------------------
[INFO] [antrun:run {execution: id.pre-site}]
[INFO] Executing tasks
     [echo] pre-site phase
[INFO] Executed tasks
[INFO] [site:site {execution: default-site}]
[INFO] Generating "About" report.
[INFO] Generating "Issue Tracking" report.
[INFO] Generating "Project Team" report.
[INFO] Generating "Dependencies" report.
[INFO] Generating "Project Plugins" report.
[INFO] Generating "Continuous Integration" report.
[INFO] Generating "Source Repository" report.
[INFO] Generating "Project License" report.

[INFO] Generating "Mailing Lists" report. [INFO] Generating "Plugin Management" report. [INFO] Generating "Project Summary" report. [INFO] [antrun:run {execution: id.site}] [INFO] Executing tasks [echo] site phase [INFO] Executed tasks [INFO] ------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------ [INFO] Total time: 3 seconds [INFO] Finished at: Sat Jul 07 15:25:10 IST 2012 [INFO] Final Memory: 24M/149M [INFO] ------------------------------------------------------------------

❮ Maven Tutorial Maven Deployment Automation ❯