Maven Life Cycle (clean+site+default)

Maven has a set of pre-defined build lifecycles to perform specific tasks. The most common lifecycles are the default, clean, and site lifecycles. This tutorial will provide an overview of each lifecycle and demonstrate their usage in a simple project.

  • Default Lifecycle:

The default lifecycle is responsible for building and deploying a project. It consists of several phases, such as compile, test, package, install, and deploy. When you invoke a specific phase, Maven executes all the preceding phases in the lifecycle.

Here's a brief overview of some important phases:

  • validate: Validates if the project structure is correct and all necessary information is available.
  • compile: Compiles the project's source code.
  • test: Executes unit tests without failing the build.
  • package: Packages the compiled code in its distributable format (e.g., JAR, WAR).
  • verify: Runs checks to verify that the package is valid and meets quality criteria.
  • install: Installs the package to the local repository for use as a dependency in other projects.
  • deploy: Copies the final package to a remote repository for sharing with other developers.
  • Clean Lifecycle:

The clean lifecycle is responsible for cleaning the project's working directory by removing the build artifacts (e.g., compiled classes, generated JARs).

It consists of the following phases:

  • pre-clean: Executes processes needed before the actual clean.
  • clean: Cleans the project, typically by deleting the target directory.
  • post-clean: Executes processes needed after the clean.
  • Site Lifecycle:

The site lifecycle is responsible for creating the project's site documentation, including reports and static files.

Key phases include:

  • pre-site: Executes processes needed before site generation.
  • site: Generates the project site documentation.
  • post-site: Executes processes needed after site generation.
  • site-deploy: Deploys the generated site to a specific location (e.g., a web server).

Example: Using Maven Lifecycles in a Simple Project

Create a new Maven project (assuming Maven is installed and configured) with the following command:

mvn archetype:generate -DgroupId=com.example -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

This command generates a basic Java project with a "pom.xml" file and sample Java class with a test case.

Navigate to the "my-app" directory and run the following Maven commands to understand the lifecycle usage:

  • Execute the "clean" lifecycle to remove the build artifacts:
mvn clean

This command deletes the "target" directory, cleaning up the previous build.

  • Execute the "default" lifecycle to compile, test, and package the project:
mvn package

This command compiles the source code, runs tests, and creates a JAR file in the "target" directory.

  • Generate the project site documentation using the "site" lifecycle:
mvn site

This command generates a site with reports and documentation in the "target/site" directory.

To summarize, Maven's clean, default, and site lifecycles provide a structured way to build, clean, and document projects. Each lifecycle consists of phases that can be executed individually or together to automate and standardize project tasks.