Advanced Maven Lifecycles & Bytecode Manipulation
Maven is an automation and project management tool developed by Apache, used mainly for Java projects. Maven provides a standard way of creating Java projects with its POM (Project Object Model) model.
In this article, we will discuss the Maven Build Lifecycles and Bytecode automation in the Maven build process.
What is the Maven Build Lifecycle?
The Maven Build Lifecycle is an organized sequence of phases that determines the order in which goals are executed. Maven Build Lifecycles streamlines core project activities—such as cleaning, compiling, testing, and packaging—by running them automatically in the proper sequence.
It organizes the build process into lifecycles, having different phases.
Maven Build Goal -
A goal represents a certain task that contributes to building and managing a project. A goal can be bound to one or more build phases. If a goal is not bound to any build phase, we can only execute it by passing the goal’s name to the mvn command.
Maven consists of different phases in a build lifecycle- compile, validate, test, install, etc.
There are three built-in lifecycles in Maven:
defaultcleansite
Default build Lifecycle:
This is the main lifecycle of the Maven build process, handling everything from validating your project to deploying it. It is a general model of a build process for a Java application.

The default build lifecycle consists of different phases-
validate- Checks if the project structure is correct and required information is available or not.compile- Compiles source code fromsrc/main/javainto.classfiles intarget/classes
Bytecode Manipulation -
During Maven's default build lifecycle, the compile phase generates .class files, even though Maven itself does not directly modify bytecode. Maven only manages the phases in which bytecode is generated using plugins. In this phase, the source files defined in the POM are transformed into platform-independent JVM bytecode, which can run on any platform. This bytecode serves as the basis for subsequent processing. Maven utilizes plugins that generate bytecode for various purposes, like testing, mocking, performance, and monitoring.
These plugins bundle the manipulated source code into deployable artifacts like JAR or WAR files.
.java → compile → .class (bytecode)
↓
test / verify (instrumentation)
↓
package (JAR/WAR)
test- Performs unit tests and integrationsrc/test/javausing the framework.
Bytecode Instrumentation -
In the test phase, Maven can execute plugins that instrument or intercept bytecode to support testing and runtime behavior analysis.
Common tools used at this stage include:
JaCoCo – use for code coverage analysis
Mockito – for runtime proxy and mock creation
ASM – for advanced bytecode manipulation
These tools modify or analyze bytecode to:
- Track test coverage
- Create mocks and proxies
- Inject additional runtime behavior
Maven executes these tools via plugins, without altering the source code.
verify- Runs checks to ensure that the package meets the quality criteria.
Bytecode Verification -
The verify phase ensures that the generated bytecode meets defined quality and compliance standards. This phase is commonly used for:
- Quality checks
- Static analysis
- Security scanning
During this stage, tools may:
- Analyze compiled bytecode
- Validate class structures
- Enforce architectural or security rules
This phase helps ensure that the bytecode is reliable and production-ready.
package- Packages the compiled code using a distributable format like JAR, WAR, or EAR.
Bytecode Packaging -
In the package phase, the verified bytecode is bundled together with resources and metadata into deployable artifacts such as JAR, WAR, or EAR.
install- Installs the package into your local repository as a dependency in other projects locally.deploy- Copies the final package to the remote repo in the build environment for sharing with other projects.
These default lifecycle phases are executed sequentially to complete the entire build process. Maven first validates the project, then compiles the source code into bytecode, performs tests, and packages the output into a binary JAR (Java Archive). After that, it runs integration tests on the packaged artifact, verifies the results, installs the validated package into the local repository, and finally deploys it to a remote repository.
Clean lifecycle:
The Maven clean Lifecycle ensures that a project is built from a clean state by removing reductant from the previous build. The clean lifecycle manages the project cleaning from the previous build to the new build environment.
When we run mvn clean command, Maven invokes the clean lifecycle, which consists of the following phases-
pre-cleancleanpost-clean
Pre-Clean phase -
The pre-clean phase is an initial stage in the Clean lifecycle that runs before any build artifacts are removed. In this phase, Maven executes any goals that are bound to pre-clean, allowing developers to perform preparatory or custom cleanup tasks. Unlike the clean phase, it does not delete the build directory itself. Instead of invoking a goal directly using a command like mvn pre-clean, Maven runs this phase automatically when the Clean lifecycle is started using the mvn clean command, ensuring all pre-clean operations are completed before the actual cleaning begins.
Clean phase -
The most important stage of the Clean lifecycle is the clean phase. During this phase, Maven automatically triggers the Clean plugin’s clean goal. This goal is responsible for clearing out the results of previous builds by removing the project’s build directory. By default, this directory is located at ${basedir}/target, as specified in Maven’s Super POM, unless the path has been explicitly changed. Instead of running the clean goal directly using mvn clean:clean . Maven performs this operation when the clean phase of the Clean lifecycle is executed.
Post-Clean phase -
The post-clean phase is the final stage of the Clean lifecycle and is executed after the build directory has been removed. In this phase, Maven runs any goals that are bound to post-clean which are typically used for follow-up or verification tasks once the cleanup is complete. It allows developers to perform actions such as logging, notifications, or environment resets. Rather than executing a goal directly using a command like mvn post-clean, Maven triggers this phase automatically when the Clean lifecycle is invoked with the mvn clean command, ensuring all post-cleanup tasks are completed after the clean phase finishes.
mvn deploy builds the project, runs tests, packages it, installs it locally, and then uploads the final artifact to a remote repository.
Site Lifecycle:
The Site lifecycle in Maven is responsible for generating and publishing project documentation. It creates a complete project website that includes reports, dependency details, test results, and other useful project information. This lifecycle helps teams maintain clear, well-structured, and up-to-date documentation, which can be generated locally or deployed to a remote site for easy access by developers.
Maven does not alter this lifecycle since packaging is concerned primarily with artifact creation, not with the type of site generated. Usually Maven project generates a site in a package with this command mvn site
Phases of Site lifecycle -
pre-site-This phase prepares the project environment for documentation generation, making sure all necessary resources, tools, and configurations are in place before Maven creates the site.
site-Generates the project's site documentation, including reports, test results, and dependency information. This phase executes documentation into HTML format.
post-site-Prepare site deployment to finalize the site generation, also validates the generated content.
site-deploy-Used for the deployment of the generated site to the required cloud platform.
Package Specific Lifecycles
Besides these built-in lifecycles in Maven, Maven also has package-specific lifecycles. The specific goals are bound with each package to a set of goals for a project’s packaging.
JAR (Java Archive) -
A JAR file is a package file format used to aggregate many Java class files and associated metadata and resources (text, images, etc.) into one file for distribution. JAR files are built on the ZIP file format and have a .jar file extension. They are used to distribute Java applications and libraries.
The JAR packaging lifecycle in Maven involves several phases, each associated with specific goals to ensure the proper compilation, testing, and packaging of Java applications. The default goals for the JAR package are:
| Lifecycle Phase | Plugins/Goal |
process-resources | resources: resources |
compile | compiler: compile |
process-test-resources | resources: testResources |
test-compile | compiler: testCompile |
test | surefire: test |
package | jar: jar |
install | install: install |
deploy | deploy: deploy |
The jar: jar is responsible for assembling the compiled Java classes and resources into a JAR file, which can then be installed locally or deployed to a remote repository. This process ensures that the Java application is packaged in a standardized format for easy distribution and execution.
EJB (Enterprise JavaBean) -
EJB is commonly named as Enterprise Java Beans. EJB produces modules that contain business logic components for Java Enterprise applications. Enterprise JavaBean packages are deployed to enterprise servers that support the EJB specification. EJB projects use a similar JAR lifecycle to apply metadata and structure. After that, Maven compiles the classes and packages them into a .jar file for deployment.
The default goals for the EJB package are listed in the table below:
| Lifecycle Phase | Plugins/Goal |
process-resources | resources: resources |
compile | compiler: compile |
process-test-resources | resources: testResources |
test-compile | compiler: testCompile |
test | surefire: test |
package | ejb: ejb |
install | install: install |
deploy | deploy: deploy |
This table outlines the typical phases involved in the EJB package lifecycle, focusing on the default goals associated with each phase.
WAR (Web Application Archive) -
A WAR file is used to package a web application in Java. It contains all the components of a web application, including servlets, Java Server Pages (JSPs), HTML files, and other resources like images and libraries. The WAR packaging lifecycle in Maven is similar to the JAR lifecycle but includes additional steps to handle web-specific resources.
The default goals for the WAR package are:
| Lifecycle Phase | Plugins/Goal |
process-resources | resources: resources |
compile | compiler: compile |
process-test-resources | resources: testResources |
test-compile | compiler: testCompile |
test | surefire: test |
package | war: war |
install | install: install |
deploy | deploy: deploy |
The war: war is responsible for assembling the web application into a deployable WAR file, which can then be installed locally or deployed to a server.
Maven lifecycles are predefined sequences of phases that automate the entire build process from cleaning old files to compiling, testing, packaging, and deploying the project with a single command. Understanding the Maven lifecycles and bytecode manipulation is crucial for optimizing the build process of Java projects. Maven's structured approach through its default, clean, and site lifecycles ensures an efficient build process, from compiling and testing to packaging and deployment. Maven features allow developers to maintain high standards of code quality and ensure that their Java applications are robust, efficient, and ready for production.