What is POM?
- The core element of any maven project.
- Maven Project consist of at least one configuration file called pom.xml. Which stands for the abbreviation Project Object Model.
- The pom.xml will always be located in the root directory of any maven project.
- Inside the pom.xml contains all the necessary information about the configuration details. dependency included and plug-ins included in the project.
Why do we need POM?
- In the Legacy project, there is no concept of any configuration file so that type of project when we add some other dependency jar so that task is a very difficult task because adding jar or any dependency take some steps first you find the jar on the web after that you add that jar in your project.
- Let suppose in the Legacy project we need to upgrade our jar to the latest version. first, download all the latest jars on the web after that we will remove the older jar and add the new jar to the project. this is another disadvantage of that type of project.
- If we use pom.xml in our project so we can download any jar to add this dependency to pom and we can simply upgrade our project jar without any headache.
Super POM
The super pom is parents pom for all the other pom files and it is maven default pom. All POMs extend the Super POM unless explicitly set.
The benefit of the super pom is it gives the common configuration and also the child will add some extra configuration in its pom.xml if needed. Although configurations can be overridden easily.
Elements of maven pom.xml file
Element | Description |
---|---|
project | It is the root element of pom.xml |
modelVersion | It is the sub-element of the project. It specifies the modelVersion. e.g 4.0.0. |
groupId | It is the sub-element of the project. It specifies the id for the project group. |
artifactId | It is the sub-element of the project. It specifies the id for the artifact (project). An artifact is something that is either produced or used by a project. Examples of artifacts produced by Maven for a project include JARs, source and binary distributions, and WARs. |
version | It is the sub-element of the project. It specifies the version of the artifact under the given group. |
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.myapp</groupId>
<artifactId>myapp</artifactId>
<version>1.0-SNAPSHOT</version>
</project>
Some other important additional elements in pom.xml
Element | Description |
---|---|
packaging | Define the package type of the project e.g war, jar, etc |
name | Define the name of the project |
URL | Define the URL of the project |
dependencies | Define the dependencies of the project |
dependency | Define the dependency of the project, inside the dependencies |
scope | defines the scope for this maven project e.g compile, test, etc. |
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<groupId>com.example.myapp</groupId>
<artifactId>myapp</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>myapp</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
</dependencies>
</project>
Project Inheritance
If we have multiple maven projects and all the projects we have similar configurations so we can use these features “Project Inheritance“. For that, we add similar configurations and make a parent project and we need to inherit that parent project, and those configurations would then be applied to all of them.
POM inheritance is an important feature in maven and inheritance can be done for,
- dependencies.
- developers and contributors.
- plugin lists.
- plugin executions with matching ids.
- plugin configuration.
To inherit pom <parent> tag must be part of child pom. The structure of child pom should be like below to inherit from super pom,
<project>
<parent>
<groupId>abc.xyx</groupId>
<artifactId>paretn_id</artifactId>
<version>x.y.z</version>
</parent>
....
</project>
Project Aggreation
It is similar to Project Inheritance. But instead of specifying the parent POM from the module, it specifies the modules from the parent POM.
|-- dummy-app
\ `-- pom.xml
|-- dummy-module-1
| `-- pom.xml
|-- dummy-module-2
| `-- pom.xml
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.dummy</groupId>
<artifactId>dummy-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>dummy-module-1</module>
<module>dummy-module-2</module>
</modules>
<dependencyManagement>
...
</dependencyManagement>
</project>
Project Interpolation
It works as a placeholder. If we have a situation where we will need to same value in several different locations so for that particular use case, Maven allows you to use both your own and pre-defined variables in the POM for example, if you want to define your version and access the value so you can define it like this –
<version>${project.version}</version>
Note – These variables are processed after inheritance as outlined above. This means that if a parent project uses a variable, then its definition in the child, not the parent, will be the one eventually used.
Project Model Variables – Any field of the model that is a single value element can be referenced as a variable. For example, ${project.groupId}
, ${project.version}
and so on.
These variables are all referenced by the prefix “project.
“. You may also see references with pom.
as the prefix, or the prefix omitted entirely – these forms are now deprecated and should not be used.
Maven Plugins
In a maven, if we do some task likes creating a jar, creating a war, compiling code, and creating a project report these are all tasks done by the plugins. Basically plugins we provide a set of some tasks which can be executed using the following syntax –
mvn [plugin-name]:[goal-name]
mvn compiler: compile
There are two types of plugins –
- Build plugins :- Build plugins basically used for creating a build and It configured in <build/> element of pom.xml.
- Reporting plugins :- It executes during the site generation process and It configured in the element of the pom.xml.
References
For more information on pom.xml, Click here.
great job