What Are POM (Project Object Model) And pom.xml In Maven

This Tutorial Explains What are POM (Project Object Model) and pom.xml in Maven along with pom.xml Example. We will also see how to set up Maven Environment:

We will explore how to set up a Maven environment, along with installation & project set up in Maven, and the details on a Project Object Model (POM).

=> Take A Look At The Maven Beginners Guide Here.

Project Set up & POM.xml

Maven Environment And Project Set-Up

Maven Environment set up is already discussed in detail on the following page.

=> Environment set up for Maven

Maven Steps To Build A Project

A project can be set up in Maven using any IDE like Eclipse and also from the command prompt.

How to build a project in Eclipse IDE has been discussed in detail on the below page.

=> Maven Project Setup

Here, we shall see how to build a Maven project from the command prompt.

#1) In order to create a project, the first command to be used is given below.

mvn archetype: generate

archetype: generate is used for creating a new project from the archetype.

#2) After this we need to provide groupId, artifactId, template to be used in the project followed by the interactive mode of the project.

The command to be used is:

mvn archetype:generate -DgroupId=testing -DartifactId=Test -DarchetypeArtifactId=
maven-archetype-quickstart -DinteractiveMode=false

Please note, -D is used to pass parameter. DarchetypeArtifactId is the parameter that is used to specify the template of the project that is to be maintained. For example, here quickstart is generally used to generate testing projects commonly.

Likewise, there are many types of templates available to define projects in Maven. Lastly, we have the interactiveMode where two values can be set as false and true.

Here, groupId testing is the name of the project, artifactId Test is the name of the subproject.

Maven Steps to build a Project

The build is progressed and if it is a success, then a Maven project will be created with information on the time taken to complete the build, timestamp of the build completion, and memory allocation.

#3) We have developed this Maven project from the command prompt outside Eclipse. In order to import the project in Eclipse, we need to first integrate Maven with Eclipse. First, we need to point to the location of the pom.xml file that is generated inside the Maven project that we created.

Then we need to run the following command:

mvn eclipse: eclipse

Once this command is executed, two files naming .classpath and .project will be generated inside the Maven project that we created.

#4) All the recent versions of Eclipse provide the Maven plugins.

#5) To check if your Eclipse has Maven already present, navigate to Window => Preferences, here Maven should be visible.

Maven Preferences

#6) In the same location in Eclipse, if we expand the Maven, we can see an option called User Settings. Here we specify the location of the Maven local repository where all the Jars for the projects get downloaded after the Maven connects to its own repository.

By default it is .m2 folder, however, if it is not set, then we need to specify the location explicitly.

Maven User Settings

#7) Now we need to import the project we created to Eclipse by following the steps as File => Import => Maven => Existing Maven Project. Proceed and we will have our project in Eclipse along with the pom.xml.

The project shall have the following skeleton:

  • Maven Dependencies
  • src /main /java
  • src /test /java
  • src
  • target
  • pom.xml

We have to keep the class file inside the src/test/java folder. In order to develop Java framework in Selenium or Appium or Rest Assured, we have to add the jars and dependencies of the Selenium in Java, Appium in Java, and Rest Assured in Java to the pom.xml file.

As per the Maven algorithm, the class file should have a name with the Test appended to the name. For example, the class name can be SeleniumJavaTest.

#8) For running this project from the command prompt, we need to first navigate to the project folder (location of the pom. Xml file). The path of the pom file can be found by doing a right-click on it, then navigate to properties and copy the location.

#9) Now the following commands are run in order to achieve the specific purposes:

  • mvn clean: Used to clean the previous build information or artifacts.
  • mvn compile: Used to compile code and check if there are syntax errors in our test. If the result is a BUILD SUCCESS, then it means that we have no error in syntax in our code.
  • mvn test: Used to trigger our test project execution. Moreover, if we skip the commands (clean and compile) and directly execute test command, then also it shall first perform clean and compilation of the code, then execute and produce results.

Advantages of setting up Maven Project from the command prompt:

  • Very useful if we like to configure Maven with Continuous Integration tools like Jenkins.
  • No need to open IDEs like Eclipse to run and trigger our project manually, just need to navigate to the location of the pom file.

Maven POM (Project Object Model)

Project Object Model or POM is the basic part of the Maven functionality. This is an XML file that has information on the dependencies, configurations, and other important information about the project. Maven goes through this information and then performs the designated task.

Given below is the list of information that the pom.xml file contains:

  1. Project dependencies
  2. Plugins
  3. Goals for the project
  4. Profiles
  5. Version
  6. Description of the project
  7. Distribution list
  8. Developers
  9. Directory of the source folder
  10. Directory of build
  11. Directory of the test source

What Is Super POM?

There is a parent-child relationship between the POM files in a project. The pom file we developed for our specific project inherits the properties of the super pom.

What Is Minimal POM Configuration?

The minimal pom configuration refers to the groupId, artifactId, and version defined for our project. It is easy and simple to describe minimal pom configuration.

Given below is a code snippet for a minimal pom configuration.

<project>
<modelVersion> 1.0 </modelVersion>
<groupId > com.TestProject </groupId>
<artifactId > MavenJavaProject </artifactId>
<version > 3.0 </version>
</project>

In case there are no minimal configurations defined, then Maven shall fetch the necessary information from the super pom.xml file.

What Is Default POM Configuration?

The default pom configuration solely depends on the archtype. For example in a Maven project that has quickstart archtype, by default, has a pom file 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion> 3.8.0 </modelVersion>
<groupId> KeywordFramework </groupId>
<artifactId> Excel </artifactId>
<version> 0.0.1-S </version>

<dependencies>
< !-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId> org.apache.poi </groupId>
<artifactId> poi-ooxml </artifactId>
<version> 4.1.1 </version>
</dependency>
< !-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId> org.apache.poi </groupId>
<artifactId> poi </artifactId>
<version> 4.1.1 </version>
</dependency>

</dependencies>
</project>

How Is POM Hierarchy Maintained In The Maven Project?

The pom file we use is a fusion of the pom file of the project, super pom file, and parent pom file (if present). This is called an effective pom file.

In order to generate an effective pom file, navigate to the project folder, and execute the following command:

mvn help:effective-pom

Key Features Of pom.xml File In Maven

  • Name: As the name suggests, it describes the name of the project. There is a difference between the name and artifactId. While artifactId identifies a project uniquely and is considered as a basic step. Name is just a readable name and is not considered as a mandatory step for identifying a project in Maven.
  • URL: This describes the url of the project. Similar to the name, url is not a mandatory tag. It mostly provides additional data about the project.
  • Packaging: This details the package type in the form of jars or war.
  • Dependencies: They describe the dependencies of the project. Each dependency is a part of the dependencies tag. Dependencies tag contain multiple dependencies.
  • Dependency: They describe individual dependency information like the groupId, artifactId, and version.
  • Scope: They outlines the periphery of the project. It can have the following values like import, system, test, runtime, provided, and compile.
  • Project: This is the root tag for the pom.xml file.
  • Model version: This is a part of the project tag. It defines the model version and for Maven 2 and 3, its value is set to 4.0.0.

POM.XML Example

Given below is a sample xml code with the above POM features:

<project>
  <modelVersion> 3.7.0 </modelVersion>

  <groupId> com.softwarehelp </groupId>
  <artifactId> Selenium Maven </artifactId>
  <version>1.0- S </version>
  <packaging> war </packaging>

  <name> Maven Tutorial Series </name>
  <url>http://maven.apacheseries.org</url>

  <dependencies>
   <groupId> org.apache.poi  </groupId>
    <artifactId> poi </artifactId>
    <version> 4.1.1 </version>
  </dependencies>
</project>

The other key features of the pom.xml file like groupId, artifactId, and version have been explained in detail in the introductory tutorial on Maven.

Conclusion

We hope that most of your doubts on how to do the environment set up for Maven, how to build a project on Maven both from Eclipse as well as from the command prompt should be clear now.

This tutorial also explained what is POM and features of the pom.xml file in detail along with examples. Maven is a very useful build tool that has really made the task of developers, testers and other people involved easy and simple.

In the next tutorial, we shall discuss the difference between Gradle & Maven, plugins, and other related topics.

=> Check Here To See A-Z Of Maven Training Tutorials.