0

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

3
  • 1
    First upgrade your Maven version (3.9.x) second you should not inherit the spring-boot-maven-plugin only the module which really creates spring boot app.. that's also the reason why the usage does not and the issue occurs... I have made a PR to your GitHub project...
    – khmarbaise
    Commented Jul 8 at 7:10
  • I have one more query is it possible to have just one main function in the parent and other modules have just there controller,services etc. and I should be able to build different combinations: 1)package all modules at once, 2)Main + common + service1 3)Main + common + service2 Or any other combination. No matter which module i choose it should give me one .jar file cause the whole project will have just one main function.
    – Muneer
    Commented Jul 9 at 1:06
  • You should create an other main (lets say main2) which comines the needed parts ... so in the end you can just build from the top and build two services in one go...
    – khmarbaise
    Commented Jul 9 at 15:09

1 Answer 1

0

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

3
  • 1
    No I haven't said so.. You should not use spring-boot-maven-plugin in a module which should be reused as an artifact... You have created separate modules which are not part of a reactor build... because the modules use the spring-boot-starter-parent... see my PR on the referenced github.com/Muneeer/testapp/pull/1
    – khmarbaise
    Commented Jul 8 at 15:33
  • 1
    @khmarbaise makes sense, thanks for that correction, looked at PR, nice!
    – akang
    Commented Jul 8 at 16:52
  • I have one more query is it possible to have just one main function in the parent and other modules have just there controller,services etc. and I should be able to build different combinations: 1)package all modules at once, 2)Main + common + service1 3)Main + common + service2 Or any other combination. No matter which module i choose it should give me one .jar file cause the whole project will have just one main function.
    – Muneer
    Commented Jul 9 at 1:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.