Monday, December 8, 2014

Maven: Project Object Model (POM) Overview

In this tutorial, we'll look at several real-world POM files.


Simple Web POM


This POM is from the Maven Web Project Deployment Tutorial.

  <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.mycompany.web</groupId>   
      <artifactId>mycompany-web</artifactId>   
      <version>1.0.0</version>   
      <packaging>war</packaging>   
   
      <build>   
           <sourceDirectory>src</sourceDirectory>   
           <finalName>mycompany-web</finalName>   
           <plugins>  
                <plugin>  
                     <groupId>org.apache.maven.plugins</groupId>  
                     <artifactId>maven-compiler-plugin</artifactId>  
                     <version>3.2</version>  
                     <configuration>  
                          <source>1.7</source>  
                          <target>1.7</target>  
                     </configuration>  
                </plugin>  
           </plugins>  
      </build>   
   
      <properties>   
   
           <maven.compiler.source>1.7</maven.compiler.source>   
           <maven.compiler.target>1.7</maven.compiler.target>   
   
           <failOnMissingWebXml>false</failOnMissingWebXml>   
   
           <javax.version>7.0</javax.version>   
   
           <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>   
           <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>   
      </properties>   
   
      <dependencies>   
   
           <dependency>   
                <groupId>javax</groupId>   
                <artifactId>javaee-api</artifactId>   
                <version>${javax.version}</version>   
                <scope>provided</scope>   
           </dependency>   
   
      </dependencies>   
 </project>   
   


Maven Coordinates

  • groupId
  • artifactId
    • The name that the project is known by (e.g. swtk-core).
  • version
    • The version of the artifact
      • I prefer to use major and minor versioning (eg. x.y.z)
  • packaging
    • the default package is JAR
    • pom, jar, maven-plugin, ejb, war, ear, rar, par.


Optional Information

  • url
    • where the project lives (eg. github URL)
  • inceptionYear
    • helpful documentation point
  • name
    • codename (or other nickname) for the project
    • has no bearing on artifact generation
      • Useful in console output
      • For example, if I include a module in a parent POM, the output from the maven build will give this name in the output, rather than the artifactId.
  • description
    • helpful documentation point


Build Information

  • sourceDirectory
  • finalName
  • Plugins
    • Compiler Plugin
      • This is an important plugin to configure for Eclipse Integration
        • Frequently, Eclipse will become out-of-date with the Maven process and needs to be refreshed.  
        • While refreshing Maven for Eclipse is simple (ALT+F5), the Maven plugin will default the Java Compiler and Compliance levels to JDK 1.5, at the time of this writing.
        • By setting this plugin, you ensure that Maven and Eclipse will not come into conflict with Compiler and Compliance levels.

Properties

  • Compiler
  • failOnMissingWebXml
  • Encoding
  • Versions



Dependency Management


Maven has two dependency sections in the POM: Dependency Management and Dependencies.

Visually, they look like this in the Eclipse POM editor:


A dependency defined under the dependency management section can be used in child modules, without a need for the version to be defined.

This can be illustrated using jUnit:
 <dependencyManagement>  
  <dependencies>  
   <dependency>  
    <groupId>junit</groupId>  
    <artifactId>junit</artifactId>  
    <version>3.8</version>  
   </dependency>  
  </dependencies>  
 </dependencyManagement>  


In the child POM, we can simply specify
  <dependencies>  
   <dependency>  
    <groupId>junit</groupId>  
    <artifactId>junit</artifactId>  
   </dependency>  
  </dependencies>  

No version number is needed.  The version can be modified in the parent POM, and all downstream dependencies will be updated automatically.



References

  1. [Stackoverflow] Dependecy vs Dependency Management Section in Maven

No comments:

Post a Comment