Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import class from transitive dependency

I have the following dependencies: A, B and X. "->" means depends on.

A -> B
X -> A

B has the following public Class:

public class PublicClassB {
     public static void do() {
     }
}

PublicClassB is used by X and A. E.

The pom for class A is the following:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>my.company.name</groupId>
    <artifactId>A</artifactId>
    <version>1.0.0</version>
    <dependencies>
          <dependency>
              <groupId>my.company.name</groupId>
              <artifactId>B</artifactId>
              <version>1.0.0</version>
          </dependency>
    </dependencies>
</project>

The pom for X is the following:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>my.company.name</groupId>
    <artifactId>X</artifactId>
    <version>1.0.0</version>
    <dependencies>
          <dependency>
              <groupId>my.company.name</groupId>
              <artifactId>A</artifactId>
              <version>1.0.0</version>
          </dependency>
    </dependencies>
</project>

Is there a way to access PublicClassB#do() from X?. I can't make the import work, Eclipse doesn't detect the package on B.

like image 807
dantebarba Avatar asked Aug 10 '26 05:08

dantebarba


1 Answers

First of all, as mentioned in the comments, you need a version tag or get the version from some dependency management. Secondly, m2e (the Eclipse plugin) does not always correctly determine dependencies. Try to build with Maven (run as -> Maven build) and see if you get any errors.

Thirdly: If you use a class from B, you should also list B as a dependency. To rely on B being drawn indirectly is dangerous (A might change its dependencies in the future) and also hides your "real" dependencies.

like image 172
J Fabian Meier Avatar answered Aug 12 '26 20:08

J Fabian Meier