Maven dependency on another local project: all ok but compiler does not see jar

935 Views Asked by At

I have two projects, let's say A and B, in the same directory dir. B is a parent project to its module C. C is dependent on A. Directory structure:

dir/
    A/
    B/
       C/

I use this method create a project with modules and to show, that C is dependent on A. After doing this, Netbeans starts to see packages of A in C, e.g. it shows no missing packages, gives hints on using classes from A in C. If I put A as a module of B, mvn clean install says that maven reactor found the correct order of compiling projects and compiles them in the order A, B, C. mvn dependency:tree shows that groupB:C:jar:0.0.1 has a child node groupA:A:jar:0.0.1:compile.

I am not sure if A can be a module of B, because A has another parent (Spring boot). Thus, I try another variant: remove A from modules of B, then mvn clean install in A, so that (I guess) the jar it produces goes to the local repo. Both variants seem to be ok for maven and Netbeans, but not for the compiler. During compilation, C does not see the packages of A.

I did mvn clean install in any of the three projects. The dependency in C must have exact properties of A or a dependency error is produced in Maven. It seems that both Netbeans' hinting mechanism and Maven think that A is a library of C. It is only the compiler who doesn't see it. I guess that I did a newbie error. What can it be?

Relevant fragments of the pom.xml involved:

A:

   <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.4.1</version>
            <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>groupA</groupId>
    <artifactId>A</artifactId>
    <packaging>jar</packaging>
    <version>0.0.1</version>
    <name>projectA</name>
    <description>project A</description>

B: (I tried variants with A being a module of B commented out or not)

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.1</version>
        <relativePath/>
</parent>
<groupId>groupB</groupId>
<artifactId>B</artifactId>
<packaging>pom</packaging>
<version>0.0.1</version>
<name>projectB</name>
<description>project B</description>

<modules>
        <!--module>../A</module-->
        <module>C</module>
</modules>

C:

<parent>
        <groupId>groupB</groupId>
        <artifactId>B</artifactId>
        <version>0.0.1</version>
        <relativePath>../</relativePath>
</parent>
<groupId>groupB</groupId>
<artifactId>C</artifactId>
        <packaging>jar</packaging>
<version>0.0.1</version>
<name>projectC</name>
<description>project C</description>
<dependencies>
        <dependency>
            <groupId>groupA</groupId>
            <artifactId>A</artifactId>
            <version>0.0.1</version>
        </dependency>
</dependencies>
1

There are 1 best solutions below

0
On

Seeing that A's jar is in the dependency tree, I searched for A's classes in that jar. It turned out, there aren't any. Instead, they are packaged separately in groupA.A.jar.original. As it turned out, it was ok to make Spring Boot a parent of A, even if A is just a library. Thanks to this, all Spring dependencies get versions automatically from the parent. However, I also included spring-boot-maven-plugin in A, not knowing what it is for. That plugin has a repackage goal which prepares a Spring Boot application. It was the cause of creating these two jars, and in effect, C not seeing classes of A. See this question for more details.

Keeping Spring Boot as a parent but removing the plugin made C see classes of A.