Maven’s POM.xml – A file to build Projects
In the previous article i.e. ‘Apache Maven – A tool to build Artifacts’, we explored Apache Maven like what is Maven? Maven Repositories and Maven Build Lifecycle and Phases. So, moving further, here we will discuss more in detail about the POM.xml.
What is POM?
A Project Object Model or POM is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used by Maven to build the project.
It contains default values for most projects. Examples for this is the build directory, which is target; the source directory, which is “src/main/java;” the test source directory, which is “src/test/java;” and so on. When executing a task or goal, Maven looks for the POM in the current directory. It reads the POM, gets the needed configuration information, then executes the goal.
Maven Architecture
Inside the POM, some of the configuration that can be specified in the POM are the project dependencies, the plugins or goals that can be executed, the build profiles, and so on. Other information such as the project version, description, developers, mailing lists and such can also be specified. The POM also defines the relationships among modules of multi-module projects for building the software.
Elements of POM.xml:
The following are the project identifiers which are minimum for a POM.xml file.
- Project – It is the root element of POM.xml.
- modelVersion – This element is mandatory and should have a value of 4.0.0 which can support both versions of Maven 2 and 3.
- groupId – This element must be unique as it specifies the Project ID.
- artifactid – This should be same as the name of your project. This element specifies the Id of the artifacts (project).
- Version – This element defines the version of the project. For example, if you began a project then the value would initially be 1.0. Depending on the major and minor changes it will change according to the changes made in the project like 1.1 or 2.0.
A POM requires that its groupId, artifactId, and version be configured. These three values form the project's fully qualified artifact name. This is in the form of <groupId>:<artifactId>:<version>. As for the example above, its fully qualified artifact name is "com.mycompany.app:my-app:1".
Let's see the following sample POM.xml file for use of the elements listed above.
There are few more elements in the POM.xml that need to be discussed, one by one can see below.
- name – This is used to give a meaningful name for the project.
- packaging – This element defines project packaging standard. It defines how a project is bundled after it has been completed. If you don't define it then the packaging is the jar by default for the project.
- URL – This element defines the project URL.
- dependencies – This element defines all the required dependencies for the project.
- scope – This element provides the scope for the compilation of Maven project.
- properties – This section are value placeholders and accessible anywhere with pom by using notation.
- repository – This element is used to hold build artifacts and dependencies of varying types. The default local repository is in the .m2/repository folder under the user’s home directory.
- build – This element defines all the required plugins for the project.
- profiles – A profile is basically a set of configuration values. By using profiles, you can customize the build for different environments such as Production/Test/Development:
Let's see the following sample POM.xml file for use of the elements listed above.
Conclusion:
We discussed the POM.xml file and its elements of the Apache Maven build tool in detail. All code examples mentioned in this article are easy to understand and are basic. If we want to get the most out of Maven project then we need to learn about all the elements of pom.xml file, it is the most important part of a Maven project.