Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Apache Maven builds the lifecycle


May 26, 2021 Maven


Table of contents


Maven - Build lifecycle

What is the build lifecycle

The build lifecycle is a sequence of stages, each of which defines the order in which the target is executed. The stages here are part of the life cycle.

For example, a typical Maven build lifecycle consists of sequences of the following phases:

Stage Processing Describe
prepare-resources A copy of the resource At this stage, you can customize the resources that need to be copied
compile Compile This phase completes the source code compilation
package Packaged This phase creates a JAR/war .xml based on the packaging configuration described in the pom-based configuration
install Installation Install the engineering package in the local/remote repository at this stage

You can use pre and post to define a target when you need to execute it before or after a particular stage.

When Maven starts building, the goals registered for each stage are executed in the order of the defined sequence of stages. Maven has three standard life cycles:

  • clean
  • default(or build)
  • site

Goals represent a specific task that is helpful for building and managing a project. I t may be bound to 0 or more build phases. Targets that are not bound to any build phase can be called directly outside the build lifecycle.

The order in which execution is performed depends on the order in which the target and build phase are called. F or example, consider the following command. The clean and package parameters are the build phase, while the dependency:copy-dependencies are a target.

mvn clean dependency:copy-dependencies package

The clean phase here will be executed first, then the dependency:copy-dependencies target will be executed, and finally the package phase will be executed.

Clean lifecycle

When we execute the mvn post-clean command, Maven calls the clean lifecycle, which contains the following stages.

  • pre-clean
  • clean
  • post-clean

Maven's clean target (clean:clean) is bound to the clean phase of the clean lifecycle. I ts clean:clean target removes the build output by deleting the build directory. So when the mvn clean command was executed, Maven deleted the build directory.

We can modify the operational behavior of this section by defining targets at any stage of the clean lifecycle above.

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

C:\MVN\project directory. pom.xml

<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>
         <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, jump to the .xml where the pom is located, and execute the mvn command below.

C:\MVN\project>mvn post-clean

Maven will begin processing and displaying 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 without doing anything during the post-clean phase.

Default (or Build) lifecycle

This is maven's primary lifecycle and is used to build applications. The following 23 stages are included.

Life cycle phase Describe
validate Check that the project is configured correctly and that all the necessary information to complete the build process is available.
initialize Initialize the build state, such as setting properties.
generate-sources Build any source files that the compilation phase needs to contain.
process-sources Work with source code, for example, filter any value.
generate-resources Build the resource files that need to be included in the project package.
process-resources Copy and process resource files into the destination directory to prepare for the packaging phase.
compile Compile the source code for the project.
process-classes Handles the strengthening and optimization of compiled files, such as Java Class bytecodes.
generate-test-sources Build any test source code that the compilation phase needs to contain.
process-test-sources Work with the test source code, for example, filtering any value.
test-compile Compile the test source code into the test purpose directory.
process-test-classes Process the file generated after the test code file is compiled.
test Run tests with the appropriate unit test framework, such as JUnit.
prepare-package Do whatever is necessary to prepare the package before you actually package it.
package Get the compiled code and package it in a publishable format, such as JAR, WAR, or EAR files.
pre-integration-test Before the integration test is performed, perform the required actions. For example, set the required environment variables.
integration-test Process and deploy the required engineering packages into an environment where integration tests can run.
post-integration-test Take the necessary action after the integration test has been performed. For example, clean up the environment.
verify Run inspection operations to verify that the package is valid and meets quality requirements.
install Install the project package into a local warehouse that can be used as a dependency on other local projects.
deploy Copy the final project package into a remote repository to share with other developers and projects.

There are some important concepts related to the Maven life cycle that need to be explained:

When a stage is called by a Maven command, such as mvn compile, only before that stage and all stages, including that stage, are executed.

Different maven targets are bound to different Maven lifecycle stages depending on the type of package (JAR/WAR/EAR).

In the following example, we add the maven-antrun-plugin:run target to part of the Build lifecycle. This allows us to display text information about the life cycle.

We've updated the pom file in the C:\MVN.xml 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.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, jump to the directory .xml where the pom is located, and execute the following mvn command.

C:\MVN\project>mvn compile

Maven will begin processing and displaying the stages of the build lifecycle up to the compilation 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] ------------------------------------------------------------------

Site lifecycle

Maven Site plug-ins are typically used to create new reporting documents, deploy sites, and so on.

Stage:

  • pre-site
  • site
  • post-site
  • site-deploy

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

We've updated the pom file in the C:\MVN.xml 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-site</id>
         <phase>pre-site</phase>
         <goals>
            <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, jump to the directory .xml where the pom is located, and execute the following mvn command.

C:\MVN\project>mvn site

Maven will begin processing and displaying the stages 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] ------------------------------------------------------------------```