Skip to content
On this page

Build Process

Maven not only provides a standardized project structure but also offers a standardized build process that can automate tasks such as compilation, packaging, deployment, and more.

Lifecycle and Phase

When using Maven, it is essential to understand what a Maven lifecycle is.

A Maven lifecycle consists of a series of phases. Taking the built-in default lifecycle as an example, it includes the following phases:

validate
initialize
generate-sources
process-sources
generate-resources
process-resources
compile
process-classes
generate-test-sources
process-test-sources
generate-test-resources
process-test-resources
test-compile
process-test-classes
test
prepare-package
package
pre-integration-test
integration-test
post-integration-test
verify
install
deploy

If we run mvn package, Maven will execute the default lifecycle, running all phases from the beginning up to the package phase:

validate
initialize
...
prepare-package
package

If we run mvn compile, Maven will also execute the default lifecycle, but this time it will stop at the compile phase, executing the following phases:

validate
initialize
...
process-resources
compile

Another commonly used Maven lifecycle is clean, which executes three phases:

pre-clean
clean
post-clean

Therefore, when we use the mvn command, the parameters following it are phases. Maven automatically runs the lifecycle up to the specified phase.

A more complex example is specifying multiple phases, such as running mvn clean package. Maven will first execute the clean lifecycle up to the clean phase, then execute the default lifecycle up to the package phase. The actual phases executed are as follows:

pre-clean
clean
validate
initialize
...
prepare-package
package

In actual development, commonly used commands include:

  • mvn clean: Cleans all generated .class and .jar files.
  • mvn clean compile: Cleans first, then executes up to the compile phase.
  • mvn clean test: Cleans first, then executes up to the test phase. Since compile must be executed before test, there's no need to specify compile.
  • mvn clean package: Cleans first, then executes up to the package phase.

Most phases do not perform any actions during execution because we usually do not configure related settings in pom.xml. Therefore, these phases effectively do nothing by default.

The phases that are frequently used are actually only a few:

  • clean: Cleans the project.
  • compile: Compiles the source code.
  • test: Runs the tests.
  • package: Packages the compiled code into a .jar or other formats.

Goal

Executing a phase will trigger one or more goals:

Executed PhaseCorresponding Goal
compilecompiler:compile
testcompiler:testCompile
surefire:test
......

Goal names always follow the abc:xyz format.

At this point, you should understand the concepts of lifecycle, phase, and goal.

Analogy

Let's draw an analogy to make it clearer:

  • Lifecycle is similar to a Java package, which contains one or more phases.
  • Phase is akin to a Java class, containing one or more goals.
  • Goal is like a class method, which actually performs the work.

In most cases, we only need to specify a phase, allowing Maven to execute the goals bound to those phases by default. Only in rare cases do we need to specify a goal directly, such as starting a Tomcat server:

sh
$ mvn tomcat:run

Summary

Maven provides a standardized build process through lifecycles, phases, and goals.

The most commonly used build commands specify a phase, allowing Maven to execute up to that phase:

  • mvn clean
  • mvn clean compile
  • mvn clean test
  • mvn clean package

In general, we only execute the goals bound to the specified phases by default, so there is no need to specify goals explicitly.

Build Process has loaded