Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 903
Dave Avatar asked Apr 05 '12 07:04

Dave


3 Answers

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.

like image 74
n0rm1e Avatar answered Nov 19 '22 10:11

n0rm1e


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>
like image 37
Roadrunner Avatar answered Nov 19 '22 10:11

Roadrunner


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>
like image 36
MykoB Avatar answered Nov 19 '22 10:11

MykoB