Maven POM Templates

Maven uses POM (Project Object Model) files to define its build and deploy tasks. You can end up with a lot of POM files (one for every app or api for instance) so I encourage the use of parent POMs, because they support inheritance and will end up saving a lot of typing and replication.
Here are a couple of examples of an application POM and its parent POM:

First, the app POM:

<?xml version="1.0" encoding="UTF-8"?>
<modelVersion>4.0.0</modelVersion>
<groupId>...</groupId> <!-- e.g. org.apache.maven -->
<artifactId>APPNAME</artifactId>
<packaging>PACKAGETYPE</packaging> <!-- e.g. jar -->
<version>...</version> <!-- e.g. 1.0.0.0-SNAPSHOT -->
<name>APPNAME</name>

<description>Description about this application</description>

<!-- The properties can be added to for each individual pom, 
this is just an example. You can add your own properties here -->
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <org.springframework.version>3.0.2.RELEASE</org.springframework.version>
    <org.slf4j.version>1.6.0</org.slf4j.version>
  </properties>

<!-- list the parent pom here - if there is one -->
<parent>
  <groupId>...</groupId><!-- Group ID of Parent POM -->
  <artifactId>...</artifactId><!-- Name of parent pom file -->
  <version>1.0.0.0</version>
</parent>

<developers>
  <developer>
    <id>1</id>
    <name>NAME</name>
    <email>EMAIL@EMAIL.COM</email>
    <organization>MYCOMPANY</organization>
    <organizationUrl>http://www.mycompany.com</organizationUrl>
  </developer>
</developers>

<profiles>
<!-- This section is optional  -->
</profiles>

<dependencies>
<!-- List all app specific dependencies here -->
<!-- use RELEASE as the version if you always want it to use the latest version, but note that this is NOT supported in Maven 3 -->
  <dependency>
    <groupId>BLA.BLA.BLA</groupId>
    <artifactId>foo-api</artifactId>
    <version>RELEASE</version>
  </dependency>
</dependencies>

<build>
  <plugins>

<!-- you might need to enter some specific plugins here, however it will be better to have them in parent pom-->

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-ejb-plugin</artifactId>
      <version>2.1</version>
      <configuration>
        <ejbVersion>3.0</ejbVersion>
      </configuration>
    </plugin>
  </plugins>
</build>

</project>

 

And now for the parent POM:

<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>...</groupId>
<artifactId>parentpom</artifactId>
<packaging>pom</packaging>
<name>Master POM for apps</name>
<version>1.0.0.0</version>

<description>
This is parent pom for apps, where all the generic configurations are defined
</description>

<ciManagement>
  <system>Bamboo</system>
  <url>http://mybuildserver</url>
  <notifiers>
    <notifier>
      <address>admin@jamesbetteley.com</address>
    </notifier>
  </notifiers>
</ciManagement>

<licenses>
  <license>
  <name>myname</name>
  <url>http://www.myname/licenses/LICENSE.txt</url>
  <distribution>http://mybuildserver:8080/my-repo/</distribution>
  </license>
</licenses>

<organization>
  <name>MyCompany</name>
  <url>http://www.mycompany.com</url>
</organization>

<issueManagement>
  <system>GForge</system> <!-- list your issue management system here -->
  <url>http://My.gforge.url/</url>
</issueManagement>

<developers>
  <developer>
    <id>1</id>
    <name>James Betteley</name>
    <email>james.betteley@jamesbetteley.com</email>
    <organization>mycompany</organization>
    <organizationUrl>http://www.mycompany.com</organizationUrl>
  </developer>
</developers>

<scm> <!-- your source control information -->
  <connection>scm:svn:http://127.0.0.1/svn/my-project</connection>
  <developerConnection>scm:svn:https://127.0.0.1/svn/my-project</developerConnection>
  <tag>HEAD</tag>
  <url>http://127.0.0.1/websvn/my-project</url>
</scm>

<properties> <!-- You can define your own properties here, like this one -->
  <halt.on.fail>false</halt.on.fail>
</properties>

<!--	Build Configuration and Plugin Definition-->

<build>
  <finalName>${artifactId}-${version}</finalName>
  <outputDirectory>build/maven/${artifactId}/target/classes</outputDirectory>
  <testOutputDirectory>build/maven/${artifactId}/target/test-classes</testOutputDirectory>
  <directory>build/maven/${artifactId}/target</directory>
  <resources>
    <resource>
    <directory>src/main/java</directory>
      <includes>
        <include>**/*.xml</include>
      </includes>
    </resource>
    <resource>
    <directory>src/main/resources</directory>
    </resource>
</resources>
  <testResources>
    <testResource>
    <directory>src/test/java</directory>
    <includes>
      <include>**/*.xml</include>
      <include>**/*.properties</include>
    </includes>
    </testResource>
    <testResource>
    <directory>src/test/resources</directory>
    </testResource>
  </testResources>
<extensions>
  <extension>
    <groupId>org.apache.maven.wagon</groupId>
    <artifactId>wagon-ssh-external</artifactId>
    <version>1.0-alpha-5</version>
  </extension>
</extensions>

<pluginManagement>
<!-- List all the optional plugins here, for example, 
the war plugin. Child POMs can override these. -->
<!-- For a child to inherit these, 
it must reference them from within their plugin section -->
  <plugins>
    <plugin>
      <artifactId>maven-war-plugin</artifactId>
      <executions>
        <execution>
        <phase>package</phase>
        <goals>
          <goal>war</goal>
        </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</pluginManagement>

<plugins> <!-- define all plugins that are common to your app poms. 
All of these will be inherited by the child poms -->
  <plugin>
  <artifactId>maven-compiler-plugin</artifactId>
  <configuration>
    <source>1.6</source>
    <target>1.6</target>
  </configuration>
  </plugin>
  <plugin>
  <artifactId>another-plugin</artifactId>
  <configuration>
    <source>etc</source>
    <target>etc</target>
  </configuration>
  </plugin>
</plugins>
</build>

<repositories>
  <repository>
    <snapshots>
      <enabled>false</enabled>
    </snapshots>
    <id>Central</id>
      <url>http://repo1.maven.org/maven2</url>
  </repository>
  <repository>
    <snapshots>
      <enabled>false</enabled>
    </snapshots>
    <id>3rd-Party-Repository</id>
      <url>http://mybuildserver:8080/maven-repo</url>
  </repository>
  <repository>
    <snapshots>
      <enabled>false</enabled>
    </snapshots>
    <id>my-Repository</id>
      <url>http://mybuildserver:8080/my-repo</url>
  </repository>
  <repository>
    <snapshots>
      <enabled>true</enabled>
    </snapshots>
    <id>my-Snapshot-Repository</id>
      <url>http://mybuildserver:8080/my-snap</url>
  </repository>
</repositories>



<distributionManagement>
  <repository>
  <id>my-Repository</id>
    <url>sftp://mybuildserver/home/maven/my-repo</url>
  </repository>
  <snapshotRepository>
  <id>my-Snapshot-Repository</id>
    <url>sftp://mybuildserver/home/maven/my-snap</url>
  </snapshotRepository>
  <site>
  <id>my-Reports-Repository</id>
    <url>sftp://mybuildserver/home/maven/maven-sites/${groupId}</url>
  </site>
</distributionManagement>

<reporting> <!-- List your reporting plugins here, such as cobertura	-->
<outputDirectory>build/maven/${artifactId}/target/site</outputDirectory>
<plugins>

  <plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>cobertura-maven-plugin</artifactId>
  <version>2.4</version>
    <configuration>
      <formats>
        <format>html</format>
        <format>xml</format>
      </formats>
    </configuration>
  </plugin>
</plugins>
</reporting>

</project>


 

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s