This post in the 3rd one of a series on maven3. You can find the information related to the demo project used in example here.

One of the painful thing with all build management system, is the maintenance.

Setting it up at the beginning is ok, but maintaining it over time take time. Ensuring that all tools run on all modules, with same version, that the experiment you made on one module, or on a subset of module do not brake the whole things ….

So, in this post will see some tips to avoid information duplication in order to ease maintenance.

1: use pomParent mechanism to share most of the configuration

The pom parent mechanism is an “inclusion like” mechanism. It’s well defined here.
In the demo project, you have it located in the PomParent directory, and it’s used in all the others pom files.
In the following pomParent I am :

  • defining common properties (java version, encoding, plugin version) use in all pom files
  • defining plugin management element for all plugin used in one of the pom, in order to have plugin version centralized in one location
  • defining developer, license, organisation in one location

PomParent content :

   <project>
<modelVersion>4.0.0</modelVersion>

<!– The Basics –>
<groupId>com.persistentsas.mavenTraining.PomParent</groupId>
<artifactId>parent</artifactId>
<version>1.9-SNAPSHOT</version>

<packaging>pom</packaging>
<properties>
<!– build mechanism property –>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!– Plugin having a custom setting –>
<maven.pmd.plugin>2.7.1</maven.pmd.plugin>
<pmd.java.version>1.7</pmd.java.version>
….
</properties>
<dependencyManagement>
…..
</dependencyManagement>
<modules></modules>
<!– Build Settings –>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>${maven.resources.plugin.encoding}</encoding>
</configuration>
</plugin>
……
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>${maven.release.plugin}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>${maven.site.plugin}</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>${maven.findbugs.plugin}</version>
</plugin>
…………….
</plugins>
</pluginManagement>
</build>
<reporting></reporting>

<!– More Project Information –>
<name>parent</name>
<description></description>
<url></url>
<inceptionYear></inceptionYear>
<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url&gt;
<distribution>repo</distribution>
<comments>A business-friendly OSS license</comments>
</license>
</licenses>
<organization>
<name>PersistentSAS</name>
<url>http://www.persistentsas.fr</url&gt;
</organization>
<developers>
<developer>
<id>LaurentT</id>
<name>Laurent Tardif</name>
<email>Laurent.Tardif at persistentsas.com</email>
<url>http://www.persistentsas.fr</url&gt;
<organization>PersistentSAS</organization>
<organizationUrl>http://www.persistentsas.fr</organizationUrl&gt;
<roles>
<role>architect</role>
<role>developer</role>
</roles>
<timezone>+1</timezone>
<properties>
<picUrl>http://www.persistentsas.fr</picUrl&gt;
</properties>
</developer>
</developers>
<contributors></contributors>

<!– Environment Settings –>
<issueManagement></issueManagement>
<ciManagement></ciManagement>
<mailingLists></mailingLists>
<scm></scm>
<prerequisites></prerequisites>
<repositories></repositories>
<pluginRepositories></pluginRepositories>
<!– not inherited –>
<distributionManagement>
<site>
<id>${project.artifactId}-site</id>
<url>file:///c:/temp/site/${project.artifactId}/${project.artifactId}.${project.packaging}</url>
</site>
<repository>
<id>GrenobleWrite</id>
<name>artifatoryWrite</name>
<url>http://MyBinaryRepo:8081/artifactory/libs-release-local/</url&gt;
</repository>
<snapshotRepository>
<id>GrenobleWrite</id>
<url>http://MyBinaryRepo:8081/artifactory/libs-snapshot-local/</url&gt;
</snapshotRepository>
</distributionManagement>
<profiles></profiles>
</project>

Inclusion in other pom :

<parent>
        <groupId>com.persistentsas.mavenTraining.PomParent</groupId>
        <artifactId>parent</artifactId>
        <version>1.9-SNAPSHOT</version>
<!– hint path, not mandatory –>
        <relativePath>.\PomParent\parent.pom</relativePath>
    </parent>

2: use properties to centralize in a common place important informations.

This is the first advise I’ll give. Use properties to put in a common place, informations your need to share.

you can define as many properties you want, and reuse them in any part of the pom file. If you define them in a PomParent, you can reuse them in all pom files. That’s help having easy to maintain project.

Among the practices i liked (but not every one agreed :-), I’m agree also to say that in the example, it’s a bit too much :-), but it’s an exercise … ), is to duplicate variable definition, in order to know where it’s used. That make also my life easier when the value need to be changed locally.

<properties>
<!– build mechanism property –>

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>${project.build.sourceEncoding}</project.reporting.outputEncoding>
        <maven.resources.plugin.encoding>${project.build.sourceEncoding}</maven.resources.plugin.encoding>
        <maven.compiler.plugin.encoding>${project.build.sourceEncoding}</maven.compiler.plugin.encoding>

        <maven.deploy.plugin>2.8.2</maven.deploy.plugin>
        <maven.checkstyle.plugin>2.7</maven.checkstyle.plugin>
….
</properties>

 3) define common build step in the pom Parent

In the pom parent you can define common build section in order to centralized that.

Now Imaging you have 4 modules, let’s say two standard jar libraries, and two webservices with code generation, and so on….

The build steps are not common to the 4 modules, but in this case you can make 3 pomParent :

1) first one common to every one (with dependencies version, common variables, … )

2) Second one, including first one, and defining build step for jar

3) 3rd one , including first one, and defining , source code generation, and so on for webservices.

Advertisements