DEV Community

Kathir
Kathir

Posted on

Project Object Model

Yesterday, we went through Maven. Now, we are going to learn about the Project Object Model (POM).

POM

Project object model 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.

Super POM

Super POM is the default POM that maven provides internally. Every maven project inherits from the Super pom even if there is no mentioning.

pom.xml doesn't start from scratch. Maven first loads the Super POM and then merges the pom.xml with it.

Super POM contains some defaults:

  1. Central Repository= Even if the pom.xml doesn't mention a specific repository, maven downloads dependencies from maven central because super POM includes it.
  2. Default Build directory=> Super POM tells that the target is where build should be present.
  3. Plugin Reposity=> The super pom tells Maven where to download plugins such as surefire plugin, compiler plugin, JAR plugin.

Effective POM

Super POM +pom.xml is what Maven actually uses during the build.

Minimal POM

The minimum requirement for a POM:

  • project (root)
  • modelVersion= should be set to 4.0.0 which is the version of POM XML model
  • groupId
  • artifactId
  • version = which is the project version

Project Object Model

When Maven reads pom.xml, it converts pom.xml into a Java Object internally.

<project xmlns="http://maven.apache.org/POM/4.0.0">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany.app</groupId>
    <artifactId>my-app</artifactId>
    <version>1</version>
</project>
Enter fullscreen mode Exit fullscreen mode

Internally maven creates something like this

Project project=new Project();
project.setModelVersion("4.0.0");
project.setGroupId("com.mycompany.app");
project.setArtifactId("my-app");
project.setVersion("1.0");
Enter fullscreen mode Exit fullscreen mode

The structure of the Java object of maven conversion is called the POM model.

A POM requires groupId, artifactId, and version and these helps to form the project's fully qualified artifact name. It is in the form of:

<groupId>:<artifactId>:<version>
Enter fullscreen mode Exit fullscreen mode

Artifact name example:

com.mycompany.app:my-app:1
Enter fullscreen mode Exit fullscreen mode

POM helps to configure details by using defaults if they are not specified.

Example:

Every maven project has packaging type. If it is not specified in the POM then the default value be "jar".

Project Inheritance

It means that one project (child) can inherit configuration from another project (parent). This helps avoid repeating the same configurationa cross multiple projects.

Project inheritance can be done using super pom and parent pom.

Parent POM

It is created with the help xml tag <parent></parent>

<project xmlns="http://maven.apache.org/POM/4.0.0">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.mycompany.app</groupId>
        <artifactId>my-app</artifactId>
        <version>1</version>
        <relativePath>../parent/pom.xl>/relativePath>
    </parent>
    <artifactId>my-module</artifactId>
</project>
Enter fullscreen mode Exit fullscreen mode

Elements in the POM that are merged are the following:

  • dependencies
  • developers adn contributors
  • plugin lists
  • plugin execustion with matching ids
  • plugin configuration
  • resources

Super POM cannot be edited but can be override in pom.xml file.

Project Aggregation

Project Aggregation is similar to Project Inheritance. But instead of specifying the parent POM from the module, it specifies modules from parent POM. By, doing it the parent project knows its modules. If a mavent command is invoked against parent project, that Maven command will then be executed to the paren'ts modules as well.

It is mainly used in microservices. Since, it contains many services like web,common,api,services as projects. It is hard to run each one separately like mvn install. That is hard to maintain so we create a common parent and invoke all of it.

Parent pom.xml

<modules>
    <module>library-common</module>
    <module>library-api</module>
    <module>library-service</module>
    <module>library-web</module>
<modules>
Enter fullscreen mode Exit fullscreen mode

This tells maven that these are the project that belong together.

Project Model Variables

Maven allows you to reference any single-value filed from the project's POM using variables.

Syntax:

${project.properyName}
Enter fullscreen mode Exit fullscreen mode

Example:

${project.groupId}
Enter fullscreen mode Exit fullscreen mode

Special Maven Variables

1. project.basedir

Represents the directory where the project is located.

2. project.baseUri

Same as project.basedir but returned as a URI (Uniform Resource Identifier= It is a string that identifes a resouce.

Syntax:

schem://path
Enter fullscreen mode Exit fullscreen mode

Example:

file:/C:/projects/MyApp
Enter fullscreen mode Exit fullscreen mode

3. maven.build.timestamp

Represents the UTC timestamp whent he maven build starts.

Default output timestamp

yyyy-mm-dd'T'HH:mm:ss'Z'
Enter fullscreen mode Exit fullscreen mode

We can customize the formal by using properties.

<properties>
    <maven.build.timestamp.format>
        yyyy-MM-dd'T'HH:mm:ss'Z'
    </maven.build.timestamp.format>
<properties>
Enter fullscreen mode Exit fullscreen mode

Maven Properties

Defining own reusable variable can done via <properties> section.

Syntax:

<properties>
    <propertyName>value</properyName>
</properties>
Enter fullscreen mode Exit fullscreen mode

We can use it as

${propertyName}
Enter fullscreen mode Exit fullscreen mode

UTC memory tip

  • M=Month
  • m=Minute
  • H=24-hours
  • h=12-hours
  • D=Day of year
  • d=Day of month
  • S=Smallest visible unit in UTC:Millisecond
  • s=second
  • Z=Numeric zone offset(+0530)
  • z=Zone name (IST,UTC)

Patterns:

  • MMM= Jul
  • MMMM=July
  • yyyy=2026
  • yy=26
  • hh:mm:ss a=02:35:20 PM

T=> Separator between date and time (ISO 8601 format)

Why use UTC (Coordinated Universal Time)?

It provides a common time reference so sytems in different countries record the same moment consistenly.

POM non-default fields

1. <name>

This is the human-readable name of the project.

Used in Generated documentation, IDE's display, Reports.

2. <url>

Project homepage or repository link. Only used for documentation so it can be any link.

3. Properties

This element contains value placeholder accessible anywhere within a POM.

4. dependencies

This element's childrent list dependencies.

5. build

This element hanles things like declaring your project's directory structure and managing plugins.

Top comments (0)