Dave
Dave

Reputation: 1214

Maven include parent classes

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?

Upvotes: 13

Views: 16454

Answers (4)

Roadrunner
Roadrunner

Reputation: 6821

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>

Upvotes: 20

n0rmzzz
n0rmzzz

Reputation: 3848

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.

Upvotes: 4

khmarbaise
khmarbaise

Reputation: 97557

The first thing you should do is to define your dependencies within a dependencyManagmeent block in your parent like this:

<dependencyManagement>
   <dependencies>
     <dependency>
       <groupId>group-a</groupId>
       <artifactId>artifact-a</artifactId>
       <version>1.0</version>
     </dependency>
     ...
   </dependencies>
</dependencyManagement>

and furthermore in your child you should say which dependency your child would like to use as dependency:

   <dependencies>
     <dependency>
       <groupId>group-a</groupId>
       <artifactId>artifact-a</artifactId>
       <!-- No version given. See dependencyManagement -->
     </dependency>
     ...
   </dependencies>

Upvotes: 0

MykoB
MykoB

Reputation: 184

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>

Upvotes: 2

Related Questions