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 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.
Maven Plugin Types
Maven has basically two types of plugins.
- Build Plugins: They execute during the build process and are configured in the <build/> element of pom.xml.
- 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.
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.
Setting up Java Home path.
sudo nano /etc/environment source /etc/environment
To check Java Home, execute the below command.
Installing Maven: Now it’s the time to install Maven.
sudo apt-get install maven
Check Maven installation by the following command.
Setting up of Maven Path
sudo vi /etc/environment source /etc/environment
To check Maven 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.
Clone a maven project from a version control.
git clone https://github.com/iankesh/game-of-life
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.
[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
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.
If we want to clean the build, we need to run mvn clean. This will delete the target directory inside gameoflife-web.
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
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.
Latest posts by Ankesh K (see all)
- Deep-Dive into Jenkins – What are all Jenkins functionalities ? - January 10, 2019
- What are different Maven Plugins and how to build a Maven project? - January 7, 2019
- What is a Maven POM File and what are different Maven Repositories? - January 3, 2019
- What is Maven and what are its benefits? - December 31, 2018
- What are the different ways to install Jenkins ? - December 27, 2018