Welcome to linuxnix.com. In our last blogs, we went through the basics of Maven (click here), the functionalities, characteristics, and usage. We even went deep into POM and Maven directory structure (click here). In this blog, we will go through the installation, configuration and the life cycle of build through Maven.

 

Maven Build Lifecycle Phrases

The sequence of steps which is defined in order to execute the tasks and goals of any maven project is known as lifecycle in Maven. The following list shows the most important Maven lifecycle phases:

  • validate – checks the correctness of the project
  • compile – Compiles the provided source code into binary artifacts
  • test – executes unit tests
  • package – packages compiled code into an archive file
  • integration-test – executes additional tests, which require the packaging
  • verify – checks if the package is valid
  • install – installs the package file into the local Maven repository
  • deploy – deploys the package file to a remote server or repository

 

Maven Plugins

Maven is actually a plugin execution framework where each and every task is executed with the help of plugins. Maven plugins are used to do the below tasks.

  • create jar file
  • create war file
  • compile code files
  • unit testing of code
  • create project documentation
  • create project reports

The execution of the plugins has the following syntax.

mvn [plugin-name]:[goal-name]

 

Maven Plugin Types

Maven has basically two types of plugins.

  1. Build Plugins: They execute during the build process and are configured in the <build/> element of pom.xml.
  2. Reporting plugins: They execute during the site generation process and they should be configured in the <reporting/> element of the pom.xml.

Below is the list of some common plugin:

  • Clean: Cleans up target after the build. Deletes the target directory.
  • Compiler: Compiles Java source files.
  • Surefire: Runs the JUnit unit tests. Creates test reports.
  • Jar: Builds a JAR file from the current project.
  • War: Builds a WAR file from the current project.
  • Javadoc: Generates Javadoc for the project.
  • Antrun: Runs a set of ant tasks from any phase mentioned of the build.

 

Installing and Configuring Maven

Here we have used Ubuntu 16.04 LTS version of Operation System where we are going to install and configure Maven.

Step 1

Installing Java: Since Maven is written in Java. You need to install Java first.

sudo add-apt-repository ppa:webupd8team/java

sudo apt-get update

sudo apt-get install oracle-java8-installer

Check Java installation by checking its version.

java -version

 

Step 2

Setting up Java Home path.

(JAVA_HOME=”/usr/lib/jvm/java-8-oracle”)

sudo nano /etc/environment
source /etc/environment

To check Java Home, execute the below command.

echo $JAVA_HOME

 

Step 3

Installing Maven: Now it’s the time to install Maven.

sudo apt-get install maven

Check Maven installation by the following command.

mvn –version

 

Step 4

Setting up of Maven Path

M2_HOME=”/usr/share/maven”

PATH=”${M2_HOME}/bin:${PATH}”

sudo vi /etc/environment
source /etc/environment

To check Maven Home.

echo $M2_HOME

 

Building a Maven Project

This is a simple demonstration application used in the Jenkins: The Definitive Guide book. In this part, we are taking a Maven project hosted in a version control (https://github.com/iankesh/game-of-life). We are going to build it and check the build artifact.

Step 1

Clone a maven project from a version control.

git clone https://github.com/iankesh/game-of-life

 

Step 2

We are now going to build it using maven commands. The application is a very simple online version of Conway’s ‘game of life’. The project is a simple multi-module Maven project. To build the whole project, just run mvn install from the root directory.

mvn install

Logs:

[INFO] Installing /root/game-of-life/gameoflife-web/target/gameoflife.war to /root/.m2/repository/com/wakaleo/gameoflife/gameoflife-web/1.0-SNAPSHOT/gameoflife-web-1.0-SNAPSHOT.war

[INFO] Installing /root/game-of-life/gameoflife-web/pom.xml to /root/.m2/repository/com/wakaleo/gameoflife/gameoflife-web/1.0-SNAPSHOT/gameoflife-web-1.0-SNAPSHOT.pom

 

Step 3

This install will create a directory named target inside gameoflife-web where it saves the build artifacts. We can check the build artifact gameoflife.war in the directory target. If you want to deploy the application, we can take this war file and deploy to any of the web servlets.

 

Step 4

If we want to clean the build, we need to run mvn clean. This will delete the target directory inside gameoflife-web.

mvn clean

If we want to delete the old build and also install it again, we need to do both with the same command.

mvn clean install

 

Conclusion

Maven is a very powerful build tool specially created to build Java projects. In this blog, we went through the installation, configuration of Maven and the life cycle of build through Maven. We have even gone through Maven basics which can be found here. The characteristics and functionalities of Maven can be found here.

Stay tuned with us at linuxnix.com.

 

 

The following two tabs change content below.
Hello, my name is Ankesh and I work as a Sr. DevOps Engineer in an IT firm where I have hands-on experience in supporting and automating deployments in the cloud, leveraging configuration management and DevOps process. I like to keep myself always in the learning path and love to share knowledge with others. One of my other interests is Graphics Designing and my hobbies are Cooking and Playing Cricket.