Open In App

Maven Lifecycle and Basic Maven Commands

Maven is a powerful project management tool that is based on POM (project object model), used for project build, dependency, and documentation. It is a tool that can be used for building and managing any Java-based project. Maven makes the day-to-day work of Java developers easier and helps with the building and running of any Java-based project.

For more details on how Maven works, how to install Maven and its applications, please visit: Introduction to Apache Maven



Maven Lifecycle: Below is a representation of the default Maven lifecycle and its 8 steps: Validate, Compile, Test, Package, Integration test, Verify, Install, and Deploy.

8 Phases of the Default Maven Lifecycle

The default Maven lifecycle consists of 8 major steps or phases for compiling, testing, building and installing a given Java project as specified below:



  1. Validate: This step validates if the project structure is correct. For example – It checks if all the dependencies have been downloaded and are available in the local repository.
  2. Compile: It compiles the source code, converts the .java files to .class, and stores the classes in the target/classes folder.
  3. Test: It runs unit tests for the project.
  4. Package: This step packages the compiled code in a distributable format like JAR or WAR.
  5. Integration test: It runs the integration tests for the project.
  6. Verify: This step runs checks to verify that the project is valid and meets the quality standards.
  7. Install: This step installs the packaged code to the local Maven repository.
  8. Deploy: It copies the packaged code to the remote repository for sharing it with other developers.

Maven follows a sequential order to execute the commands where if you run step n, all steps preceding it (Step 1 to n-1) are also executed. For example – if we run the Installation step (Step 7), it will validate, compile, package and verify the project along with running unit and integration tests (Step 1 to 6) before installing the built package to the local repository.

Maven Commands:

Generally when we run any of the above commands, we add the mvn clean step so that the target folder generated from the previous build is removed before running a newer build. This is how the command would look on integrating the clean step with install phase:

mvn clean install

Similarly, if we want to run the step in debug mode for more detailed build information and logs, we will add -X to the actual command. Hence, the install step with debug mode on will have the following command: 

mvn -X install

Consider a scenario where we do not want to run the tests while packaging or installing the Java project. In this case, we use -DskipTests along with the actual command. If we need to run the install step by skipping the tests associated with the project, the command would be:

mvn install -DskipTests
Article Tags :