Build Tools · Lesson 4
Maven Build Lifecycle and Plugins
The Maven Build Lifecycle
Maven organizes work into lifecycle phases such as compile, test, package, and install. Running a later phase runs the earlier phases first, which gives teams a predictable build flow. Plugins provide the actual behavior behind those phases.
Running a Plugin Goal
A plugin goal is a specific action, and a phase can bind one or more goals. The following Java class gives the compiler a concrete source target to process during the lifecycle.
public class BuildProfile {
public static String describe(String environmentName) {
return "Building Java Academy for " + environmentName;
}
public static void main(String[] arguments) {
System.out.println(describe("production"));
}
}
Typical commands include mvn compile, mvn test, and mvn package. Maven resolves the configured plugins and dependencies before executing each phase.
Lifecycle Comparison
| Phase | Main result | Typical check |
|---|---|---|
compile | Main classes compiled | Compiler errors |
test | Unit tests run | Assertions and reports |
package | JAR or WAR created | Artifact exists |
verify | Integration checks | Quality gates |
Common Mistake and Practices
Common Mistake: Calling a plugin goal directly and assuming the full lifecycle ran. A direct goal can skip compilation, tests, or resource processing that a normal phase would include.
- Prefer lifecycle phases in CI commands.
- Pin important plugin versions.
- Keep plugin configuration close to its purpose.
- Fail builds on test or quality errors.
- Inspect effective configuration when behavior is surprising.
Real-World Use Case
A CI job runs mvn verify for every pull request. Maven compiles the project, runs tests, packages the artifact, and applies configured verification plugins before the change can merge.
Quiz Check