Maven 2 lifecycle phases

In Maven 2, this notion is standardized into a set of well-known and well-defined lifecycle phases(see figure). Instead of invoking plug-ins, the Maven 2 developer invokes a lifecycle phase: $mvn compile. All the dependencies, goals, tasks, plugins which belong to this life cycle phase will be invoked otherwise not.

maven life cycle phases

maven life cycle phases

For example if Trinidad report plugin is required to be executed during test phase then it can be configured in below way

 <execution>
    <id>Trinidad-report</id>
    <phase>compile</phase>
    <goals>
        <goal>java</goal>
    </goals>
    <configuration>
        <mainClass>com.ptc.fusion.fitnesse.fixture.SetDatesAsSystemProperties</mainClass>
    </configuration>
</execution>

 

Some useful Maven 2 lifecycle phases are the following:

  • generate-sources: Generates any extra source code needed for the application, which is generally accomplished using the appropriate plug-ins
  • compile: Compiles the project source code
  • test-compile: Compiles the project unit tests
  • test: Runs the unit tests (typically using JUnit) in the src/test directory
  • package: Packages the compiled code in its distributable format (JAR, WAR, etc.)
  • integration-test: Processes and deploys the package if necessary into an environment where integration tests can be run
  • install: Installs the package into the local repository for use as a dependency in other projects on your local machine
  • deploy: Done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects

 

The lifecycle phase invokes the plug-ins it needs to do the job. Invoking a lifecycle phase automatically invokes any previous lifecycle phases as well. Since the lifecycle phases are limited in number, easy to understand, and well organized, becoming familiar with the lifecycle of a new Maven 2 project is easy.

 

All Maven Default Lifecycle Phases

  1. validate
  2. generate-sources
  3. process-sources
  4. generate-resources
  5. process-resources
  6. compile
  7. process-classes
  8. generate-test-sources
  9. process-test-sources
  10. generate-test-resources
  11. process-test-resources
  12. test-compile
  13. test
  14. prepare-package (maven 2.1+)
  15. package
  16. pre-integration-test
  17. integration-test
  18. post-integration-test
  19. verify
  20. install
  21. deploy

There are a couple key concepts to be aware of with maven lifecycles. First off, if we call a particular phase via a maven command, such as “mvn compile”, all phases up to and including that phase will be executed. So, in the case of “mvn compile”, we would actually go through the validate, generate-sources, process-sources, generate-resources, process-resources, and compile phases. The second main concept to be aware of in regards to lifecycles is that, based on the packaging of a project (jar, war, ear, etc), different maven goals will be bound to different phases of the maven lifecycle.