Adam Freymiller
Adam Freymiller

Reputation: 1939

Maven compilation error when importing from a nested sibling module

I am working with a multi-module Maven project and trying to import a dependent class from a module nested in one child of a parent to a module nested in its sibling. Here is a simplified hierarchy of the project:

pom.xml (parent)
 |--A
    |--pom.xml
    |--C
       |--pom.xml
       |--src
 |--B
    |--pom.xml
    |--D
       |--pom.xml
       |--src

Here's a class within the C module:

Dummy.java

package com.xyz.A.C;

import com.xyz.B.D.DummyDependency;

public class Dummy {
   public static int callDependency(int num) {
       return dummyDependency.absoluteVal(num);
   }
}

Here's a class within the D module:

DummyDependency.java

package com.xyz.B.D;

public class DummyDependency {
    /**
     * Return the absolute value of a number.
     * @param num an integer
     */
     public static int absoluteVal(int num) {
         if (num > 0) {
            return num;
         } else {
            return -num;
         }
     }
 }

When I run mvn clean install I get a compilation error that resembles the following:enter image description here

How do I get access to the module nested in the sibling of the child module without the compilation error?

Upvotes: 4

Views: 706

Answers (1)

Mureinik
Mureinik

Reputation: 312136

Don't rely on the build order. If a class in module D needs to use a class from module C, you should explicitly depend on it:

<dependencies>
    <dependency>
        <groupId>com.xyz</groupId>
        <artifactId>A.C</artifactId>
        <version>${project.version}</version>
    </dependency>
</dependencies>

Upvotes: 3

Related Questions