Muneer
Muneer

Reputation: 29

Issues with Maven Multi-Module Project: Dependency Not Found

I am working on a Maven multi-module project and I'm having issues using a common package in two of my modules. Here is the project structure:

enter image description here

Goal: I want to use the common package in both image-gen and module2 modules. Additionally, I would like to:

  1. Create one JAR file for all the modules combined, for example:

    ./mvnw package  #This should create one JAR file containing all modules (pseudocode)
    
  2. Create individual JAR files for each module as needed, for example:

    ./mvnw clean package -pl image-gen  # This should create a JAR file for the image-gen module
    

Current Setup: Parent pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.3.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>testapp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <modules>
        <module>common</module>
        <module>image-gen</module>
        <module>module2</module>
    </modules>
    <name>testapp</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

pom.xml for image-gen and module2:

<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://www.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.example</groupId>
        <artifactId>testapp</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>image-gen</artifactId> <!-- or module2 -->
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

Issue: When I run ./mvnw clean package, the build fails with the following error message:

[INFO] common 1.0-SNAPSHOT ................................ SUCCESS [  2.105 s]
[INFO] image-gen 1.0-SNAPSHOT ............................. FAILURE [  0.582 s]
[INFO] module2 1.0-SNAPSHOT ............................... SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.658 s
[INFO] Finished at: 2024-07-08T10:27:28+09:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.13.0:compile (default-compile) on project image-gen: Compilation failure
[ERROR] /C:/Projects/testapp/image-gen/src/main/java/com/example/imagegen/service/ImgGenService.java:[5,33] package com.example.common.entity does not exist
[ERROR]
[ERROR] -> [Help 1]

Desired Outcome:

  1. Be able to build all modules and use the common module in image-gen and module2.

  2. Be able to create one combined JAR file for all modules, for example:

    ./mvnw package

  3. Be able to create individual JAR files for each module, for example:

    ./mvnw clean package -pl image-gen

You can reproduce the same result by cloning this repository and running:

git clone https://github.com/Muneeer/testapp.git
cd testapp
./mvnw clean package

Any help or guidance on resolving this issue would be greatly appreciated!

Additional Information:

Java version: 17

Maven version: 3.8.1

Spring Boot version: 3.3.1

Upvotes: 0

Views: 121

Answers (1)

akang
akang

Reputation: 94

Spring boot submodules should point to spring-boot-starter-parent and build with spring-boot-maven-plugin. Parent pom.xml doesn't need those. Also will need to use also make flag when building individual sub apps that depend on common module

./mvnw clean package -pl image-gen -am

Created example of multi-module with common lib: https://github.com/akang1108/so-78718963

Upvotes: 0

Related Questions