« - »

Pulling it all together: the Maven parent project

19 May 2008

It’s always good to make things work. It’s even better when you make things work better, faster, or cheaper. It was good to finally get my Maven-generated .ear file to run successfully on WebSphere, but it was quite the pain to go through all of the steps to rebuild it each time I made change, particularly when the change was to one of the lower-level dependent projects such as one of the .jar files. Surely there had to be a better way than to have to build each project independently, one project at a time.

Fortunately, there is a better way: the Maven multi-modules project. To implement this solution, I created one more project, example-parent, with the following pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.restafarian</groupId>
  <artifactId>example-parent</artifactId>
  <packaging>pom</packaging>
  <name>example-parent</name>
  <version>9</version>
  <description>example-parent</description>

  <modules>
    <module>../approval</module>
    <module>../authorization</module>
    <module>../core</module>
    <module>../approval-web</module>
    <module>../authorization-web</module>
    <module>../core-web</module>
    <module>../example-web</module>
    <module>../identity-web</module>
    <module>../example</module>
  </modules>

  <developers>
    <developer>
      <id>restamon</id>
      <name>The Restamon</name>
      <email>restamon@restafarian.org</email>
      <roles>
        <role>Developer</role>
      </roles>
      <organization>Restafarian.org</organization>
    </developer>
  </developers>

  <scm>
    <connection>
      scm:svn:http://svn2.assembla.com/svn/restafarian/example-parent/
    </connection>
    <developerConnection>
      scm:svn:http://svn2.assembla.com/svn/restafarian/example-parent/
    </developerConnection>
    <url>http://trac2.assembla.com/restafarian/browser/example-parent/</url>
  </scm>

</project>

Once the parent project was created, I had to go back into the pom.xml file for every one of the listed modules and add the following lines:

  <parent>
    <groupId>org.restafarian</groupId>
    <artifactId>example-parent</artifactId>
    <version>9</version>
    <relativePath>../example-parent/pom.xml</relativePath>
  </parent>

After all of the child projects were updated, all I needed to do was right-click on the parent project and select Run as -> Maven whatever to execute whatever Maven target that I wanted to run against the entire lot. For example, Run as -> Maven clean runs the clean process on all projects in the group and Run as -> Maven install does the same with the install process. And based on the dependencies specified in the individual projects, everything is executed in the proper sequence so that dependent projects are built first before those projects that require them.

So, now we have one more project committed to the repository:

http://trac2.assembla.com/restafarian/browser/example-parent/trunk

Yes, it’s one extra project that produces no useful artifacts, but it sure makes building things a whole lot simpler. I think it’s quite an improvement, actually.


http://blog.restafarian.org/2008/05/pulling-it-all-together-the-maven-parent-project/

Comments are closed.

Sorry, the comment form is closed at this time.