Maven Build Lifecycle In Java

Maven Build Lifecycle in java

In the Java programming language, the Maven build lifecycle indicates a continuous sequence of important guide steps followed by the Maven build lifecycle to create a Java software project, test existing software source code, group build the program source package, and deploy it to a dedicated system or hardware device.

Maven Build Lifecycle In Java

Java Maven build has three built-in important lifecycle methods.

  • default lifecycle — It creates and deploys a software project in a Java program.
  • clean lifecycle — It cleans the build files and logs generated in a software project in a Java program.
  • site lifecycle — It generates a software project detail guide documentation in a Java program.

Default Lifecycle Method in Java.

The default lifecycle is an imported method in the most commonly used lifecycle in Java program source code. The Default Lifecycle concept helps Java users create and manage source code.

The important steps in the Default Lifecycle are.

Validate Default Lifecycle Method

                      ↓

Compile Java source code

                     ↓

Test source code

                     ↓

Build source code package

                     ↓

Verify source code

                     ↓

Install and deploy build package

                     ↓

Final deployment

Important maven builds lifecycle phases.

#Build PhasePurpose of Maven Build Lifecycle Phases
1.Validate phaseMaven builds lifecycle validate phase used to checks, that the project is correct and required all information is available in existing project.
2.Compile phaseCompile phase used to compile the main java program source code from machine to human readable program code.
3.Test phaseTest phase used to compile and runs java source code unit tests.
4.Package phasePackage phase used to build or creates a jar/war/ environment etc.
5.Verify phaseVerify phase used to runs checks to verify the package is valid or not.
6.Install phaseInstall phase used to installs the artifact into the local maven repository database storage location.
7.Deploy phaseDeploy phase used to publishes the artifact to a remote repository with database.

Maven Build Lifecycle Commands Example.

mvn compile command.

This compiles the Maven Build Lifecycle project as required by the Java user.

mvn test command.

This compiles the Maven Build Lifecycle project and runs user-defined program tests.

mvn package command.

This Maven build lifecycle runs all the previous required phase steps in the project and creates a JAR/WAR environment.

Example of mvn package.

mvn package

An mvn package basically goes through these steps.

validate method.

→ compile Java program source code phase

→ test Java program source code phase

→ package Java program source code phase

These mvn packages generally do not need to run each phase separately.

mvn clean Method in Java.

The clean lifecycle in Java program source code helps delete or remove files created by previous user-defined program source code builds. In this process, it typically cleans the target file and directory data.

mvn clean command.

This is a very commonly used command method for cleaning Java program source code.

mvn clean package command.

It refers to the user-defined program source code cleaning step method in Java programs.

Clear the previous build source code data

                     ↓

Compile the existing source code

                     ↓

Run your source code tests

                    ↓

Create JAR/WAR data object

Install vs. deploy concepts in the Maven build lifecycle.

Java users often get confused between the concepts of install and deploy in Java Maven.

mvn install command method.

mvn install command.

In the Java Maven build lifecycle, mvn install helps create a project and stores the artifacts created by this project in your local Maven repository, typically located at

~/.m1/repository.

This method allows other projects on the same machine to use those artifacts as dependencies in this repository.

mvn deploy command method.

The mvn deploy command.

This builds an mvn project in the Java Maven build lifecycle and publishes its artifacts to a remote Maven repository. This mvn project is treated as if the project were configured for deployment.

Example of the mvn deploy command.

mvn install method

               ↓

Local Java repository package

mvn deploy method

                ↓

Remote Java repository

Plugins and Goals in the Maven Lifecycle.

  • Maven builds lifecycle phases are implemented in Java source code through plugin goals.
  • For example, during the Java source code compile phase, the Maven Compiler provides plugin compilation.
  • Remember, Java users can also execute plugin goals directly.

mvn compiler:compile

In the expression above.

  • Compiler → Plugin
  • Compile → Plugin Goal

Whereas instead,

The mvn compile method.

This invokes the Maven build lifecycle phase.

The main difference is.

  • Lifecycle Phase → Compile Method
  • Plugin Goal → Compiler:Compile

A typical Java project structure.

A Maven build lifecycle project in a Java program typically has an internal structure like this:

sample-project/

├── pom.xml

├── src/

│ ├── main/

│ │ └── java/

│ └── test/

│ └── java/

└── target/

Then Java.

mvn package cycle.

Then Java compiles the Maven source code, runs the necessary tests, and finally creates a packaged artifact.

target/

For example.

target/

└── sample-project-3.0.jar

Essential Maven build lifecycle concept.

When a Java user runs a Maven build lifecycle phase, the Maven lifecycle runs that phase and all previous phases in the lifecycle.

For example.

mvn test concept.

In this process, the Maven build lifecycle doesn’t just run tests. It first runs the necessary previous phases.

validate method → ​​compile source code → test build package

Similarly.

mvn install method.

This runs all the necessary source code phases in the Java Maven build lifecycle.

The Maven build lifecycle validate concept.

→ compile step

→ test step

→ package build step

→ verify step method

→ install setps

This makes the build process predictable and repeatable throughout the Maven build lifecycle.

Leave a Reply