ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming»
  • Programming Languages

Maven's Build Lifecycle

Updated on December 9, 2012

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!

Comments

    0 of 8192 characters used
    Post Comment

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: "https://hubpages.com/privacy-policy#gdpr"

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized. (Privacy Policy)
    CloudflareThis is used to quickly and efficiently deliver files such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisements has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)