In this article, we will explore and understand the major elements of “pom.xml” file
Q) What is pom.xml in Maven?
POM stands for Project Object Model and this is specific to every project. In fact every Maven based project should have one pom.xml lying at the project’s root directory describing its various configurations relating to project
These configurations are
- project dependencies
- plugins with its goals
- build profiles
- developers involved in the project development
- mailing lists
- the organization and licenses
- the URL of where the project lives
- and other little pieces related to the project
In simple terms to summarize, pom.xml is a one-stop-shop concerning the project
1. Major elements of pom.xml
- project –> root element
- modelVersion –> mandatory element with value 4.0.0 which is currently supported version for both Maven 2 & 3
- groupId –> this is the top-most element while considering maven repository structure and its generally kept unique amongst organization
- artifactId –> this is similar to name of the project and this lies under <groupId> element
- version –> this is final element in Maven co-ordinate system and generally it defines version of the project. Like, initially it can be kept 1.0 and later with couple changes version changes to 1.1 or 2.0 depending on the major/minor releases. Together with <groupId> & <artifactId> elements acts like an address and timestamp in one
- name –> any meaningful name to the project
- description –> description of the project
- packaging –> this elements defines how exactly this project needs to be bundled or packaged after development. Various packaging standards in Java/JEE project developemtns are JAR or WAR or EAR. When no <packaging> element defined, then by default final products is packaged as JAR
- url –> this elements can be used to define where exactly project going to live after development (normally it points to SVN location)
- dependencies/dependency –> defines what are the required dependencies for this project into developments (read here about dependency scopes and transitive dependencies)
- build/plugins –> various plugins along with their goals can be configured under <build> element (read here about Maven plugins)
- finalName –> this is the child element under <build> element defining what will be final name of the project and a JAR or WAR or EAR will be generated under “target” folder with this name
pom.xml
<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">
<!-- Currently supported version is 4.0.0 for Maven 2 & 3 -->
<modelVersion>4.0.0</modelVersion>
<!-- The Basics -->
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<packaging>...</packaging>
<dependencies>
...
</dependencies>
<parent>
...
</parent>
<dependencyManagement>
...
</dependencyManagement>
<modules>
...
</modules>
<properties>
...
</properties>
<!-- Build Settings -->
<build>
...
</build>
<reporting>
...
</reporting>
<!-- More Project Information -->
<name>...</name>
<description>...</description>
<url>...</url>
<inceptionYear>...</inceptionYear>
<licenses>
...
</licenses>
<organization>
...
</organization>
<developers>
...
</developers>
<contributors>
...
</contributors>
<!-- Environment Settings -->
<issueManagement>
...
</issueManagement>
<ciManagement>
...
</ciManagement>
<mailingLists>
...
</mailingLists>
<scm>
...
</scm>
<prerequisites>
...
</prerequisites>
<repositories>
...
</repositories>
<pluginRepositories>
...
</pluginRepositories>
<distributionManagement>
...
</distributionManagement>
<profiles>
...
</profiles>
</project>
2. Three important elements of pom.xml (Minimum requirement):
- Apart from root element i.e.; <project> and <modelVersion> element, there are 3 more mandatory elements required for a minimum pom.xml to exists and run/execute successfully
- These mandatory elements are groupId, artifactId, and version
3. Minimum pom.xml
pom.xml
<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>in.bench.resources</groupId>
<artifactId>MavenTestArticle</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>MavenTestArticle</name>
<description>This is test project to try out various features in Maven</description>
</project>
Q) How to get effective pom.xml (Effective POM tab in Eclipse IDE) ?
There are two types of POM, when dealing with Maven based projects
- Application pom.xml
- Effective pom.xml
Application POM
- Configuration we do specific to any Java/JEE projects is called “application POM”. These configurations normally contain dependencies, plugins along with their goals and build profiles
Effective POM
- After configuring application POM for any Java/JEE projects, Maven constructs an “effective POM” with required plugins and inheriting mandatory properties and other elements configured in settings.xml before executing any commands or goals
- To get effective POM, use Maven command (from command prompt or terminal)
mvn help:effective-pom
- In Eclipse IDE, when we open pom.xml in editor there are 5 tabs available. Out of these 5 tabs, “Effective POM” tab shows the effective pom.xml
4. Configuring source/target JDK version in Maven compiler plugin:
- There are couple of plugins adds up in the effective POM, one such plugin is Maven compiler plugin. Basically this plugins dictates which version of JDK to use to compile the source code
- By default, target and source setting is 1.5. But we can increase to higher level to JDK_1.6 or JDK_1.7 or JDK_1.8 depending on the business requirement by configuring (overriding) maven-compiler-plugin in our application POM
- For example, let’s configure to JDK 1.7
<!-- plugin 2-maven compiler plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
5. Inheritance from parent pom.xml concept in multi-module project:
- If we want to segregate different projects based on the domains, then each project will contain single pom.xml
- Let’s assume AddressProject, CustomerProject and NetworkProject are three projects and their inter-dependencies are sequenced as below
- NetworkProject –> CustomerProject –> AddressProject
- Now we will create another project which will be parent of these three projects and here we will mention what are all the child projects with <modules>/<module> elements in pom.xml
- So, if we execute any maven command in the parent pom.xml then it will start building the child projects as well (no need consider sequence, Maven takes care of it)
- Also, dependencies configured in the parent POM (project) will be inherited to child POM (projects) –> just we have to mention <groupId> and <artifactId> elements and there is absolutely no need of <version> element, as this will be considered/inherited from parent POM. But if we want to override, then we have to specifically mention in the distinct child POM with <version> element
Useful Eclipse IDE shortcuts :
- Eclipse IDE – How to show line numbers ?
- Eclipse IDE – How to GO TO any line number directly ?
- Eclipse IDE – How to remove unused imports ?
- Eclipse IDE – How to clean project ?
- Eclipse IDE – How to build Java project automatically ?
- Eclipse IDE – How to comment and un-comment line & block ?
- Eclipse IDE – How to generate constructor using fields ?
- Eclipse IDE – How to generate getters and setters ?
- Eclipse IDE – How to search files ?
- Eclipse IDE – How to locate methods in Java file ?
- Eclipse IDE – How to open editor using CTRL + E ?
- Eclipse IDE – Java compiler compliance level issue
Related Articles:
- Apache Maven – Introduction
- Apache Maven – Install on Windows 7 OS
- Apache Maven – Settings.xml explanation
- Apache Maven – Proxy setting explanation
- Apache Maven – pom.xml explanation
- Apache Maven – Plugins explanation
- Apache Maven – Changing default Maven repository location in Windows 7 OS
- Apache Maven – Local, Central and Remote Repositories
- Apache Maven – Installing custom library into local repository
- Apache Maven – Transitive dependencies explanation
- Apache Maven – Exclusion of Transitive dependencies
- Apache Maven – Dependency Scopes
- Apache Maven – Skipping unit test using surefire plugin
- Apache Maven – Exclusions and Inclusions of unit test
- Apache Maven – offline execution
- Apache Maven – Co-ordinates explained
- Eclipse + Maven – Integration
- Eclipse + Maven – How to import Maven project with pom.xml ?
- Eclipse + Maven – Setting M2_REPO classpath variable in IDE
- Eclipse + Maven – M2_REPO is Non Modifiable
- Eclipse + Maven – Creating and exploring projects using archetypes
- Eclipse + Maven – Converting Web project to Maven project
- Eclipse + Maven – mvn eclipse:eclipse command
- Eclipse + Maven – Plugin execution not covered by lifecycle configuration
References:
- http://maven.apache.org/pom.html
- http://maven.apache.org/pom.html#What_is_the_POM
- http://maven.apache.org/pom.html#The_Basics
- http://maven.apache.org/guides/introduction/introduction-to-the-pom.html
- http://en.wikipedia.org/wiki/Apache_Maven#Project_Object_Model
- https://www.benchresources.net/apache-maven-plugins-explanation
- https://www.benchresources.net/apache-maven-dependency-scopes
- https://www.benchresources.net/apache-maven-transitive-dependencies-explanation
- http://maven.apache.org/plugins/maven-help-plugin/usage.html
- http://maven.apache.org/plugins/maven-compiler-plugin/
- http://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html
Happy Coding !!
Happy Learning !!