Maven Build Tool – Guide

Maven is a build automation and a project management tool, mainly used for Java projects. Maven project structure and contents are declared in an xml file called pom.xml, referred as Project Object Model (POM), pom is the heart of the maven build system

Learn Maven in 5 minutes !

Step 1 :
Download maven zip extract from here

Step 2 :
Unzip the dowloaded file and set the Environment variables
In Windows:
M2_HOME : C:\Program Files\Apache Software Foundation\apache-maven-3.3.1
M2=%M2_HOME%\bin
MAVEN_OPTS=-Xms256m -Xmx512m

Step 3 :
Check if maven configuration is done successfully :
C:\>mvn –version
It should display information like below, if it does show something like below, it means the maven installation/configuration is successful.
Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2019-01-24T10:31:37+02:00)
Maven home: C:\Program Files\Apache Software Foundation\apache-maven-3.6.0\bin\..
Java version: 1.8.0_161, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk1.8.0_161\jre
OS name: "windows 7", version: "6.1", family: "windows"

Create a Project

Create a directory in your machine anywhere, eg: mkdir test

Goto test : C:\>cd test

Run the command :
mvn archetype:generate -DgroupId=com.programmertoday.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false

This archetype : generate goal created a simple project based upon a maven-archetype-quickstart archetype and will create a directory with the name given in -DartifactId

When you go inside this directory: C:\test>cd my-app
you will see a pom.xml file and a src directory

The src dir will have sub folders : src/main/java/com/programmertoday/app/App.java & src/test/java/com/programmertoday/app/AppTest.java

Pom.xml file will look like this :

<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.programmertoday.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1.0-SNAPSHOT</version>
 
  <properties>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>
 
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Build the Project

C:\test\my-app>mvn package
Output[INFO] ----------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ----------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Thu Jan 24 21:34:52 CEST 2019
[INFO] Final Memory: 3M/6M
[INFO] ----------------------------------

This command is a phase rather than a goal in the previous mvn archetype:generate command, So when a phase is given Maven will execute every phase in a sequence upto and including the one defined. For example if the phase given is C:\test\my-app>mvn compile, then the phases that actually get executed are:
1. validate
2. generate-sources
3. process-sources
4. generate-resources
5. process-resources
6. compile

After mvn package, the command has created the package as the jar

Now you can run the jar’s main method by the command:
java -cp target/my-app-1.0-SNAPSHOT.jar com.programmertoday.app.App

It prints the output on the console

Hello world !

Maven Phases

List of most common default lifecycle phases :

  • validate: validate the project is correct and all necessary information is available
  • compile: compile the source code of the project
  • test: test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
  • package: take the compiled code and package it in its distributable format, such as a JAR.
  • integration-test: process and deploy the package if necessary into an environment where integration tests can be run
  • verify: run any checks to verify the package is valid and meets quality criteria
  • install: install the package into the local repository, for use as a dependency in other projects locally
  • deploy: done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

There are two other Maven lifecycles of note beyond the default list above. They are

  • clean: cleans up artifacts created by prior builds
  • site: generates site documentation for this project

Alternate ways of using Maven :

  • Build Maven project using eclipse IDE : Download maven plugin in eclipse and use.
  • Build Maven project using Intellij IDE : You can use the external maven as configured above and use the Intellij console to run the maven goals.

Summary

In this tutorial we saw how to use maven as a build tool, how to create a maven based project, sample POM.xml, building the project and learn about maven phases.
Hope you liked it !


Leave a Comment