Search code examples
javamavenbuildmulti-module

Spring Boot Multi module project - Build issues due to compilation issues for imports from different module


We have a multi-module Spring boot project starting from scratch, the hierarchy is like this

[INFO] Reactor Build Order:
[INFO] 
[INFO] hh                                                                 [pom]
[INFO] common-service                                                     [jar]
[INFO] account-service                                                    [jar]
[INFO] product-service                                                    [jar]
[INFO] auth-service                                                       [jar]
[INFO] application-service                                                [war]

This is the pom.xml of the account-service.

<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.hh.sukku</groupId>
        <artifactId>hh</artifactId>
        <version>1.0.0</version>
    </parent>

    <artifactId>account-service</artifactId>
    <name>account-service</name>
    <packaging>jar</packaging>
    
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
    </properties>

    <dependencies>
    
        <!-- Project module dependencies start -->
        
        <dependency>
            <groupId>com.hh.sukku</groupId>
            <artifactId>common-service</artifactId>
            <version>${project.version}</version>
        </dependency>
        
        <!-- Project module dependencies end -->
    
    </dependencies>

</project>

This is the parent module 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>2.3.4.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.hh.sukku</groupId>
    <artifactId>hh</artifactId>
    <version>1.0.0</version>
    <name>hh</name>
    <packaging>pom</packaging>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
    </properties>

    <modules>
        <module>application-service</module>
        <module>common-service</module>
        <module>account-service</module>
        <module>product-service</module>
        <module>auth-service</module>
    </modules>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>com.hazelcast</groupId>
            <artifactId>hazelcast</artifactId>
        </dependency>
        <dependency>
            <groupId>com.hazelcast</groupId>
            <artifactId>hazelcast-spring</artifactId>
        </dependency>

        <dependency>
            <groupId>org.modelmapper</groupId>
            <artifactId>modelmapper</artifactId>
            <version>2.3.5</version>
        </dependency>

        <dependency>
            <groupId>net.kaczmarzyk</groupId>
            <artifactId>specification-arg-resolver</artifactId>
            <version>2.6.2</version>
        </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>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

The problem comes with the Controller in the account-service, the response bean and the error code of the API come from the common-service. Initially while importing it showed compilation issues for both but after doing a maven-update there were no errors.

This is the controller

package com.hh.sukku.account.controller;

import static com.hh.sukku.common.util.ErrorCodes.SUCCESS;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hh.sukku.account.dto.UserDTO;
import com.hh.sukku.account.service.UserService;
import com.hh.sukku.common.beans.Response;

/**
 * 
 * @author arun.sudhakaran
 *
 * 02-Apr-2023 11:14:15 pm
 */

@RestController
@RequestMapping("v1/user")
public class UserController {
    
    @Autowired
    private UserService userService;

    @PostMapping(path = "", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Response> create(@Valid @RequestBody UserDTO request) {
        
        userService.create(request);
        
        Response response = new Response(SUCCESS, "Success");
        
        return new ResponseEntity<Response>(response, HttpStatus.OK);
    }
}

The problem starts when we try to do a maven clean install on the parent module, it fails with this error

Console output of Maven clean install

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO] 
[INFO] hh                                                                 [pom]
[INFO] common-service                                                     [jar]
[INFO] account-service                                                    [jar]
[INFO] product-service                                                    [jar]
[INFO] auth-service                                                       [jar]
[INFO] application-service                                                [war]
[INFO] 
[INFO] --------------------------< com.hh.sukku:hh >---------------------------
[INFO] Building hh 1.0.0                                                  [1/6]
[INFO] --------------------------------[ pom ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ hh ---
[INFO] Deleting D:\hh\haraji\workspace\hh\target
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.3.4.RELEASE:repackage (repackage) @ hh ---
[INFO] 
[INFO] --------------------< com.hh.sukku:common-service >---------------------
[INFO] Building common-service 1.0.0                                      [2/6]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ common-service ---
[INFO] Deleting D:\hh\haraji\workspace\hh\common-service\target
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ common-service ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ common-service ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 9 source files to D:\hh\haraji\workspace\hh\common-service\target\classes
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ common-service ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ common-service ---
[INFO] Changes detected - recompiling the module!
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ common-service ---
[INFO] 
[INFO] --- maven-jar-plugin:3.1.1:jar (default-jar) @ common-service ---
[INFO] Building jar: D:\hh\haraji\workspace\hh\common-service\target\common-service-1.0.0.jar
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.3.4.RELEASE:repackage (repackage) @ common-service ---
[INFO] Replacing main artifact with repackaged archive
[INFO] 
[INFO] --------------------< com.hh.sukku:account-service >--------------------
[INFO] Building account-service 1.0.0                                     [3/6]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ account-service ---
[INFO] Deleting D:\hh\haraji\workspace\hh\account-service\target
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ account-service ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ account-service ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 17 source files to D:\hh\haraji\workspace\hh\account-service\target\classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/service/FileUploadService.java:[21,37] package com.hh.sukku.common.exception does not exist
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/service/FileUploadService.java:[22,34] package com.hh.sukku.common.params does not exist
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/service/FileUploadService.java:[23,32] package com.hh.sukku.common.util does not exist
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/service/FileUploadService.java:[47,17] cannot find symbol
  symbol:   class ParamsFileOperations
  location: class com.hh.sukku.account.service.FileUploadService
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/controller/UserController.java:[3,39] package com.hh.sukku.common.util does not exist
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/controller/UserController.java:[3,1] static import only from classes and interfaces
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/controller/UserController.java:[25,33] package com.hh.sukku.common.beans does not exist
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/controller/UserController.java:[26,33] package com.hh.sukku.common.beans does not exist
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/controller/UserController.java:[27,32] package com.hh.sukku.common.util does not exist
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/service/UserService.java:[19,33] package com.hh.sukku.common.beans does not exist
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/service/UserService.java:[20,37] package com.hh.sukku.common.exception does not exist
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/controller/UserController.java:[50,31] cannot find symbol
  symbol:   class Response
  location: class com.hh.sukku.account.controller.UserController
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/controller/UserController.java:[60,16] cannot find symbol
  symbol:   class PagedResponse
  location: class com.hh.sukku.account.controller.UserController
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/service/UserService.java:[59,16] cannot find symbol
  symbol:   class PagedResponse
  location: class com.hh.sukku.account.service.UserService
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/controller/UserController.java:[61,70] cannot find symbol
  symbol:   variable Constants
  location: class com.hh.sukku.account.controller.UserController
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/controller/UserController.java:[62,70] cannot find symbol
  symbol:   variable Constants
  location: class com.hh.sukku.account.controller.UserController
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/controller/UserController.java:[64,71] cannot find symbol
  symbol:   variable Constants
  location: class com.hh.sukku.account.controller.UserController
[INFO] 17 errors 
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] hh 1.0.0 ........................................... SUCCESS [  2.689 s]
[INFO] common-service ..................................... SUCCESS [  8.217 s]
[INFO] account-service .................................... FAILURE [  3.859 s]
[INFO] product-service .................................... SKIPPED
[INFO] auth-service ....................................... SKIPPED
[INFO] application-service 1.0.0 .......................... SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 16.033 s
[INFO] Finished at: 2023-04-11T22:39:59+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project account-service: Compilation failure: Compilation failure: 
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/service/FileUploadService.java:[21,37] package com.hh.sukku.common.exception does not exist
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/service/FileUploadService.java:[22,34] package com.hh.sukku.common.params does not exist
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/service/FileUploadService.java:[23,32] package com.hh.sukku.common.util does not exist
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/service/FileUploadService.java:[47,17] cannot find symbol
[ERROR]   symbol:   class ParamsFileOperations
[ERROR]   location: class com.hh.sukku.account.service.FileUploadService
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/controller/UserController.java:[3,39] package com.hh.sukku.common.util does not exist
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/controller/UserController.java:[3,1] static import only from classes and interfaces
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/controller/UserController.java:[25,33] package com.hh.sukku.common.beans does not exist
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/controller/UserController.java:[26,33] package com.hh.sukku.common.beans does not exist
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/controller/UserController.java:[27,32] package com.hh.sukku.common.util does not exist
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/service/UserService.java:[19,33] package com.hh.sukku.common.beans does not exist
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/service/UserService.java:[20,37] package com.hh.sukku.common.exception does not exist
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/controller/UserController.java:[50,31] cannot find symbol
[ERROR]   symbol:   class Response
[ERROR]   location: class com.hh.sukku.account.controller.UserController
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/controller/UserController.java:[60,16] cannot find symbol
[ERROR]   symbol:   class PagedResponse
[ERROR]   location: class com.hh.sukku.account.controller.UserController
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/service/UserService.java:[59,16] cannot find symbol
[ERROR]   symbol:   class PagedResponse
[ERROR]   location: class com.hh.sukku.account.service.UserService
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/controller/UserController.java:[61,70] cannot find symbol
[ERROR]   symbol:   variable Constants
[ERROR]   location: class com.hh.sukku.account.controller.UserController
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/controller/UserController.java:[62,70] cannot find symbol
[ERROR]   symbol:   variable Constants
[ERROR]   location: class com.hh.sukku.account.controller.UserController
[ERROR] /D:/hh/haraji/workspace/hh/account-service/src/main/java/com/hh/sukku/account/controller/UserController.java:[64,71] cannot find symbol
[ERROR]   symbol:   variable Constants
[ERROR]   location: class com.hh.sukku.account.controller.UserController
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
[ERROR] 
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <goals> -rf :account-service

We even tried maven clean install -X and maven install individually on the account-service. All attempts failed with the same error.

The fun fact is that the application can be started successfully but when we try to hit the API we are getting 404. What could be missing? There are no visible compilation errors.

The other interesting fact is that when we tried to import the controller from the account-service into the application-service and tried to build application-service it was a success. But when we tried to import a bean from the common-service the same got failed, so we can narrow down that the issue is with the common-service.

But at some point when we started the application-service we were able to process the request and insert data into the table, so we tried to build the parent pom, but again it failed. Later the application started giving 404 again for the same request.

Major changes with initial edit

  1. One thing we found was that the groupId of the modules was not the same as the parent, so we created a new one with all of them having the same groupId. The issue remained the same.
  2. We hardcoded the parent versions to every module and their dependencies, but the issue remained the same.
  3. We tried to import the Response bean from the Fix project setup... option and locate the bean in the popup. The issue remained the same.
  4. We newly created the same project and added the new modules by checking the options Create a simple project (skip archetype selection) and Add project(s) to the working set, but the issue remained the same.
  5. There was a warning with the parent pom.xml, so we removed the pom from the profile section in the run configuration (reference), but the issue remained the same.
  6. We even tried compile package, which was successful, and two war files were generated application-service-1.0.0.war and hh-1.0.0.war. But the deployment was not fine, there were no logs of a spring boot application startup and requests were giving 404.

Major changes with @Toerktumlare's suggestions

  1. Removed the child modules from the .m2, and performed mvn clean package the result was the same failure.

  2. Added the compiler plugin and performed step-1, failed again.

  3. Tried running this command --projects account-service --also-make clean package for individual modules. First was common-service and it was successful, but with account-service it failed saying

    [ERROR] Failed to execute goal on project account-service: Could not resolve dependencies for project com.hh.sukku:account-service:jar:1.0.0: Could not find artifact com.hh.sukku:common-service:jar:1.0.0 in central (https://repo.maven.apache.org/maven2)

Project URL : https://github.com/Arun-Sudhakaran/hh


Solution

  • After a test application was provided i managed to build the project after i applied what i recommended as a second course of action in the comments above which had not been implemented.

    So by adding the following in the pom.xml i managed to compile the project

    hh/pom.xml

    <build>
        <pluginManagement> <!-- this line here -->
            <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>
    
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement> <!-- and this line here -->
    </build>
    

    you can read more about pluginManagementin the following links:

    plugin management baeldung

    what is plugin management

    difference between plugins and pluginmanagement tag in maven pom xml