Maven Tutorials – POM in Maven

Previous – Chapter 2

Maven Tutorial – Chapter 3

What is POM?

The easiest way to describe a POM in a maven project is, it is nothing but the core element of any maven project. Basically any maven project consists of one configurable file called pom.xml, which stands for the abbreviation “Project Object Model”. This pom.xml will always be located in the root directory of any maven project. This file represents the very basic and fundamental unit in maven.

The pom.xml basically contains the information related to the project which is built or to be built in. It contains all the necessary information about the configuration details, dependencies included and plug-ins included in the project. In simple, it contains the details of the build life cycle of a project.

Below are some of the configurations that can be handled in the pom.xml file:

  • Dependencies used in the projects (Jar files)
  • Plugins used (report plugin)
  • Project version
  • Developers involved in the project
  • Mailing list
  • Reporting
  • Build profiles

Sample pom.xml file

A typical pom.xml of simple java project will look like below.

Sample POM xml
Sample POM xml

A pom.xml will always start with the root element called <project> under which all the other required configurations will be made. Developers should ensure to define the below list of elements which are known as “maven co-ordinates” before defining a pom.xml file:

  1. groupId – as the name itself shows that this is an id which is unique for any project in an organization.
  2. artifactId – Even though the name says as “id”, this is basically defines the name of any project.
  3. version – This element is used to derive the version of any project in order to classify the versions as and when the major changes/implementations are carried during the development phase of a project.

Whenever it comes for executing a task for a project, maven scans through the entries made in the pom. Xml file. This will enable the maven to read all the configurations made, build profiles defined, repositories configured and all other important details and then executes the task accordingly.

Note: Actually pom.xml was earlier used in the name of project.xml in maven 1. As the maven 2 version was introduced, this was renamed to pom.xml

Super POM

All the maven projects pom.xml files always extends the super pom.xml file. This super pom.xml basically defines a set of default configurations which is shared and used by all the maven projects. This super pom.xml is not required to be written by the developers. This will come as a default one with the maven installation.

A typical super pom.xml can be found below.


<?xml version="1.0"?>
<project>
 <modelVersion>4.0.0</modelVersion>
 <name>Maven Default Project</name>

 <repositories>
     <repository>
       <id>central</id> (1)
       <name>Maven Repository Switchboard</name>
       <layout>default</layout>
       <url>http://repo1.maven.org/maven2</url>
       <snapshots>
         <enabled>false</enabled>
       </snapshots>
    </repository>
 </repositories>

 <pluginRepositories>
     <pluginRepository>
        <id>central</id> (2)
        <name>Maven Plugin Repository</name>
        <url>http://repo1.maven.org/maven2</url>
        <layout>default</layout>
        <snapshots>
          <enabled>false</enabled>
        </snapshots>
        <releases>
          <updatePolicy>never</updatePolicy>
        </releases>
      </pluginRepository>
 </pluginRepositories>

 <build> (3)
      <directory>${project.basedir}/target</directory>
         <outputDirectory>${project.build.directory}/classes</outputDirectory>
         <finalName>${project.artifactId}-${project.version}</finalName>
         <testOutputDirectory>
           ${project.build.directory}/test-classes
         </testOutputDirectory>
          <sourceDirectory>
           ${project.basedir}/src/main/java
         </sourceDirectory>
         <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>
         <testSourceDirectory>
            ${project.basedir}/src/test/java
         </testSourceDirectory>
      <resources>
        <resource>
           <directory>${project.basedir}/src/main/resources</directory>
        </resource>
      </resources>
     <testResources>
        <testResource>
          <directory>${project.basedir}/src/test/resources</directory>
        </testResource>
     </testResources>

     <pluginManagement> (4)
        <plugins>
          <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.3</version>
          </plugin>
          <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.2-beta-2</version>
          </plugin>
          <plugin>
            <artifactId>maven-clean-plugin</artifactId>
            <version>2.2</version>
            </plugin>
          <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.0.2</version>
          </plugin>
          <plugin>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.0</version>
           </plugin>
          <plugin>
            <artifactId>maven-deploy-plugin</artifactId>
            <version>2.4</version>
          </plugin>
          <plugin>
            <artifactId>maven-ear-plugin</artifactId>
            <version>2.3.1</version>
          </plugin>
          <plugin>
            <artifactId>maven-ejb-plugin</artifactId>
            <version>2.1</version>
          </plugin>
          <plugin>
            <artifactId>maven-install-plugin</artifactId>
            <version>2.2</version>
          </plugin>
          <plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.2</version>
          </plugin>
          <plugin>
            <artifactId>maven-javadoc-plugin</artifactId>
            <version>2.5</version>
          </plugin>
          <plugin>
            <artifactId>maven-plugin-plugin</artifactId>
            <version>2.4.3</version>
          </plugin>
          <plugin>
            <artifactId>maven-rar-plugin</artifactId>
            <version>2.2</version>
          </plugin>
          <plugin>
            <artifactId>maven-release-plugin</artifactId>
            <version>2.0-beta-8</version>
          </plugin>
          <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.3</version>
          </plugin>
          <plugin>
            <artifactId>maven-site-plugin</artifactId>
            <version>2.0-beta-7</version>
            </plugin>
          <plugin>
            <artifactId>maven-source-plugin</artifactId>
            <version>2.0.4</version>
          </plugin>
          <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.4.3</version>
          </plugin>
          <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1-alpha-2</version>
          </plugin>
     </plugins>
 </pluginManagement>

Goals in maven

Goal in maven is nothing but a particular task which leads to the compiling, building and managing of a project. A goal in maven can be associated to zero or more build phases. Only this matters is the order of the goals defined for a given project in pom.xml. Because, the order of execution is completely dependent on the order of the goals defined.

<pre><?xml version="1.0"?></pre>
<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.testorg.testgroup</groupId>
    <artifactId>project</artifactId>
    <version>1.0</version>
    <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.1</version>
        <executions>
          <execution>
             <id>id.pre-clean</id>
             <phase>pre-clean</phase>
             <goals>
               <goal>run</goal>
             </goals>
             <configuration>
                <tasks>
                   <echo>pre-clean phase</echo>
                </tasks>
             </configuration>
          </execution>
          <execution>
             <id>id.clean</id>
             <phase>clean</phase>
             <goals>
               <goal>run</goal>
             </goals>
             <configuration>
               <tasks>
                 <echo>clean phase</echo>
               </tasks>
             </configuration>
             </execution>
             <execution>
               <id>id.post-clean</id>
               <phase>post-clean</phase>
               <goals>
                 <goal>run</goal>
               </goals>
             <configuration>
               <tasks>
                 <echo>post-clean phase</echo>
               </tasks>
            </configuration>
          </execution>
      </plugin>
    </plugins>
  </build>
</project>

Next – Chapter 4


Comments

Questions? Comments? Suggestions? Let us know!! Like / Subscribe / Follow for more updates.