Search code examples
springspring-bootmavenmaven-dependency

Maven - Add a maven module as dependency to other maven module


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.


Solution

  • 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.