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

7
  • Did you declare the parent in the child poms? Commented Nov 16, 2016 at 21:25
  • I've updated post-check it. thx Commented Nov 16, 2016 at 21:27
  • Hmmm. I would go through: stackoverflow.com/questions/3211643/… and if that's not an issue, try a full clean build from Eclipse. Likely a silly config issue or Eclipse acting up and needing a full clean. Commented Nov 16, 2016 at 21:29
  • Did you declare child modules dependencies in parent module pom? Commented Nov 16, 2016 at 21:36
  • You mean '<modules>' ?Yes. Commented Nov 16, 2016 at 21:39

2 Answers 2

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> 
Sign up to request clarification or add additional context in comments.

3 Comments

So what is the diference between <dependencyManagement> and <dependencies> tags? Why both exists?
<depenedncyManagement> 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 <dependencies>. 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 <plugins> and <pluginManagement> in <build>. And with plugins it is much more useful, since they may have large specific configurations.
So... "grand-parent" can have <pluginManagment> with common settings, then its children may have actual <plugin> 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.
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> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.