使用 aws codeartifact 和 MAVEN 构建规范

buildspec with aws codeartifact with MVN

我一直在寻找一个示例构建规范,其中将 codeartifacts 与 mvn 集成。

这是我的 buildpsec, 以下是我的疑惑。

  1. 由于我们无法在 aws 告诉我们提及服务器、镜像、配置文件和令牌的地方创建 setting.xml,我们如何将依赖项上传到工件存储库。
  2. 我的目标是只将生成的 jar 而不是 .m2/* 所有依赖项放入 artefact 存储库中,而且这是一个好方法吗?

buildspec file

version: 0.2 
phases: 
  install: 
    runtime-versions: 
      java: openjdk8 
    commands: 
      - pip3 install awscli --upgrade --user 
      - export CODEARTIFACT_TOKEN=`aws codeartifact get-authorization-token --domain $DOMAIN --domain-owner $ACCOUNT_ID --query authorizationToken --output text` 
  build: 
    commands: 
      - echo Build started on `date` 
      - mvn package 
artifacts: 
  type: zip 
  files: 
    - '/target/launcher-0.0.1-SNAPSHOT.jar' 
cache: 
  paths: 
    - '/root/.m2/**/*'.

since we cannot create a setting.xml where aws tells us to mention the servers, mirrors, profile and token, how can we upload the dependencies to the artifact repository.

事实上,你可以。你走在正确的轨道上。 mvn 直到 build 阶段才会执行,因此在 install 中您可以编辑其设置,包括 settings.xml。最简单的是完全替换它们:

phases:
  install:
    commands:
      - cp ./codebuild-maven-settings.xml /root/.m2/settings.xml 

然后您可以在自定义 settings.xml 文件中使用您的 CODEARTIFACT_TOKEN 环境变量。

为了使此解决方案起作用,您需要将 codebuild-maven-settings.xml 文件放在存储库的根目录下。这可能不是最优雅的,如果你真的想让它尽可能顺利,我建议先把文件放在 S3 上并下载它。