masterdany88
masterdany88

Reputation: 5351

How to declare global dependency for all submodules with maven and eclipse

I have multimodule structured maven project as follow:

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

Upvotes: 1

Views: 2748

Answers (2)

Vadim
Vadim

Reputation: 4130

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>

Upvotes: 1

AJNeufeld
AJNeufeld

Reputation: 8705

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>

Upvotes: 0

Related Questions