tezvyn:

Maven's Build Lifecycle: A Sequence of Build Phases

AI-drafted, machine-checkedSource: maven.apache.orgintermediate

Maven's build lifecycle is a predefined sequence of phases like `compile`, `test`, and `package`. Running a phase executes all preceding ones, ensuring a consistent build.

WHY IT EXISTS Maven was created to standardize the build process across Java projects. Before it, developers often wrote custom build scripts for each project, leading to inconsistency and a steep learning curve. The build lifecycle introduces a shared, universal process, meaning a developer only needs to learn a small set of commands to build any Maven project.

THE MENTAL MODEL Think of the Maven build lifecycle as a factory assembly line. Each station on the line is a "phase," such as compile, test, or package. To get your project to the package station, it must first pass through all preceding stations in order. You don't run individual tasks; you tell Maven the final phase you want to reach, and it runs the entire sequence up to that point.

HOW IT WORKS Maven has three built-in lifecycles: default, clean, and site. The default lifecycle is the most important one for building and distributing your project. It is defined by a sequence of phases. When you run a command like mvn install, Maven executes every phase in the default lifecycle up to and including install.

The key phases of the default lifecycle, in order, are: validate: Checks if the project is correct and all necessary information is available. compile: Compiles the project's source code. test: Runs unit tests against the compiled code. package: Packages the compiled code into its distributable format, like a JAR or WAR. verify: Runs checks on the package to ensure it meets quality criteria, often including integration tests. install: Installs the package into the local repository for use as a dependency in other local projects. deploy: Copies the final package to a remote repository to share with other developers.

WHEN TO USE IT Use lifecycle phases for all standard build operations. Run mvn test to compile and run unit tests. Run mvn package to create the project artifact. The source documentation recommends mvn verify as a good default command, as it runs all steps including packaging and integration tests, ensuring the artifact is valid.

WHEN NOT TO USE IT The lifecycle is not a general-purpose task runner for arbitrary scripts. While you can execute specific plugin goals directly, this is uncommon and bypasses the standardized sequence. For nearly all build-related activities, from compiling to deploying, invoking a lifecycle phase is the intended and correct method.

ONE CANONICAL EXAMPLE A command frequently used in CI/CD pipelines is mvn clean deploy. This combines two lifecycles. First, it runs the clean lifecycle, which deletes the build output directory (target). Then, it executes the default lifecycle through every phase up to deploy. This ensures a fresh build from source code that is then published to a shared repository.

Read the original → maven.apache.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.