javaspringspring-bootmavenmulti-module

Package does not exist [Maven Multi-module Spring Boot]


I downloaded a Spring Boot project on Spring Initializer, got version 3.4.2, and installed Maven. I planned to build a POC to add some shared classes to the Shared module and reuse them in other modules. However, I encountered an error that made me mad.

I do have a spring project with this structure

    root/
    ├── pom.xml
    ├── shared/
    │   └── pom.xml
    ├── app1/
    │   └── pom.xml
    ├── app2/
    │   └── pom.xml
    └── app3/
        └── pom.xml

My root POM is like this:

<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.4.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <packaging>pom</packaging>
    <groupId>br.com.root</groupId>
    <artifactId>root</artifactId>
    <version>0.0.1</version>
    <name>root</name>
    <description>Root</description>

    <modules>
        <module>shared</module>
        <module>app1</module>
        <module>app2</module>
        <module>app3</module>
    </modules>

    <properties>
        <java.version>21</java.version>
        <spring.boot.version>3.4.2</spring.boot.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring.boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

My shared POM is like this:

<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>br.com.root</groupId>
        <artifactId>root</artifactId>
        <version>0.0.1</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <groupId>br.com.api</groupId>
    <artifactId>shared</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>shared</name>
    <description>Shared App</description>

    <properties>
        <java.version>21</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

My App1, App2, and App3 POMs are like this:

<?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>br.com.root</groupId>
        <artifactId>root</artifactId>
        <version>0.0.1</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <groupId>br.com.api</groupId>
    <artifactId>app1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>app1</name>
    <description>App 1</description>
    <properties>
        <java.version>21</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>br.com.api</groupId>
            <artifactId>shared</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.10.5</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
            <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>

I already tried a bunch of things I've found in StackOverflow or ChatGpt, but I always get the same error message when I run mvn clean install -DskipTests -U or only mvn clean install -pl shared,app1 -DskipTests -U to be more accurate.


[INFO] shared ............................................. SUCCESS [  1.787 s]
[INFO] app1 ............................................... FAILURE [  0.692 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.786 s
[INFO] Finished at: 2025-01-25T17:47:29-03:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.13.0:compile (default-compile) on project app1: Compilation failure
[ERROR] /C:/Users/MyUser/dev/spring boot/multi-module-app/app1/src/main/java/br/com/api/app1/App1Application.java:[3,32] package br.com.api.shared.domain does not exist

Do you have some suggestion about the reason it's happening?


Solution

  • You don't need spring-boot-maven-plugin in shared module.