Apache Maven Tutorial

Posted by

The Apache Maven tutorial provides the basic and advanced concepts of Apache Maven. The Apache Maven tutorial is for the developers, beginners and professionals to understand the overall build and dependency process during project development.

Project development goes through various process like coding, build, deployment , testing before it is being developed completely to be deployed into production. The build and deployment process is a tedious and time consuming process and requires tasks such as adding library dependencies, additional project jars on the project class path, code compilation and fixing compilation issues, deploying compiled artifacts to the environment as JAR, WAR, EAR files. Apache Maven automates the above mentioned tasks and helps in reducing the overall deployment process, minimizing human errors and separates it as an activity than code development process.

Apache Maven is a powerful project management and build tool which is based on Project Object Model (POM) and is used for project build, project dependency and documentation. Project Object Model is used as pom.xml in the project which contains project and configuration information used by maven to build the project dependencies, build directory, source directory, managing project class path, managing file extensions, etc.

Benefits of using Apache Maven as the build tool

The Apache Maven is a similar tool like ANT but it provides additional features which makes it more common choice for the developers. The below are the advantages of apache maven:

  • Simplifies the Project Setup: Apache Maven provides project templates named archetypes which simplifies the project configuration process.
  • Manages Dependency Management: Automates the validation and compatibility process and manages the dependencies for the project.
  • Project Dependencies: Apache Maven retrieves the project dependencies from the dependency repositories
  • Plug-in Dependencies: Apache Maven retrieves the plug-in dependencies from the plugin repositories
  • Central Repository System: Apache Maven using the Central Repository System for public repositories

Thus , we can say that Apache Maven is a project management and build tool and helps in managing the below:

  • Build
  • Dependencies
  • Documentation
  • Releases
  • Distribution
  • Reporting
  • SCMs

Difference between Ant and Maven

The below given are the primarily difference between the ANT and Maven.

  • Project Structure details are required to be provided when using ANT as the build tool, whereas it is not required while using Maven. It uses the project templates called archetypes for project configuration.
  • Developers need to defined the ordering of steps (makes ANT a procedural approach) to be executed through ANT build whereas Maven is declarative and executes build based on configuration defined in pom.xml file.
  • ANT build doesn’t follow any life cycle whereas Maven processes the defined life cycle.
  • ANT is more as a build tool whereas Maven is a Framework which includes project management and build processes.
  • ANT scripts are project specific and cannot be resused whereas Maven is declarative and can be reused.

Apache Maven Life cycle

The Apache Maven used the Build Life cycle to clearly define the process for building and distributing the project artifacts. The below given are the build life cycles for the maven

  1. Default: manages the project deployment process
  2. Clean: manages the project cleaning process
  3. Site: manages the project documentation

Apache Maven build life cycle phases

The Maven build life cycle goes through various phases which are given below:

  • Validate: validates the project related information
  • Compile:helps in compiling the source code of the project
  • Test: validating the compiled source code using unit testing framework
  • Package:converts the compiled source code into distribution format like JAR, WAR, EAR files
  • Verify: executes the integration test to ensure quality standards are met
  • Install: helps in installing the package ( JAR /WAR/EAR file) to the local repository
  • Deploy: helps in deploying the package from local repository to the build environment.

Apache Maven plugin goals using Command line execution

The Maven plugin is the collection of goals to be executed in different phases of the maven life cycle. The Maven plugin determines the order in which these goals are to be executed. Let’s try to understand how the above mentioned build life cycle phases gets executed when using the maven commands

mvn verify – This command performs the default life cycle . within default life cycle , it executes the Validate, compile, test , package phases before executing the verify. The maven commands checks and executes the required previous phases based on the command execution.

mvn clean deploy – This command is the performs the Clean life cycle which means maven does the project cleaning and then executes the deploy process for the project.

List of available plugin from Apache maven- https://maven.apache.org/plugins/

Apache Maven installation process on windows

The Apache Maven installation is supported on Windows, Linux , Mac OS platforms. Let’s go through the maven installation on windows.

  • Download Apache from the official site : https://maven.apache.org/download.cgi
  • Minimum JDK1.7 or above is to be installed on the computer for Maven to work
  • Ensure that the environment variable (JAVA_HOME) is set for java path
  • Ensure that the environment variable (MAVEN_HOME) is set for the maven path
  • Add Maven path in the Environment variable
  • Execute maven -verison on the command prompt to verify the maven installed version