- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming»
- Programming Languages
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
- pre-clean
- clean
- post-clean
Default Lifecycle
- 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
Site Lifecycle
- pre-site
- site
- post-site
- 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!