Multi Module Inheritance

The previous tutorials explained the structure of simple and hierarchical multi module project. This tutorial explain the use of inheritance feature to compact and streamline the module POMs.

The example code extended-multi, used in this tutorial, is available at GitHub Maven Examples. Download the examples as zip and extract it to some location or clone it with git.

Most elements from the parent POM are inherited by its children. Out of these, properties and dependencies comes handy in multi module projects. Let’s see how we can use them.

In extend-multi project, we set sourceEncoding and compiler source and target levels in multiple POM.

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
</properties>

Edit app/pom.xml, shared/util/pom.xml and shared/config/pom.xml and remove the properties block.

Similarly, junit dependency is specified in multiple locations

<dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
</dependency>

Edit app/pom.xml, shared/util/pom.xml and shared/config/pom.xml and remove the junit dependency.

 
 

Now execute mvn test from the top level folder and as expected, Maven build fails because of missing junit library. It also displays a warning that build is platform dependent as sourceEncoding is not set.

<a href="https://maven.apache.org/pom.html#Inheritance" target="_blank">WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!

To overcome these errors, modify top-level POM i.e. extended-multi/pom.xml and add properties and junit dependency to it. The file content after modification is as follows

extended-multi/pom.xml

<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>org.codetab</groupId>
    <artifactId>extended-multi</artifactId>
    <version>1.0</version>
    <packaging>pom</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>

    <modules>
        <module>app</module>
        <module>shared</module>
    </modules>

</project>

Now, execute mvn test and project builds successfully without any warning.

 
 

To sum up, we removed properties and junit dependency from all the module pom.xml and moved it to the top level pom.xml. Maven inheritance feature ensures that all child pom.xml inherits these items from the parent.

However, be careful while adding the dependencies to the top-level pom.xml as they are inherited by all child POM. Add only those dependencies which are required by each and every module. Good examples of such libraries are JUnit, Log4j or Sl4j etc., and these are normally required by all modules.

When a dependency is required by some, but not by all the modules, then use Dependency Management feature which is explained in the next chapter.

To know more about inheritable and non inheritable elements refer POM Reference - Inheritance