maven - multi-module project access parent source code & avoid redundant dependency declaration

1.2k Views Asked by At

So, I have project I am trying to split up into multi-modules where each module has its own pom.xml file & thus, builds as a separate artifact. At the same time, I also have a single parent pom file outside each of these modules that is the <parent> of each module's pom file.

I want each of my modules to pull in all of the dependencies from my parent pom file without having to redefine them in their own <dependencies> section. And at the same time, I want them to be able to have access to the classes that are in the general parent module.

For instance, take the below project directory structure. In this example, I want both child-module-1 & child-module-2 to include all of the dependencies defined in the outer parent pom (last line) & also have access to MyParent.java.

  parent-project
      child-module-1
          src
              main
                  MyChild1.java
              test
                  MyChild1Test.java
          pom.xml

      child-module-2
          src
              main
                  MyChild2.java
              test
                  MyChild2Test.java
          pom.xml

      src
          main
              MyParent.java
          test
              MyParentTest.java
      pom.xml

Is this possible ? I could live with having to redefine the dependencies (without versions) in the child poms even though it would be redundant but, having access to the classes outside the child modules is a must.

2

There are 2 best solutions below

3
On

I'm can't believe i'm this dumb. All I had to do was include the parent as a dependency (with version) in each child pom to gain access to all files & dependencies.

<dependency>
    <groupId>my.org.path</groupId>
    <artifactId>parent-project</artifactId>
    <version>1.0.0</version>
</dependency>
1
On

Parent projects do not contain code.

Parent projects are of packaging pom. If you need common code, create a common module for it and use it as dependency.