1

I have multimodule structured maven project as follow:

  • parent
    • server
    • shared
    • client

For me it is obvious that I would write unit test for all of those projects. So I thought that will be enought to add dependency to parent's pom.xml as follow:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>

and then in each submodule use it, f.e.:

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

but I am getting errors in Eclipse like: The import org.junit cannot be resolved

When I do Maven-> Update project, or mvn clean install, there are no errors at all.

I refer to the parent module in a children one as follow:

<parent>
    <groupId>pl.daniel.erp</groupId>
    <artifactId>erp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>

Please help

masterdany88
  • 5,041
  • 11
  • 58
  • 132

2 Answers2

1

AJNeufeld:

"but you must still specify the dependencies in the child modules"

It is correct only if dependency is in <dependencyManagement>. Just try to set <dependency> in parent <dependencies> and do not set same dependency in child at all.

Parent POM:

<project>
 ...
 <dependencies>
   ...
   <dependency>
     <groupId>junit</groupId>
     <artifactId>junit</artifactId>
     <version>4.12</version>
     <scope>test</scope>
   </dependency>
   ...
 <dependencies>
 ...
</project>

Child POM:

<project>
  ...
  <!-- no junit dependency defined at all -->
  ...
</project>
Vadim
  • 4,027
  • 2
  • 10
  • 26
  • So what is the diference between `` and `` tags? Why both exists? – masterdany88 Nov 29 '16 at 19:43
  • is kind of "abstract" declaration (same idea as Abstract class :) Anywhere In parent projects tree it can declare common settings (i.e. version, scope etc.), but dependency will be added into classpath only when it is defined in . When it is in dependencyManagement, actual dependency needs to have only groupId/artifactId. So there are a lot of room for hierarchical settings... Same is correct for and in . And with plugins it is much more useful, since they may have large specific configurations. – Vadim Nov 29 '16 at 22:59
  • So... "grand-parent" can have with common settings, then its children may have actual with specific settings and all their "children" do not need to define plugin at all... Something like that. Actually in reality I have more than 100 such projects. And all dependency versions and plugins are defined in only one place. It is easy to manage with version upgrades. – Vadim Nov 29 '16 at 23:06
0

You can configure dependencies in the parent, but you must still specify the dependencies in the child modules. This allows you to specify the version once in the parent POM, and have all child modules use that version. Each child module must still list its dependencies, though.

Parent POM:

<project>
   ...
   <dependencyManagement>
       <dependencies>
           <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>4.12</version>
              <scope>test</scope>
          </dependency>
         ...
       <dependencies>
   </dependencyManagement>
   ...
</project>

Child POMs:

<project>
   ...
   <dependencies>
       <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
       </dependency>
      ...
   </dependencies>
   ...
</project>
AJNeufeld
  • 8,526
  • 1
  • 25
  • 44