Build lifecyle in Maven

A build lifecycle has fixed number of phases and in each phase, one or more goals(tasks) are executed. There are 3 standard life cycles in maven.
  • clean
  • site
  • build
Now let us see what are the standard phases in each of the above cycles.
  • clean lifecycle – preclean, clean, postclean
  • site lifecycle – presite, site, postsite
  • build lifecycle – [see below]
Build life cycle is the biggest life cycle in maven with below phases in it.
  • validate – Validates that project is ready to be built.
  • initialize – Initializes build process
  • generate-sources – Generate any source code
  • process-sources – Process the source code
  • generate-resources – Generate resources
  • process-resources – Process the resources
  • compile – Compile the source code
  • process-classes – process compiled classes.
  • generate-test-sources – Generate test source code
  • process-test-sources – Process the test source code
  • test-compile – Compile the test source code
  • process-test-classes Process the test source code files
  • test – Run tests (JUnit/testNG tests in project)
  • prepare-package – Prepare a package.
  • package – Package the code in formats like JAR, WAR, or EAR depending upon project type and packaging specified in POM file
  • pre-integration-test – tasks before integration tests are executed
  • integration-test – run integration tests
  • post-integration-test – tasks after integration tests are executed
  • verify – verify the package
  • install – put the package in local repository
  • deploy – store the package on remote repository for distribution purpose
Remember that order of the phases is very important. When we execute any phase, all phases up to and including that phase will execute. For example when we execute test phase in build life cycle, all of the phases up to test phase are executed in sequence (validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources , process-test-sources, test-compile, process-test-classes, test) Below maven command will execute test phase and all phases before it.
 
mvn test

Each phase can execute one or more goals. For example, when we say we are executing test phase, we are actually executing test goal. But it does not mean that we can only execute only test goal in test Phase. We can also configure maven to execute other goals in any phase.
Complex problems, Simple Solutions