Sunday, December 18, 2011

Maven - Project Object Model




Maven - Project Object Model
POM
Concept, Tags, Case Studies, Examples

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. For examples the build directory, which is "target";
 the source directory, which is "src/main/java"; the test source directory, which is "src/main/test"; and so on.
POM also contains the goals and plugins. So, while 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.
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.
Some key elements of POM:
project This is the top-level element in all Maven pom.xml files.
modelVersion  This element indicates what version of the object model this POM is using. The version of the model itself changes very infrequently but it is mandatory in order to ensure stability of use if and when the Maven developers deem it necessary to change the model.
groupId  This element indicates the unique identifier of the organization or group that created the project. The groupId is one of the key identifiers of a project and will be fully explored in the Naming Conventions lesson.

artifactId  This element indicates the unique base name of the primary artifact being generated by this project. The primary artifact for a project is typically a JAR file. Secondary artifacts like source bundles also use the artifactId as part of their final name. A typical artifact produced would have the form <artifactId>-<version>.<extension> (for example, myapp-1.0.jar). It is another key identifier of a project and is fully explored in the Naming Conventions lesson.

packaging  This element indicates the package type to be used by this artifact (e.g. JAR, WAR, EAR, etc.). This not only means if the artifact produced is JAR, WAR, or EAR but can also indicate a specific lifecycle to use as part of the build process. (The lifecycle is a topic we will deal with further on in the guide. For now, just keep in mind that the indicated packaging of a project can play a part in customizing the build lifecycle.) The default value for the packaging element is JAR so you do not have to specify this for most projects.

version  This element indicates the version of the artifact generated by the project. Maven goes a long way to help you with version management. It is another key identifier of a project which is fully explained in the Naming Conventions lesson.

name  This element indicates the display name used for the project. This is often used in Maven's generated documentation.

url  This element indicates where the project's site can be found. This is often used in Maven's generated documentation.

description This elements provides a basic description of your project. This is often used in Maven's generated documentation.

Why POM
The POM is the basic unit of work in Maven. This is important to remember because Maven is inherently project-centric in that everything revolves around the notion of a project. Every task we ask Maven to perform will be conducted based upon the information found into our POM file. For instance, let's say we want to package our project. Before running the task, Maven will first read the packaging element of your POM to see what kind of archive is supposed to be generated for this project. Only then after knowing what we want, it will decide how to perform the operation based upon the retrieved value.
What is a POM made of ?
The POM can basically be decomposed into three different sections :
• The project-related properties : *[need some description]*




• The build process-related properties : *[need some description]*



• The environment-related properties : *[need some description]*




• The overall POM structure : *[need some description]*


POM elements order

 The POM has restrictions on the different elements order (since its structure is validated by an XML schema).

POM for Multi Module Project :-

Modules are projects that this POM lists, and are executed as a group. An pom packaged project may aggregate the build of a set of projects by listing them as modules, which are relative directories to those projects.

  <modules>
    <module>module_one<module>
    <module>module_two<module>
  </modules>

we do not need to consider the inter-module dependencies ourself when listing the modules, i.e. the ordering of the modules given by the POM is not important. Maven will topologically sort the modules such that dependencies are always build before dependent modules.

Parent-Child v/s Aggregation


Inheritance and aggregation create a nice dynamic to control builds through a single, high-level POM. You will often see projects that are both parents and aggregators. For example, the entire maven core runs through a single base POM org.apache.maven:maven, so building the Maven project can be executed by a single command: mvn compile. However, although both POM projects, an aggregator project and a parent project are not one in the same and should not be confused. A POM project may be inherited from - but does not necessarily have - any modules that it aggregates. Conversely, a POM project may aggregate projects that do not inherit from it.
For parent-child pom structure please refer Entities POM case study.

Case Studies


• ABCComponent :-

1. Define project’s Group, Artifact, packaging, name and version
 

{Line 1} project tag is top-level element of every pom.
{Line 2} model version tag value 4.0.0 is a version of the object model , which is in use for this pom file. In most of the cases the value should be 4.0.0 or more.
{Line 3}  group id tag indicates the unique identifier of the organization or group that created the project.
{Line 4} artifactId is id of the artifact (project) . ABCComponent-war in our case.
{Line 5} packaging tag is used to define package type for project. It may be war,jar,ear etc.
{Line 6} version  tag indicates 1.0-snapshot version will generated by the project.
{Line 9} Dependencies declared from line 9 in ABCComponent's  pom file. Here we described the jar files with their versions used by us. For example

2. Define dependencies
Here we are declaring dependency of “antlr.jar”, that we need 2.7.6 version of this jar.


3. Plugins and their configurations


{Line 381} In ABCComponent's POM from line number 381 build tag starts. Here build tag is used to define used plugins with their configuration , execution details etc.

for example in line number 385 Maven plugin is defined. In description of maven plugin we can define test cases with Maven setup should execute at the time of build.





Next (from {Line 395}) is declaration for 3 environments of project. With tags to split the configuration details.

{Line 432} Here Source Control Management (scm) tag is used to involve “starteam” repository at the time of build at client side.



{Line 436} Distribution management tag  is used to manage the distribution of the artifact and supporting files generated throughout the build process.
The sub-tag <repository> is defined to specify the POM the location and manner in which Maven may download remote artifacts for use by the current project, distributionManagement specifies where (and how) this project will get to a remote repository when it is deployed.



Next case study is ‘Employees’ application where we are using aggregation as well as parent child relationship in pom. Rest of the tags are same as ABCComponent.

2. Employees -


     Here POMs are defined with relation like aggregation and inheritance .

    The Parent POM contains aggregation of  6 projects.

Next is declarations of Profiles.

A profile element contains both an optional activation (a profile trigger) and the set of changes to be made to the POM if that profile has been activated.
Activations are the key of a profile. The power of a profile comes from its ability to modify the basic POM only under certain circumstances. Those circumstances are specified via an activation element.
Generally “activation ” tag contains following sub-elements
jdk: activation has a built in, Java-centric check in the jdk element. This will activate if the test is run under a jdk version number that matches the prefix given.
os: The os element can define some operating system specific properties shown above.
property: The profile will activate if Maven detects a property (a value which can be dereferenced within the POM by ${name}) of the corresponding name=value pair. In Entities it is “enableCiProfile=true”.
file: Finally, a given filename may activate the profile by the existence of a file, or if it is missing.

After dependencies now it's turn to define details of organization and license.

In Child's POM parent tag is used to define parent pom.


At last plugin for ejb is defined.



No comments: