SAGARFIVE

Tutorials

4.2 Maven Build Lifecycle / Build Phase

  • When we build a Maven project, it executes a set of clearly defined tasks based on the project pom.xml configuration and the command-line options.
  • This standard set of tasks creates the maven build lifecycle.
  • The benefit of a clearly defined lifestyle is that we have to remember only a few sets of commands to compile, build, install, and deploy our projects.
  • Maven build lifecycle goes through a set of stages, they are called build phases. For example, the default lifecycle is made up of the following phases

Mostly Used Default or Buid Phases/stages :

Sl.Stage/CommandInformation
1validateThis step validates if the project structure is correct. For example – It checks if all the dependencies have been downloaded and are available in the local repository.
2compileIt compiles the source code, converts the .java files to .class, and stores the classes in the target/classes folder.
3test-compileCompile the source code in the test destination directory
4testIt runs unit tests for the project.
5packageThis step packages the compiled code in a distributable format like JAR or WAR.
6integration-testIt runs the integration tests for the project.
7verifyThis step runs checks to verify that the project is valid and meets the quality standards.
8installThis step installs the packaged code to the local Maven repository.
9deployIt copies the packaged code to the remote repository for sharing it with other developers.

Total 23 Default Phases of maven by maven.apache.org

S.NPhaseDescription
1validatevalidate the project is correct and all necessary information is available.
2initializeinitialize build state, e.g. set properties or create directories.
3generate-sourcesgenerate any source code for inclusion in compilation.
4process-sourcesprocess the source code, for example to filter any values.
5generate-resourcesgenerate resources for inclusion in the package.
6process-resourcescopy and process the resources into the destination directory, ready for packaging.
7compilecompile the source code of the project.
8process-classespost-process the generated files from compilation, for example to do bytecode enhancement on Java classes.
9generate-test-sourcesgenerate any test source code for inclusion in compilation.
10process-test-sourcesprocess the test source code, for example to filter any values.
11generate-test-resourcescreate resources for testing.
12process-test-resourcescopy and process the resources into the test destination directory.
13test-compilecompile the test source code into the test destination directory
14process-test-classespost-process the generated files from test compilation, for example to do bytecode enhancement on Java classes.
15testrun tests using a suitable unit testing framework. These tests should not require the code be packaged or deployed.
16prepare-packageperform any operations necessary to prepare a package before the actual packaging. This often results in an unpacked, processed version of the package.
17packagetake the compiled code and package it in its distributable format, such as a JAR.
18pre-integration-testperform actions required before integration tests are executed. This may involve things such as setting up the required environment.
19integration-testprocess and deploy the package if necessary into an environment where integration tests can be run.
20post-integration-testperform actions required after integration tests have been executed. This may include cleaning up the environment.
21verifyrun any checks to verify the package is valid and meets quality criteria.
22installinstall the package into the local repository, for use as a dependency in other projects locally.
23deploydone in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

Phases with Goals :

PhaseGoal
process-resourcesresources:resources
compilecompiler:compile
process-test-resourcesresources:testResources
test-compilecompiler:testCompile
testsurefire:test
packagejar:jar
installinstall:install
deploydeploy:deploy

One can invoke either a maven phase or a goal while building a project.

To invoke a phase — mvn <phase-name>

Sample pom.xml for default build life cycle :

<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/xsd/maven-4.0.0.xsd”>
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.companyname.projectgroup</groupId>
   <artifactId>project</artifactId>
   <version>1.0</version>
   <build>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.1</version>

            <executions>
               <execution>
                  <id>id.validate</id>
                  <phase>validate</phase>
                  <goals>
                     <goal>run</goal>
                  </goals>
                  <configuration>
                     <tasks>
                        <echo>validate phase</echo>
                     </tasks>
                  </configuration>
               </execution>
           
               <execution>
                  <id>id.compile</id>
                  <phase>compile</phase>
                  <goals>
                     <goal>run</goal>
                  </goals>
                  <configuration>
                     <tasks>
                        <echo>compile phase</echo>
                     </tasks>
                  </configuration>
               </execution>
           
               <execution>
                  <id>id.test</id>
                  <phase>test</phase>
                  <goals>
                     <goal>run</goal>
                  </goals>
                  <configuration>
                     <tasks>
                        <echo>test phase</echo>
                     </tasks>
                  </configuration>
               </execution>
           
               <execution>
                  <id>id.package</id>
                  <phase>package</phase>
                  <goals>
                     <goal>run</goal>
                  </goals>
                  <configuration>
                     <tasks>
                        <echo>package phase</echo>
                     </tasks>
                  </configuration>
               </execution>
           
               <execution>
                  <id>id.deploy</id>
                  <phase>deploy</phase>
                  <goals>
                     <goal>run</goal>
                  </goals>
                  <configuration>
                     <tasks>
                        <echo>deploy phase</echo>
                     </tasks>
                  </configuration>
               </execution>
            </executions>
         </plugin>
      </plugins>
   </build>
</project>

C:\Users\gajan\artifactId-MavenApp > mvn validate
C:\Users\gajan\artifactId-MavenApp > mvn compile
C:\Users\gajan\artifactId-MavenApp > mvn test
C:\Users\gajan\artifactId-MavenApp > mvn package
C:\Users\gajan\artifactId-MavenApp > mvn deploy

Output response :
C:\Users\gajan\artifactId-MavenApp>mvn compile
[INFO] Scanning for projects…
[INFO]
[INFO] —————-< com.companyname.projectgroup:project >—————-
[INFO] Building project 1.0
[INFO] ——————————–[ jar ]———————————
[INFO]
[INFO] — maven-antrun-plugin:1.1:run (id.validate) @ project —
[INFO] Executing tasks
[echo] validate phase
[INFO] Executed tasks
[INFO]
[INFO] — maven-resources-plugin:2.6:resources (default-resources) @ project —
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\gajan\artifactId-MavenApp\src\main\resources
[INFO]
[INFO] — maven-compiler-plugin:3.1:compile (default-compile) @ project —
[INFO] No sources to compile
[INFO]
[INFO] — maven-antrun-plugin:1.1:run (id.compile) @ project —
[INFO] Executing tasks
[echo] compile phase
[INFO] Executed tasks
[INFO] ————————————————————————
[INFO] BUILD SUCCESS
[INFO] ————————————————————————
[INFO] Total time: 3.033 s
[INFO] Finished at: 2021-12-10T20:05:46+05:30
[INFO] ————————————————————————
C:\Users\gajan\artifactId-MavenApp >