Apache Maven – Create a simple maven project

What is Apache Maven?

Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information.

Key Features of Maven

  • Simple project setup that follows best practices.
  • Consistent usage across all projects.
  • A large and growing repository of libraries.
  • Dependency management including automatic updating.

Maven Project Structure

  • A Source tree for application’s sources (main) and a source tree for test sources (test).
  • The application sources reside in project home/src/main/java
  • The application resources reside in project home/src/main/resources
  • The test sources reside in project home/src/test/java
  • The test resources reside in project home/src/test/resources
  • Project home represents the directory containing target and pom.xml file.

What is POM.xml file?

POM stands for Project Object Model. The pom.xml file contains information of project and configuration information for the maven to build the project such as dependencies, build directory, source directory, test source directory, plug-in, goals etc.

While executing a task or goal, Maven looks for the pom.xml in the current directory. It reads the pom.xml, gets the needed configuration information, and then executes the goal.

Some of the configuration that can be specified in the pom.xml are following:

  • Project Dependencies
  • Project Version
  • Plugins
  • Developers
  • Goals
  • Mailing List
  • Build Profiles

Pre-requisite

  • Java should be configured
  • Eclipse should be installed

Create maven project

  • Launch Eclipse
  • In Eclipse, select Menu –> File –> New –> Other

  • In New screen, select ‘Maven Project’ and click on ‘Next’ button

  • In New Maven Project screen, select ‘Create a simple project’ and click on ‘Next’ button

  • In New Maven Project screen, enter ”Group ID’, ‘Artifact ID’, ‘Version’ and click on ‘Finish’ button

Element Description
Group ID

Will identify the project uniquely across the projects.

Ex: com.npntraining

Artifact ID Name of the project.

Ex: test-project

Version Version of the application.

Ex: 1.0 or 1.0.1

  • After clicking on ‘Finish’, the maven project was created successfully.

Maven Dependency Management

In the maven project, open pom.xml file to update the required dependent libraries.

<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.npntraining</groupId>
  <artifactId>maventest</artifactId>
  <version>1.0</version>
</project>

Under project tag, enter <dependencies> </dependencies> tag and start update the required dependent libraries inside the dependencies tag

<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.npntraining</groupId>
  <artifactId>maventest</artifactId>
  <version>1.0</version>
  <dependencies>
  <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-core -->
<dependency>
    <groupId>org.apache.spark</groupId>
    <artifactId>spark-core_2.12</artifactId>
    <version>2.4.0</version>
</dependency>
  
  </dependencies>
</project>

There is no limitation for dependent libraries inside the pom.xml, hence we can define multiple libraries as shown below

<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.npntraining</groupId>
  <artifactId>maventest</artifactId>
  <version>1.0</version>
  <dependencies>
  <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-core -->
<dependency>
    <groupId>org.apache.spark</groupId>
    <artifactId>spark-core_2.12</artifactId>
    <version>2.4.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.141.59</version>
</dependency>
  </dependencies>
</project>

After entering all the dependent libraries in dependencies tag, after saving the same, application will display ‘Building workspace’.

As, the dependent libraries were downloading in maven local repository (%USER_HOME%/.m2 directory. i.e., C:\Users\<username>\.m2 folder)

Pradeep

M.r Pradeep is QA expert working as a Research Analyst at NPN Training. He is having 12 years of experience in Quality Assurance with extensive of 4 years in Automation experience (Selenium, UFT, Rest Assured, Jmeter & Soap UI). He has helped the peers/students to understand and learn Automation with Selenium and API automation with Rest Assured with Hands-On approach.