Search code examples
amazon-web-servicesmavensam

I cant build my spring-boot multi module project with SAM


When running SAM build, I get a dependency error on the module that depends on another local module within my project. From mvn or IntelliJ I have no problems, but when i execute SAM build, i got an error of notFound symbols and classses.


    Build Failed
    Error: JavaMavenWorkflow:MavenBuild - Maven Failed: [INFO] Scanning for projects...
    [INFO] ------------------------------------------------------------------------
    [INFO] Reactor Build Order:
    [INFO]
    [INFO] backend                                                            [pom]
    [INFO] Api                                                                [jar]
    [INFO] Register                                                           [jar]
    [INFO]
    [INFO] ------------------------< com.backend:backend >-------------------------
    [INFO] Building backend 0.0.1-SNAPSHOT                                    [1/3]
    [INFO] --------------------------------[ pom ]---------------------------------
    [INFO]
    [INFO] --- maven-clean-plugin:3.2.0:clean (default-clean) @ backend ---
    [INFO]
    [INFO] --- spring-boot-maven-plugin:3.0.6:repackage (repackage) @ backend ---
    [INFO]
    [INFO] --- maven-install-plugin:3.0.1:install (default-install) @ backend ---
    [INFO] Installing /tmp/tmptflbmqgd/pom.xml to /home/laingard/.m2/repository/com/backend/backend/0.0.1-SNAPSHOT/backend-0.0.1-SNAPSHOT.pom
    [INFO]
    [INFO] --------------------------< com.backend:Api >---------------------------
    [INFO] Building Api 0.0.1-SNAPSHOT                                        [2/3]
    [INFO] --------------------------------[ jar ]---------------------------------
    [INFO]
    [INFO] --- maven-clean-plugin:3.2.0:clean (default-clean) @ Api ---
    [INFO] Deleting /tmp/tmptflbmqgd/Api/target
    [INFO]
    [INFO] --- maven-resources-plugin:3.3.1:resources (default-resources) @ Api ---
    [INFO] Copying 0 resource from src/main/resources to target/classes
    [INFO] skip non existing resourceDirectory /tmp/tmptflbmqgd/Api/config
    [INFO]
    [INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) @ Api ---
    [INFO] Changes detected - recompiling the module!
    [INFO] Compiling 93 source files to /tmp/tmptflbmqgd/Api/target/classes
    [INFO]
    [INFO] --- maven-resources-plugin:3.3.1:testResources (default-testResources) @ Api ---
    [INFO] skip non existing resourceDirectory /tmp/tmptflbmqgd/Api/src/test/resources
    [INFO]
    [INFO] --- maven-compiler-plugin:3.10.1:testCompile (default-testCompile) @ Api ---
    [INFO] Changes detected - recompiling the module!
    [INFO]
    [INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ Api ---
    [INFO] Tests are skipped.
    [INFO]
    [INFO] --- maven-jar-plugin:3.3.0:jar (default-jar) @ Api ---
    [INFO] Building jar: /tmp/tmptflbmqgd/Api/target/Api-0.0.1-SNAPSHOT.jar
    [INFO]
    [INFO] --- spring-boot-maven-plugin:3.0.6:repackage (repackage) @ Api ---
    [INFO] Replacing main artifact with repackaged archive
    [INFO]
    [INFO] --- maven-install-plugin:3.0.1:install (default-install) @ Api ---
    [INFO] Installing /tmp/tmptflbmqgd/Api/pom.xml to /home/laingard/.m2/repository/com/backend/Api/0.0.1-SNAPSHOT/Api-0.0.1-SNAPSHOT.pom
    [INFO] Installing /tmp/tmptflbmqgd/Api/target/Api-0.0.1-SNAPSHOT.jar to /home/laingard/.m2/repository/com/backend/Api/0.0.1-SNAPSHOT/Api-0.0.1-SNAPSHOT.jar
    [INFO]
    [INFO] ------------------------< com.backend:Register >------------------------
    [INFO] Building Register 0.0.1-SNAPSHOT                                   [3/3]
    [INFO] --------------------------------[ jar ]---------------------------------
    [INFO]
    [INFO] --- maven-clean-plugin:3.2.0:clean (default-clean) @ Register ---
    [INFO] Deleting /tmp/tmptflbmqgd/Register/target
    [INFO]
    [INFO] --- maven-resources-plugin:3.3.1:resources (default-resources) @ Register ---
    [INFO] Copying 0 resource from src/main/resources to target/classes
    [INFO] skip non existing resourceDirectory /tmp/tmptflbmqgd/Register/config
    [INFO]
    [INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) @ Register ---
    [INFO] Changes detected - recompiling the module!
    [INFO] Compiling 9 source files to /tmp/tmptflbmqgd/Register/target/classes
    [INFO] -------------------------------------------------------------
    [ERROR] COMPILATION ERROR :
    [INFO] -------------------------------------------------------------
    [ERROR] /tmp/tmptflbmqgd/Register/src/main/java/com/careerwatch/register/service/RegisterServiceImpl.java:[3,34] package com.careerwatch.Api.entity does not exist
    [ERROR] /tmp/tmptflbmqgd/Register/src/main/java/com/careerwatch/register/service/RegisterServiceImpl.java:[4,37] package com.careerwatch.Api.exception does not exist
    [ERROR] /tmp/tmptflbmqgd/Register/src/main/java/com/careerwatch/register/service/RegisterServiceImpl.java:[5,38] package com.careerwatch.Api.repository does not exist
    [ERROR] /tmp/tmptflbmqgd/Register/src/main/java/com/careerwatch/register/service/RegisterServiceImpl.java:[18,19] cannot find symbol
      symbol:   class UserRepository
      location: class com.careerwatch.register.service.RegisterServiceImpl
    [ERROR] /tmp/tmptflbmqgd/Register/src/main/java/com/careerwatch/register/jwt/JwtService.java:[3,34] package com.careerwatch.Api.entity does not exist
    [ERROR] /tmp/tmptflbmqgd/Register/src/main/java/com/careerwatch/register/jwt/JwtService.java:[4,38] package com.careerwatch.Api.repository does not exist
    [ERROR] /tmp/tmptflbmqgd/Register/src/main/java/com/careerwatch/register/mapper/RegisterDtoMapper.java:[3,34] package com.careerwatch.Api.entity does not exist
    [ERROR] /tmp/tmptflbmqgd/Register/src/main/java/com/careerwatch/register/service/RegisterServiceImpl.java:[15,1] cannot find symbol
      symbol:   class UserRepository
      location: class com.careerwatch.register.service.RegisterServiceImpl
    [ERROR] /tmp/tmptflbmqgd/Register/src/main/java/com/careerwatch/register/mapper/RegisterDtoMapper.java:[13,12] cannot find symbol
      symbol:   class User
      location: class com.careerwatch.register.mapper.RegisterDtoMapper
    [ERROR] /tmp/tmptflbmqgd/Register/src/main/java/com/careerwatch/register/jwt/JwtService.java:[26,5] cannot find symbol
      symbol:   class UserRepository
      location: class com.careerwatch.register.jwt.JwtService
    [ERROR] /tmp/tmptflbmqgd/Register/src/main/java/com/careerwatch/register/jwt/JwtService.java:[34,33] cannot find symbol
      symbol:   class User
      location: class com.careerwatch.register.jwt.JwtService
    [ERROR] /tmp/tmptflbmqgd/Register/src/main/java/com/careerwatch/register/jwt/JwtService.java:[66,47] cannot find symbol
      symbol:   class User
      location: class com.careerwatch.register.jwt.JwtService
    [INFO] 12 errors
    [INFO] -------------------------------------------------------------
    [INFO] ------------------------------------------------------------------------
    [INFO] Reactor Summary for backend 0.0.1-SNAPSHOT:
    [INFO]
    [INFO] backend ............................................ SUCCESS [  0.565 s]
    [INFO] Api ................................................ SUCCESS [  3.314 s]
    [INFO] Register ........................................... FAILURE [  0.534 s]
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD FAILURE
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  4.575 s
    [INFO] Finished at: 2023-06-01T12:51:53-03:00
    [INFO] ------------------------------------------------------------------------
    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.10.1:compile (default-compile) on project Register: Compilation failure: Compilation failure:
    [ERROR] /tmp/tmptflbmqgd/Register/src/main/java/com/careerwatch/register/service/RegisterServiceImpl.java:[3,34] package com.careerwatch.Api.entity does not exist
    [ERROR] /tmp/tmptflbmqgd/Register/src/main/java/com/careerwatch/register/service/RegisterServiceImpl.java:[4,37] package com.careerwatch.Api.exception does not exist
    [ERROR] /tmp/tmptflbmqgd/Register/src/main/java/com/careerwatch/register/service/RegisterServiceImpl.java:[5,38] package com.careerwatch.Api.repository does not exist
    [ERROR] /tmp/tmptflbmqgd/Register/src/main/java/com/careerwatch/register/service/RegisterServiceImpl.java:[18,19] cannot find symbol
    [ERROR]   symbol:   class UserRepository
    [ERROR]   location: class com.careerwatch.register.service.RegisterServiceImpl
    [ERROR] /tmp/tmptflbmqgd/Register/src/main/java/com/careerwatch/register/jwt/JwtService.java:[3,34] package com.careerwatch.Api.entity does not exist
    [ERROR] /tmp/tmptflbmqgd/Register/src/main/java/com/careerwatch/register/jwt/JwtService.java:[4,38] package com.careerwatch.Api.repository does not exist
    [ERROR] /tmp/tmptflbmqgd/Register/src/main/java/com/careerwatch/register/mapper/RegisterDtoMapper.java:[3,34] package com.careerwatch.Api.entity does not exist
    [ERROR] /tmp/tmptflbmqgd/Register/src/main/java/com/careerwatch/register/service/RegisterServiceImpl.java:[15,1] cannot find symbol
    [ERROR]   symbol:   class UserRepository
    [ERROR]   location: class com.careerwatch.register.service.RegisterServiceImpl
    [ERROR] /tmp/tmptflbmqgd/Register/src/main/java/com/careerwatch/register/mapper/RegisterDtoMapper.java:[13,12] cannot find symbol
    [ERROR]   symbol:   class User
    [ERROR]   location: class com.careerwatch.register.mapper.RegisterDtoMapper
    [ERROR] /tmp/tmptflbmqgd/Register/src/main/java/com/careerwatch/register/jwt/JwtService.java:[26,5] cannot find symbol
    [ERROR]   symbol:   class UserRepository
    [ERROR]   location: class com.careerwatch.register.jwt.JwtService
    [ERROR] /tmp/tmptflbmqgd/Register/src/main/java/com/careerwatch/register/jwt/JwtService.java:[34,33] cannot find symbol
    [ERROR]   symbol:   class User
    [ERROR]   location: class com.careerwatch.register.jwt.JwtService
    [ERROR] /tmp/tmptflbmqgd/Register/src/main/java/com/careerwatch/register/jwt/JwtService.java:[66,47] cannot find symbol
    [ERROR]   symbol:   class User
    [ERROR]   location: class com.careerwatch.register.jwt.JwtService
    [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 <args> -rf :Register

This is my template.yml

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31

Globals:
  Function:
    Timeout: 30

Resources:
  CareerWatchFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: .
      Handler: com.careerwatch.Api.StreamLambdaHandler::handleRequest
      Runtime: java17
      AutoPublishAlias: production
      SnapStart:
        ApplyOn: PublishedVersions
      Architectures:
        - x86_64
      MemorySize: 1024
      Environment:
        Variables:
          POWERTOOLS_SERVICE_NAME: CareerWatchApi
          DB_HOST: !Ref DBhost
          DB_PORT: !Ref DBport
          DB_NAME: !Ref DBname
          DB_USERNAME: !Ref DBusername
          DB_PASSWORD: !Ref DBpassword
      Events:
        HelloWorld:
          Type: Api
          Properties:
            Path: /{proxy+}
            Method: ANY
  RegisterFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: .
      Handler: com.careerwatch.register.RegisterLambdaHandler::handleRequest
      Runtime: java17
      AutoPublishAlias: production
      SnapStart:
        ApplyOn: PublishedVersions
      Architectures:
        - x86_64
      MemorySize: 1024
      Environment:
        Variables:
          POWERTOOLS_SERVICE_NAME: CareerWatchApi
          DB_HOST: !Ref DBhost
          DB_PORT: !Ref DBport
          DB_NAME: !Ref DBname
          DB_USERNAME: !Ref DBusername
          DB_PASSWORD: !Ref DBpassword
          SECRET_KEY: !Ref SecretKey
      Events:
        RegisterEndpoint:
          Type: Api
          Properties:
            Path: /api/v1/register
            Method: POST
Parameters:
  DBhost:
    Type: String
    Default: ''
    Description: Enter the DB host name or IP address
  DBport:
    Type: String
    Default: ''
    Description: Enter the DB port
  DBname:
    Type: String
    Default: ''
    Description: Enter the DB name
  DBusername:
    Type: String
    Default: ''
    Description: Enter the DB username
  DBpassword:
    Type: String
    Default: ''
    Description: Enter the DB password
  SecretKey:
      Type: String
      Default: ''
      Description: Enter the secret jwt key password

I did mvn install and I checked the whole project structure, however with maven it works, but with sam build it doesn't.


Solution

  • You stated in your comment:

    "I am building a serverless application, deploying lambdas functions in api gateway."

    If you are intereted in builidng a serverless app with Java, look at the PAM example. This example builds a complete serverless example that uses API Gateway, Lambda functions, Java SDK, a client app that uses Cognito to log in users, etc.

    Here is the overview illustration:

    enter image description here

    As well, this example uses the AWS CDK to standup various resources.

    This does not use SAM.

    See:

    Create a photo asset management application that lets users manage photos using labels