1

I have 2 Spring boot maven projects(module A, Module B). And they both are added as modules to a parent project. Both Modules have some common dependencies and java classes(Domain objects), so I created third module Module C, and placed all the common java files and dependencies there.

I added the Module C to parent pom as one of the module. And added Module C as dependency to Module A and Module B. In Module A, B wherever the classes of Module C is referred there it was resolved and is pointing to Module C Classes (on ctrl+click).No error was shown in eclipse and maven dependencies are updated. But when I build the projects either from Parent pom or build Module A alone (after building the module c), I get cannot find symbol error in the places where module C classes were referenced.

Below are my pom.xml's

Parent pom.xml

<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">
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example.services</groupId>
    <artifactId>cloud-services</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <modules>
        <module>ModuleC</module> <!-- Project where common dependencies and Common java classes are placed -->
        <module>ModuleA</module>
        <module>ModuleB</module>
    </modules>
</project>

Module C Pom.xml - Common Project

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.example.services</groupId>
    <artifactId>cloud-services</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>ModuleC</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>ModuleC</name>
    
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR3</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
    </dependencyManagement>
</project>

Module A Pom.xml - Dependent Project on Module C

<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>
    <parent>
        <groupId>com.example.services</groupId>
        <artifactId>cloud-services</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>ModuleA</artifactId>
    <dependencies>
    <!-- Project where common dependencies and Common java classes are placed -->
        <dependency>
            <groupId>com.example.services</groupId>
            <artifactId>ModuleC</artifactId>
            <scope>compile</scope>
        </dependency>
    </dependencies>
</project>

Module B Pom.xml

Same as ModuleA.

Whats wrong with my configuration. Any help is much appreciated. Thanks.

Rajesh2389
  • 349
  • 2
  • 15
  • have you tried including ModuleC's version in ModuleA & B's pom.xml? – Shreya Sharma Jul 14 '20 at 10:00
  • Yeah it didn't work. – Rajesh2389 Jul 14 '20 at 10:02
  • Can you check the jar file after extraction what is the structure. If the structure has BOOT-INF as root folder it will not work – Santosh b Jul 14 '20 at 10:07
  • Which jar you want me to extract? Except for Common-project nothing was building due to build error. – Rajesh2389 Jul 14 '20 at 10:12
  • @Santosh, I'm not sure if you're asking about common-project jar. But yeah, it has Boot-INF as root folder. Is it the reason I'm getting build error? Do you think it'll work if it's not Spring-boot project? – Rajesh2389 Jul 14 '20 at 15:14
  • Yeah if its a spring project the root folder would be BOOT-INF class files wont get resolved. It will definitely work if its not a spring project – Santosh b Jul 15 '20 at 02:13
  • Thanks for the info @Santosh. I modified it to a normal maven project as spring boot is not necessary. It's working now. Any reason the classes inside boot-inf folder aren't getting resolved? Between you can make your comment as answer. – Rajesh2389 Jul 15 '20 at 06:57

2 Answers2

1

If its a spring project the root folder of the jar would be BOOT-INF.

If you create a normal project jar it wouldn't be having BOOT-INF.

The way it works is if you have a Project A and it has dependency on x.jar

Say Project A needs com.x.y.ClassA.class from x.jar it will search in root folder of x.jar with this path x/y/ClassA.class.

In your case as it was a spring project the resolution didnt work and it was complaining about that.

Santosh b
  • 729
  • 1
  • 8
  • 19
0

I guess that the spring-boot-starter-parent is the culprit.

By using it as a parent POM to your parent POM, you are building all your modules with it. AFAIK, jars build with spring-boot-starter-parent cannot be used as dependencies because the classes are moved to a different directory inside the jar.

As I am not Spring expert, I cannot really offer you a good solution for this.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
  • I added the ```spring-boot-dependencies``` as part of dependencyManagement to remove the ```spring-boot-starter-parent```. But issue still persists. – Rajesh2389 Jul 14 '20 at 13:45