Maven include parent classes

13,255

Solution 1

I'd change the structure of your project like this:

  • parent (pom)
    • core (jar, with all the classes that used to be in parent)
    • child (war, depends on core)

Parent:

<project>
  <groupId>my.group</groupId>
  <artifactId>parent</artifactId>
  <version>0.0.1</version>
  <packaging>pom</packaging>

  <modules>
    <module>core</module>
    <module>child</module>
    <!-- possibly more modules... -->
  </modules>
</project>

Core:

<project>
  <parent>
    <groupId>my.group</groupId>
    <artifactId>parent</artifactId>
    <version>0.0.1</version>
  </parent>
  <artifactId>core</artifactId>
  <packaging>jar</packaging>
</project>

Child:

<project>
  <parent>
    <groupId>my.group</groupId>
    <artifactId>parent</artifactId>
    <version>0.0.1</version>
  </parent>
  <artifactId>module1</artifactId>
  <packaging>war</packaging>

  <dependencies>
    <dependency>
      <groupId>my.group</groupId>
      <artifactId>core</artifactId>
      <version>${project.version}</version>
    </dependency>
  </dependencies>
</project>

Solution 2

Since your parent module's packaging is "pom", no jar file will be provided and no .class file from that will be accessible to other modules. Try changing it to "jar" and give it another try.

Although it's not a good practice to have a parent as "jar". I'd stick to the "pom" parent and probably create a new "core" or "common" child and make my "war" depend on it.

Solution 3

Try to add an internal dependency in child module pom, so it is familiar with its parent

<dependency>
   <groupId>my.group</groupId>
   <artifactId>parent</artifactId>
   <version>0.0.1</version>
</dependency>
Share:
13,255
Dave
Author by

Dave

Updated on June 04, 2022

Comments

  • Dave
    Dave about 2 years

    I have a fairly simple maven-ized Java project, but am having trouble getting my head around it.

    My parent module defines a lot of Java classes (and dependencies) that I expect to be useful for several child modules. One of the child modules is dedicated to deploying a web app, so it needs a few extra classes (the servlets) plus everything from the parent module.

    The file structure looks like this

     - parent
       - src
       - pom.xml
       - child
         - src
         - pom.xml
    

    My parent pom looks like this:

    <project>
      <groupId>my.group</groupId>
      <artifactId>parent</artifactId>
      <version>0.0.1</version>
      <packaging>pom</packaging>
    
      ...
    
      <modules>
        <module>child</module>
      </modules>
    </project> 
    

    And the child looks like this:

    <project>
      <artifactId>child</artifactId>
      <packaging>war</packaging>
      <parent>
        <artifactId>parent</artifactId>
        <groupId>my.group</groupId>
        <version>0.0.1</version>
      </parent>
    
      ...
    
    </project>
    

    Is this all I need to have the child know about the classes and dependencies defined in parent? It doesn't seem to be: eclipse gives compile errors, and running mvn clean package from parent folder or child folder results "cannot find symbol" messages any time a class from parent is mentioned.

    What am I doing wrong?