如何使用AWS服务配置AWS Lambda与Java的持续集成AWS CodeBuild,Codedeploy,CloudFormation

时间:2019-06-13 19:38:00

标签: java amazon-web-services aws-codepipeline aws-codebuild

我正在使用aws的持续集成工具来实现lambda函数。 CodeSource,CodeBuild CodePipeLine。

设置全部后,当我测试lambda时,结果为

{
  "errorMessage": "Class not found: com.ad.client.App",
  "errorType": "java.lang.ClassNotFoundException"
}

Class not found: com.ad.client.App: java.lang.ClassNotFoundException
java.lang.ClassNotFoundException: com.ad.client.App
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)



流水线的所有阶段都已成功完成(源,构建,部署)

如果直接在lambda控制台中加载jar,则结果是正确的 我查看了构建日志,发现了这一点:

[Container] 2019/06/13 13:09:38 Running command echo THE PATH WORK IS !!! 
THE PATH WORK IS !!! 

[Container] 2019/06/13 13:09:38 Running command pwd 
/codebuild/output/src748698927/src 

[Container] 2019/06/13 13:09:38 Running command echo The list of file is !! 
The list of file is !! 

[Container] 2019/06/13 13:09:38 Running command ls 
Readme.md 
buildspec.yml 
dependency-reduced-pom.xml 
ftc-client.iml 
outputtemplate.yaml 
pom.xml 
src 
target 
template.yaml 

[Container] 2019/06/13 13:09:38 Running command echo CODE BUILD SRC DIRECTORY 
CODE BUILD SRC DIRECTORY 

[Container] 2019/06/13 13:09:38 Running command echo $CODEBUILD_SRC_DIR 
/codebuild/output/src748698927/src 

INFO] skip non existing resourceDirectory /codebuild/output/src748698927/src/src/main/resources 

在代码的某些部分中,我知道src路径是重复的,我不知道它是否与问题有关

我的配置文件是:

template.yaml

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Ftc-client
Resources:
  FtcClientFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: com.ad.client.App::handleRequest
      Runtime: java8
      CodeUri: ./
      Events:
        MyFtcClientApi:
          Type: Api
          Properties:
            Path: /client
            Method: GET

buildspec.yml

version: 0.2

phases:
  install:
    runtime-versions:
      java: openjdk8
  build:
    commands:
      - echo Build started on `date`
      - mvn test
      - export BUCKET=my-bucket-for-test
      - aws cloudformation package --template-file template.yaml --s3-bucket $BUCKET --output-template-file outputtemplate.yaml
    finally:
      - echo THE PATH WORK IS !!!
      - pwd
      - echo The list of file is !!
      - ls
      - echo CODE BUILD SRC DIRECTORY
      - echo $CODEBUILD_SRC_DIR
  post_build:
    commands:
      - echo Build completed on `date`
      - mvn package
artifacts:
  files:
    - target/ftc-client-1.0-SNAPSHOT.jar
    - template.yaml
    - outputtemplate.yaml
  discard-paths: yes

The source code structure is :
/fclient/src/main/java/com/ad/App.java
/tclient/buildspec.yml
/fclient/pom.xml
/fclient/template.yaml

我想用Java来做到这一点:https://docs.aws.amazon.com/lambda/latest/dg/build-pipeline.html

感谢所有可以给我提示的人

1 个答案:

答案 0 :(得分:1)

好的,这是解决方案,有必要将jar解压缩到我的代码的根目录中。

version: 0.2
phases:
  install:
    runtime-versions:
      java: openjdk8
  pre_build:
    commands:
      - echo Test started on `date`
      - mvn clean compile test
  build:
    commands:
      - echo Build started on `date`
      - export BUCKET=my-bucket-for-test
      - mvn package shade:shade
      - mv target/ftc-client-1.0-SNAPSHOT.jar
      - unzip ftc-client-1.0-SNAPSHOT.jar
      - rm -rf target tst src buildspec.yml pom.xml ftc-client-1.0-SNAPSHOT.jar
      - aws cloudformation package --template-file template.yaml --s3-bucket $BUCKET --output-template-file outputtemplate.yaml
  post_build:
    commands:
      - echo Build completed on `date` !!!
artifacts:
  files:
    - tarjet/ftc-client-1.0-SNAPSHOT.jar
    - template.yaml
    - outputtemplate.yaml

https://docs.aws.amazon.com/codebuild/latest/userguide/build-spec-ref.html