Maven's Build Lifecycle

Build lifecycle: the Heart of Maven

If you have already used Maven to manage your projects, you most likely know that its most important part is the lifecycle. Following the documentation we know that Maven covers the main build phases of a project separated in 3 lifecycles: default, clean and site.

In this article I will show you how different phases (the complete list is below) in Maven (3.0) are "called" when specific goals are executed. To do this, we will create a simple project and change the pom.xml using the maven-antrun-plugin to display a message with usual ant-echo when a phase is executed. You can use this pom.xml as a probe to test which phases are involved when you launch specific maven goals from the command line. Example: mvn war:war; mvn clean test; etc.

Note: not all the phases are available for all the versions of Maven.

Clean Lifecycle

  1. pre-clean
  2. clean
  3. post-clean

Default Lifecycle

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

Site Lifecycle

  1. pre-site
  2. site
  3. post-site
  4. site-deploy

(to read the description for every single phase, please read the documentation)

First of all we need to create a sample application. From the command line run:

$> mvn archetype:generate -DgroupId=org.sample.app -DartifactId=first-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false


If everything is ok you should get on your system a directory "first-app" that represents your project, and inside it a file pom.xml.

Now we need to use the antrun plugin to display messages (ant-echo) when a specific phase is called. This is an example for the test phase:

<build>
   <plugins>
      <plugin>
         <artifactId>maven-antrun-plugin</artifactId>
         <executions>
            ...
            <execution>
               <phase>test</phase>
               <goals>
                  <goal>run</goal>
               </goals>
               <configuration>
                  <tasks>
                     <echo message="test phase"/>
                  </tasks>
               </configuration>
            </execution>
            ...
         </executions>
      </plugin>
   </plugins>
</build>

Finally the complete pom.xml (note I have included all the three lifecycles default, clean, site):

<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.sample.app</groupId>
  <artifactId>first-app</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>first-app</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>

        <!-- default lifecycle -->
        <executions>
          <execution>
            <id>validate</id>
            <phase>validate</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>validate phase</echo>
              </tasks>
            </configuration>
          </execution>
          <execution>
            <id>initialize</id>
            <phase>initialize</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>initialize phase</echo>
              </tasks>
            </configuration>
          </execution>
          <execution>
            <id>generate-sources</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>generate-sources phase</echo>
              </tasks>
            </configuration>
          </execution>
          <execution>
            <id>process-sources</id>
            <phase>process-sources</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>process-sources phase</echo>
              </tasks>
            </configuration>
          </execution>
          <execution>
            <id>generate-resources</id>
            <phase>generate-resources</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>generate-resources phase</echo>
              </tasks>
            </configuration>
          </execution>
          <execution>
            <id>process-resources</id>
            <phase>process-resources</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>process-resources phase</echo>
              </tasks>
            </configuration>
          </execution>
          <execution>
            <id>compile</id>
            <phase>compile</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>compile phase</echo>
              </tasks>
            </configuration>
          </execution>
          <execution>
            <id>process-classes</id>
            <phase>process-classes</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>process-classes phase</echo>
              </tasks>
            </configuration>
          </execution>
          <execution>
            <id>generate-test-sources</id>
            <phase>generate-test-sources</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>generate-test-sources phase</echo>
              </tasks>
            </configuration>
          </execution>
          <execution>
            <id>process-test-sources</id>
            <phase>process-test-sources</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>process-test-sources phase</echo>
              </tasks>
            </configuration>
          </execution>
          <execution>
            <id>generate-test-resources</id>
            <phase>generate-test-resources</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>generate-test-resources phase</echo>
              </tasks>
            </configuration>
          </execution>
          <execution>
            <id>process-test-resources</id>
            <phase>process-test-resources</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>process-test-resources phase</echo>
              </tasks>
            </configuration>
          </execution>
          <execution>
            <id>test-compile</id>
            <phase>test-compile</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>test-compile phase</echo>
              </tasks>
            </configuration>
          </execution>
          <execution>
            <id>process-test-classes</id>
            <phase>process-test-classes</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>process-test-classes phase</echo>
              </tasks>
            </configuration>
          </execution>
          <execution>
            <id>test</id>
            <phase>test</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>test phase</echo>
              </tasks>
            </configuration>
          </execution>
          <execution>
            <id>prepare-package</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>prepare-package phase</echo>
              </tasks>
            </configuration>
          </execution>
          <execution>
            <id>package</id>
            <phase>package</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>package phase</echo>
              </tasks>
            </configuration>
          </execution>
          <execution>
            <id>pre-integration-test</id>
            <phase>pre-integration-test</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>pre-integration-test phase</echo>
              </tasks>
            </configuration>
          </execution>
          <execution>
            <id>integration-test</id>
            <phase>integration-test</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>integration-test phase</echo>
              </tasks>
            </configuration>
          </execution>
          <execution>
            <id>post-integration-test</id>
            <phase>post-integration-test</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>post-integration-test phase</echo>
              </tasks>
            </configuration>
          </execution>
          <execution>
            <id>verify</id>
            <phase>verify</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>verify phase</echo>
              </tasks>
            </configuration>
          </execution>
          <execution>
            <id>install</id>
            <phase>install</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>install phase</echo>
              </tasks>
            </configuration>
          </execution>
          <execution>
            <id>deploy</id>
            <phase>deploy</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>deploy phase</echo>
              </tasks>
            </configuration>
          </execution>

          <!-- clean lifecycle -->
          <execution>
            <id>pre-clean</id>
            <phase>pre-clean</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>pre-clean phase</echo>
              </tasks>
            </configuration>
          </execution>
          <execution>
            <id>clean</id>
            <phase>clean</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>clean phase</echo>
              </tasks>
            </configuration>
          </execution>
          <execution>
            <id>post-clean</id>
            <phase>post-clean</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>post-clean phase</echo>
              </tasks>
            </configuration>
          </execution>

          <!-- site lifecycle -->
          <execution>
            <id>pre-site</id>
            <phase>pre-site</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>pre-site phase</echo>
              </tasks>
            </configuration>
          </execution>
          <execution>
            <id>site</id>
            <phase>site</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>site phase</echo>
              </tasks>
            </configuration>
          </execution>
          <execution>
            <id>post-site</id>
            <phase>post-site</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>post-site phase</echo>
              </tasks>
            </configuration>
          </execution>
          <execution>
            <id>site-deploy</id>
            <phase>site-deploy</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>site-deploy phase</echo>
              </tasks>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

Always inside the directory "first-app" after launched the command "mvn test" you should now be able to see (I cut out some parts to highlight the "echo" messages):

~/first-app$ mvn test
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building first-app 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-antrun-plugin:1.3:run (validate) @ first-app ---
[INFO] Executing tasks
     [echo] validate phase
...
     [echo] initialize phase
...
     [echo] generate-sources phase
...
     [echo] process-sources phase
...
     [echo] generate-resources phase
...
     [echo] process-resources phase
...
     [echo] compile phase
...
     [echo] process-classes phase
...
     [echo] generate-test-sources phase
...
     [echo] process-test-sources phase
...
     [echo] generate-test-resources phase
...
     [echo] process-test-resources phase
...
     [echo] test-compile phase
...
     [echo] process-test-classes phase
[INFO] Executed tasks
[INFO] 
[INFO] --- maven-surefire-plugin:2.5:test (default-test) @ first-app ---
[INFO] Surefire report directory: /home/gnele/first-app/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running org.sample.app.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.049 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] 
[INFO] --- maven-antrun-plugin:1.3:run (test) @ first-app ---
[INFO] Executing tasks
     [echo] test phase
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.228s
[INFO] Finished at: Sat Oct 22 16:16:49 BST 2011
[INFO] Final Memory: 9M/245M
[INFO] ------------------------------------------------------------------------

Cool isn't it? Now you can use it to check the phases executed when a Maven's command run and customize the execution of any single phase of your projects.

Have fun!

More by this Author

  • How To Parse Html In Java - Part 2
    0

    In the first article we saw how to parse html in Java using the libraries: Jsoup HtmlCleaner Internal Swing SDK parser This time we will see other useful libraries to parse html like: TagSoup HTML Parser...

  • How To Parse Html In Java
    7

    Examples and information about how to parse html in Java using: Jsoup, HtmlCleaner, Internal Swing SDK parser, TagSoup, HTML Parser, JTidy, NekoHtml, Cobra, HotSAX and Jerico HTML Parser.

  • How to Create a Chat Room in Java
    0

    A quick and easy way to build your free and personal chat room in Java.


Comments 2 comments

Jerome Verstrynge 4 years ago

I have created an introduction to Maven concepts: http://tshikatshikaaa.blogspot.com/2012/05/introdu...


Nani 4 years ago

very good exaplanation

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working