Introduction To Apache Maven | Automation Tool For Projects

Suman Kumari
BYJU’S Exam Prep Engineering
5 min readOct 1, 2019

Apache Maven is a build Automation tool primarily used for java projects. It is a powerful project management tool based on POM(Project Object Model). Development team can automate the project’s build infrastructure in almost no time as Maven uses a standard directory layout and a default build lifecycle. It comes out with the aspect that how an automation software is build and list down all the dependencies. Maven can manage a project’s build, reporting and documentation from a central piece of information. Maven can be used as automation tool in language like Java, C#, Scala, Ruby.

Why Maven?

  • Maven makes project build process too easy.
  • It provides project document and better project management practice.
  • Managing project dependencies.
  • It allows automation project build in POM(Project Object Model) model.
  • It maintains standard directory structure for projects.
  • It has a central repository for download and resolve all the jar dependencies.
  • It avoids package dependent jars for project compilation and run.
  1. Maven POM(Project Object Model) Project Directory Structure:

2. Maven POM(Project Object Model) Configuration:

Maven POM stores all the configurations for a single project. General configurations consists of :

  • A file name POM with XML extension as pom.xml
  • Individual phases of build process known as plugins
  • Stores all the dependencies of project
  • Contains source directory

Maven POM configuration in pom.xml file is:

Elements of Dependencies:

  1. dependencies: It is an elemental xml tag used to wrap all the dependency of the project. It is like a wrapper for all the dependency.
  2. dependency: It is an individual dependency used in the project listed inside dependencies tag and is defined by groupId, artifactId, versions, scope.
  3. groupId: It will identify your project uniquely across all projects, so we need to enforce a naming schema. It has to follow the package name rules, and you can create as many subgroups as you want. e.g org.apache.maven
  4. artifactId: It is the name of the jar without version. e.g testng, log4j, extentreports.
  5. version: It contains the version number of the project. e.g 3.1.1, 2.1.
  6. scope: It indicates that dependency is not required for compilation, but is required during execution.

3. Build Lifecycle Of Maven Projects:

The default lifecycle of a Maven project comprises of these phases:

  • validate: validate the project is correct and all necessary information is available.
  • compile: compile the source code of the project.
  • test: run tests using a suitable unit testing framework. It is independent of code packaging and deployment.
  • package: take the compiled code and package it in its distributable format, such as a JAR
  • verify: 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: copies the final package to remote repository or to release environment.

4. Configuration Of Maven Project:

  • Execute the command mvn -version to check successful installation of Maven from your terminal. It will give output like-
  • Open the project directory from terminal as:
  • Run the command mvn clean (mvn clean attempts to clean the files and directories generated by Maven during its build).
  • Run the command mvn package (mvn package takes the compiled code and package it in its distributable format, such as a JAR or WAR file).

5. Getting Started With Maven Project:

  • Open a new project with project type as Maven
  • Give groupId and artifactId (Project Name) as per your wish.
  • On finish the project architecture will be like:
  • Add the required dependencies in pom.xml file as:
  • Write the desired code and to run the desired test include that test file in a test.xml file as:

include your test file inside class tag. If you have to multiple test files for parallel executions include every file inside class tag which will be wrapped by classes tag.

  • The running maven test will have format like:
  • Running of test xml file from terminal as :

mvn clean test- command

Dsurefire.suiteXmlFiles- plugin included in pom.xml file

Quizzes.xml- test file to be executed.

  • Running parametrised test xml file from terminal as:

mvn clean test- command

Dsurefire.suiteXmlFiles- plugin included in pom.xml file

LoginWithEmailPassword.xml- test file to be executed

Dbrowser= browser you want to choose to run your test.

Demail, Dpassword = parameters you use for your testcases.

test file where these parameters are declared is :

6. Some Fall Back Of Maven:

  • Library repositories are not always safe.
    In maven, you can define one or several repositories of library. However, sometimes if the management of library are bad, you can have compile error or differences between jar on different repositories.

That’s it, folks!

--

--