Understanding project inheritance

There are times when you might want a project to use values from another .pom file. You may be building a large software product, so you do not want to repeat the dependency and other elements multiple times.

Maven provides a feature called project inheritance for this. Maven allows a number of elements specified in the parent pom file to be merged to the inheriting project. In fact, the super pom file is an example of project inheritance.

Getting ready

Maven is set up on your system and is verified to work. To do this, refer to Chapter 1, Getting Started.

How to do it...

  1. Open a project that has inheritance; project-with-inheritance in our case. This has a subfolder named child, which is the project that inherits from the parent.
  2. Update the parent pom file as follows:
        <groupId>com.packt.cookbook</groupId>
        <artifactId>project-with-inheritance</artifactId>
        <packaging>pom</packaging>
        <version>1.0-SNAPSHOT</version>
  3. Create the pom file for child as follows:
      <parent>
          <groupId>com.packt.cookbook</groupId>
          <artifactId>project-with-inheritance</artifactId>
          <version>1.0-SNAPSHOT</version>
      </parent>
      <modelVersion>4.0.0</modelVersion>
      <artifactId>child</artifactId>
      <packaging>jar</packaging>
      <name>Child Project</name>
  4. Run the following Maven command in the child subfolder:
    mvn clean package
    
  5. Observe the output:
    How to do it...

How it works...

We specified a parent element in the pom file of child. Here, we added the coordinates of the parent, namely groupId, artifactId, and version. We did not specify the groupId and version coordinates of the child project. We also did not specify any properties and dependencies.

In the parent pom file, we specified properties and dependencies.

Due to the relationship defined, when Maven runs on the child project, it inherits groupId, version, properties, and dependencies defined in the parent.

Interestingly, the parent pom file (project-with-inheritance) is oblivious to the fact that there is a child project.

However, this only works if the parent project is of the pom type.

How did Maven know where the parent pom is located? We did not specify a location in the pom file. This is because, by default, Maven looks for the parent pom in the parent folder of child. Otherwise, it attempts to download the parent pom from the repository.

There's more...

What if the parent pom is not in any repository? Also, what if it is in a different folder from the parent folder of the child? Let's see what happens:

  1. Open a child project, where the parent project is not in the parent folder but in a subfolder (in our case, parent):
    There's more...
  2. Update the pom file of the child project as follows:
      <parent>
          <groupId>com.packt.cookbook</groupId>
          <artifactId>parent</artifactId>
          <version>1.0-SNAPSHOT</version>
          <relativePath>../parent/pom.xml</relativePath>
     </parent> 
  3. Build the child project:
    mvn clean package
    

Maven now determines the location of the parent pom by virtue of the relativePath element, which indicates the folder where the parent pom is located. Using this, it builds the child project successfully.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.17.128