Eclipse – Missing Maven dependencies in Eclipse multi-module project

dependencieseclipsem2emaven

I’m using STS 2.9.1 (build on Eclipse 3.7.2) with m2e plugin bundled with STS (v1.0.200.20111228-1245).
I have a problem regarding missing dependencies in Eclipse project that contains several modules, or maybe I don’t fully understand how it should work.

It’s a maven project.
In my Project > Properties > Java Build Path > Libraries I have “Maven Dependencies” library, but it's empty (and that’s the problem).
The main POM doesn’t have any dependencies, but it has several modules declared in it.
Adding a dependency to module’s POM doesn’t add it to the “Maven Dependencies” library (what was my expectation) and leads to Eclipse showing errors in source files.
Adding a dependency to the main POM adds it to the “MD” lib, but of course I don’t want to add all of my modules’ dependencies to the main POM just to have it in “MD” lib and adding every single dependency to the Build Path doesn’t seem right nor practical.

I’ve tried:

  • Project > Clean,
  • Maven > Update dependencies,
  • Maven > Update project configuration,
  • Unchecking the checkbox: Project > Properties > Maven > Resolve dependencies from Workspace projects.

None of the above seems to do the trick.

Example:
Simplified project structure:

  • simple.project
    • sample-module
      • pom.xml
    • pom.xml

simple.project/pom.xml:

<project ...>
    <modelVersion>4.0.0</modelVersion>
    <groupId>test</groupId>
    <artifactId>simple.project</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <modules>
        <module>sample-module</module>
    </modules>
    <dependencies>
        <dependency><!-- This dependency is present in "MD" lib. -->
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

simple.project/sample-module/pom.xml:

<project ...>
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <artifactId>simple.project</artifactId>
        <groupId>test</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>test</groupId>
    <artifactId>sample-module</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <dependencies>
        <dependency><!-- I've expected this dependency also to appear in "MD" lib. -->
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

Best Answer

It is not supposed to work. A project only imports a dependency from another one if it depends on that project (using dependency) or if it inherits from it (using parent). The module element only represents an aggregation.