【问题标题】:AWS Codepipeline Spring boot War fileAWS Codepipeline Spring Boot War 文件
【发布时间】:2021-07-23 21:31:52
【问题描述】:

我有一个带有 gradle 作为依赖管理器的 Spring Boot 应用程序。我一直在使用导出为战争文件创建战争文件。并在 AWS ElasticBeanstalk 中部署了相同的内容。我使用的平台是Tomcat 8.5 with Java 8 running on 64bit Amazon Linux/3.4.5

现在,我想使用 AWS 的 CI/CD,并想创建一个代码管道来将 war 文件部署到 EBS。

在创建代码管道时,它要求我构建代码构建项目。

以下是我的构建命令:

version: 0.2

#env:
  #variables:
     # key: "value"
     # key: "value"
  #parameter-store:
     # key: "value"
     # key: "value"
  #secrets-manager:
     # key: secret-id:json-key:version-stage:version-id
     # key: secret-id:json-key:version-stage:version-id
  #exported-variables:
     # - variable
     # - variable
  #git-credential-helper: yes
#batch:
  #fast-fail: true
  #build-list:
  #build-matrix:
  #build-graph:
phases:
  #install:
    #If you use the Ubuntu standard image 2.0 or later, you must specify runtime-versions.
    #If you specify runtime-versions and use an image other than Ubuntu standard image 2.0, the build fails.
    #runtime-versions:
      # name: version
      # name: version
    #commands:
      # - command
      # - command
  #pre_build:
    #commands:
      # - command
      # - command
  build:
    commands:
      - gradle build
      # - command
  #post_build:
    #commands:
      # - command
      # - command
#reports:
  #report-name-or-arn:
    #files:
      # - location
      # - location
    #base-directory: location
    #discard-paths: yes
    #file-format: JunitXml | CucumberJson
#artifacts:
  #files:
    # - location
    # - location
  #name: $(date +%Y-%m-%d)
  #discard-paths: yes
  #base-directory: location
#cache:
  #paths:
    # - paths

我在此处进行了哪些更改,以便创建 war 文件并将其部署到 EBS。因为,我对构建过程和 CI/CD 还很陌生,所以我有点困惑。

我的 build.gradle 文件是:

plugins {
    id 'org.springframework.boot' version '2.2.7.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
    id 'war'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
    developmentOnly
    runtimeClasspath {
        extendsFrom developmentOnly
    }
}

repositories {
    mavenCentral()
}

dependencies {
    
    implementation 'org.springframework.boot:spring-boot-starter-web'
    annotationProcessor 'org.projectlombok:lombok'
    providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
    
    //compile group: 'org.hibernate', name: 'hibernate-gradle-plugin', version: '5.4.15.Final'
    
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '2.2.7.RELEASE'
    compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.20'
    
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.11.0'
    
    compile group: 'io.jsonwebtoken', name: 'jjwt', version: '0.9.1'
    
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-mail', version: '2.3.0.RELEASE'
    
    
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-security', version: '2.3.0.RELEASE'
    
    compile group: 'com.amazonaws', name: 'aws-java-sdk', version: '1.11.784'
    
    providedCompile group: 'org.projectlombok', name: 'lombok', version: '0.11.0'
    // https://mvnrepository.com/artifact/org.projectlombok/lombok
    compileOnly group: 'org.projectlombok', name: 'lombok', version: '1.18.18'
    
    
    compile group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
    
    compile group: 'org.apache.poi', name: 'poi-ooxml', version: '4.1.2'
    
    compile group: 'org.apache.poi', name: 'poi', version: '4.1.2'
    
    compile group: 'org.codehaus.mojo', name: 'exec-maven-plugin', version: '3.0.0'
    
    compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'
    
    compile group: 'com.google.zxing', name: 'core', version: '3.4.1'
    
    implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.12.0'
    
    
    implementation group: 'com.google.common', name: 'google-collect', version: '0.5'
    
    
    
    
    developmentOnly("org.springframework.boot:spring-boot-devtools")
    
    
    
    
    
    //testImplementation 'org.springframework.security:spring-security-test'
}

test {
    useJUnitPlatform()
}

【问题讨论】:

    标签: amazon-web-services spring-boot gradle aws-codepipeline


    【解决方案1】:

    几天前我遇到了同样的问题,我几乎找不到任何好的 gradle 资源。通过上传 gradle 构建期间生成的 WAR 文件,您可以轻松地将应用程序直接部署到 Elastic Beanstalk。

    当您将 WAR 文件上传到 Beanstalk 时,它会被提取/分解,而 Beanstalk 会在内部为您执行此操作。但是在使用管道的时候,需要直接上传炸开的文件,而不是WAR。看来 AWS Codepipeline 仍然不是 WAR 友好的。

    首先要生成分解的 WAR 文件,您需要在 build.gradle 中添加以下内容:

    task explodedWar(type: Sync) {
    into "${buildDir}/exploded"
    with war }
    

    要生成展开的文件夹,您需要通过终端使用命令运行以下任务:

    ./gradlew explodedWar
    

    您会在build 文件夹中找到一个名为exploded 的文件夹,您可以在其中看到分解后的WAR 的内容。

    这里是以下 builspec.yml 文件,您可以使用它来构建代码并使用管道进行部署。我使用的环境是Tomcat 8.5 with Corretto 8 running on 64bit Amazon Linux 2

    version: 0.1
    phases:
      install:
        run-as: root
        commands:
          - echo Installing gradle
          - wget https://services.gradle.org/distributions/gradle-7.1.1-bin.zip
          - unzip -d /opt/gradle gradle-7.1.1-bin.zip
      pre_build:
        commands:
          - echo Entering pre build
      build:
        commands:
          - /opt/gradle/gradle-7.1.1/bin/gradle clean
          - /opt/gradle/gradle-7.1.1/bin/gradle explodedWar
      post_build:
        commands:
          - echo Entering post build
    artifacts:
      files:
        - '**/*'
      base-directory: build/exploded/
      discard-paths: no
    

    您可以查看AWS buildspec.yml documentation 了解更多语法和功能,具体取决于您的用例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-03
      • 1970-01-01
      • 1970-01-01
      • 2021-05-27
      • 1970-01-01
      • 2020-07-05
      • 2017-09-04
      • 2015-09-04
      相关资源
      最近更新 更多