Maven include parent classes

inheritance, java, maven

Solution

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>

Problem

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?

Original source