Java ProgrammingLearn About the Project Object Model Tool in Maven

Learn About the Project Object Model Tool in Maven

In this article, we are going to discuss about the POM which is the fundamental unit of work in the Apache Maven tool. The full form of POM is Project Object Model. Maven tool obeys the Project Object Model where the project structure and contents are defined and declared in an xml file known as pom.xml. This xml file always exist in the base directory of the project with the file name as pom.xml.

Contents of POM
The pom.xml file contains the project level information as well as various configuration details which should be used by Apache Maven tool in order to build the project(s). POM file also contains the plugins and tasks which should be executed during building a project. Apache Maven tool looks for the POM in the current directory and reads it to fetch the required configuration information in order to execute the tasks or goals related to the project build. The following are some of the configuration which can be specified in the POM configuration file.
• Project dependencies
• Plugins
• Goals
• Build profiles
• Project version
• Developers
• Automated mailing list

Prerequisites for creating a POM file
There are some prerequisites before we can create a POM file for any project. These prerequisites are the project group (groupId), the Artifact ID or project name (artifactId) and its version. All of these attributes help in the unique identification of the project in the project repository. When we use eclipse to create a Maven project, it prompts to enter these details in a dialogue box as shown below.
Object Maven Projects
Example of POM file

<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>com.edunoix</groupId>
  <artifactId>maven_demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>maven_demo</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

In a multi-tier JAVA project environment, it should be noted that for every project there exists a single POM file and each of these projects require the project element and the three mandatory fields such as groupId, artifactId, and version. The projects notation in a repository is groupId: artifactId: version. The root element of the pom.xml file is project which has the three major sub nodes as follows:

S No

Node

Description

1.

groupId The groupId represents an Id of the project’s group. This Id must be always unique amid an organization or a project. E.g., a technology company group com.edunoix.tech has all technology related projects.

2.

artifactId The artifactId represents an Id of the project. It is the general name of the project. E.g., a consumer-technology. The groupId along with the artifactId define the artifact’s location within the repository.

3.

version The version represents the version of the project. It is used along with the groupId within an artifact’s repository in order to separate versions from each other. The following are the examples.

com.edunoix.tech: consumer-technology: 1.0.0.

com.edunoix.tech: consumer- technology: 1.1.0.

Concept of Super POM
In a multi-tier project, all project POMs always inherit from a parent and there is no need to define it explicitly. This base POM is nothing but the Super POM that contains the values to be inherited by the child POM files. Apache Maven uses the effective POM which includes the configuration from the super or base POM along with the current project configurations in order to execute the required task or goal. Such a hierarchy of POMS helps developer to specify minimum possible configuration detail at the project level in the pom.xml file. Also, all these POM configurations can be overridden easily. We can execute the following command in order to look the default configurations of the super POM.

mvn help:effective-pom

In the following example, we are going to use the pom.xml file which is created for the project “maven_demo” as explained before. We can use this POM file to execute the above maven help command for looking the default configurations of the super POM. We can open a console or windows command prompt to execute this command. The following will be the logs on the console post execution of the above command. Here, we can observe the effective POM used by the Maven tool.

[INFO]
Effective POMs, after inheritance, interpolation, and profiles are applied:

<!-- ====================================================================== -->
<!--                                                                        -->
<!-- Generated by Maven Help Plugin on 2017-06-18T06:25:48                  -->
<!-- See: http://maven.apache.org/plugins/maven-help-plugin/                -->
<!--                                                                        -->
<!-- ====================================================================== -->

<!-- ====================================================================== -->
<!--                                                                        -->
<!-- Effective POM for project 'com.edunoix:maven_demo:jar:0.0.1-SNAPSHOT'  -->
<!--                                                                        -->
<!-- ====================================================================== -->

<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>com.edunoix</groupId>
  <artifactId>maven_demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>maven_demo</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <repositories>
    <repository>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
    </pluginRepository>
  </pluginRepositories>
  <build>
    <sourceDirectory>C:\work\project\maven_demo\src\main\java</sourceDirectory>
    <scriptSourceDirectory>C:\work\project\maven_demo\src\main\scripts</scriptSourceDirectory>
    <testSourceDirectory>C:\work\project\maven_demo\src\test\java</testSourceDirectory>
    <outputDirectory>C:\work\project\maven_demo\target\classes</outputDirectory>
    <testOutputDirectory>C:\work\project\maven_demo\target\test-classes</testOutputDirectory>
    <resources>
      <resource>
        <directory>C:\work\project\maven_demo\src\main\resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>C:\work\project\maven_demo\src\test\resources</directory>
      </testResource>
    </testResources>
    <directory>C:\work\project\maven_demo\target</directory>
    <finalName>maven_demo-0.0.1-SNAPSHOT</finalName>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.2-beta-5</version>
        </plugin>
        <plugin>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>2.8</version>
        </plugin>
        <plugin>
          <artifactId>maven-release-plugin</artifactId>
          <version>2.3.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
    <plugins>
      <plugin>
        <artifactId>maven-clean-plugin</artifactId>
        <version>2.5</version>
        <executions>
          <execution>
            <id>default-clean</id>
            <phase>clean</phase>
            <goals>
              <goal>clean</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.6</version>
        <executions>
          <execution>
            <id>default-testResources</id>
            <phase>process-test-resources</phase>
            <goals>
              <goal>testResources</goal>
            </goals>
          </execution>
          <execution>
            <id>default-resources</id>
            <phase>process-resources</phase>
            <goals>
              <goal>resources</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <executions>
          <execution>
            <id>default-jar</id>
            <phase>package</phase>
            <goals>
              <goal>jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <executions>
          <execution>
            <id>default-compile</id>
            <phase>compile</phase>
            <goals>
              <goal>compile</goal>
            </goals>
          </execution>
          <execution>
            <id>default-testCompile</id>
            <phase>test-compile</phase>
            <goals>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.12.4</version>
        <executions>
          <execution>
            <id>default-test</id>
            <phase>test</phase>
            <goals>
              <goal>test</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-install-plugin</artifactId>
        <version>2.4</version>
        <executions>
          <execution>
            <id>default-install</id>
            <phase>install</phase>
            <goals>
              <goal>install</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-deploy-plugin</artifactId>
        <version>2.7</version>
        <executions>
          <execution>
            <id>default-deploy</id>
            <phase>deploy</phase>
            <goals>
              <goal>deploy</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-site-plugin</artifactId>
        <version>3.3</version>
        <executions>
          <execution>
            <id>default-site</id>
            <phase>site</phase>
            <goals>
              <goal>site</goal>
            </goals>
            <configuration>
              <outputDirectory>C:\work\project\maven_demo\target\site</outputDirectory>
              <reportPlugins>
                <reportPlugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-project-info-reports-plugin</artifactId>
                </reportPlugin>
              </reportPlugins>
            </configuration>
          </execution>
          <execution>
            <id>default-deploy</id>
            <phase>site-deploy</phase>
            <goals>
              <goal>deploy</goal>
            </goals>
            <configuration>
              <outputDirectory>C:\work\project\maven_demo\target\site</outputDirectory>
              <reportPlugins>
                <reportPlugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-project-info-reports-plugin</artifactId>
                </reportPlugin>
              </reportPlugins>
            </configuration>
          </execution>
        </executions>
        <configuration>
          <outputDirectory>C:\work\project\maven_demo\target\site</outputDirectory>
          <reportPlugins>
            <reportPlugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-project-info-reports-plugin</artifactId>
            </reportPlugin>
          </reportPlugins>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <reporting>
    <outputDirectory>C:\work\project\maven_demo\target\site</outputDirectory>
  </reporting>
</project>

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 26.323 s
[INFO] Finished at: 2017-06-18T18:25:48-04:00
[INFO] Final Memory: 12M/81M
[INFO] ------------------------------------------------------------------------

C:\work\project\maven_demo>

Conclusion: –
In this tutorial, we discussed about the POM which is the fundamental unit of the Apache Maven tool. Also, we have demonstrated POM and super POM inheritance along with the creation of the effective POM through Maven tool to display its content.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -