简体   繁体   English

使用 sbt 将工件发布到 AWS CodeArtifact

[英]Publish artifact to AWS CodeArtifact with sbt

I'm trying to publish artifacts to AWS CodeArtifact using sbt , but I'm having some troubles;我正在尝试使用sbt将工件发布到AWS CodeArtifact ,但我遇到了一些麻烦;

Given an sbt project, running the command sbt publish the package is uploaded to the repo but It remains in the Unfinished state. The AWS CodeArtifact documentation says:给定一个sbt项目,运行命令sbt publish package 已上传到存储库,但它仍保留在Unfinished的 state 中。AWS CodeArtifact 文档说:

Unfinished : The last attempt to publish did not complete.未完成:上次发布尝试未完成。 Currently only Maven package versions can have a status of Unfinished.目前只有 Maven package 版本可以具有未完成状态。 This can occur when the client uploads one or more assets for a package version but does not publish a maven-metadata.xml file for the package that includes that version.当客户端为 package 版本上传一个或多个资产但不为包含该版本的 package 发布 maven-metadata.xml 文件时,可能会发生这种情况。

I'm using sbt at version 1.3.3 , I'm not using plugins, the property publishMavenStyle is true .我在1.3.3版本使用sbt ,我没有使用插件,属性publishMavenStyletrue

I know that the sbt-maven-resolver ( here the repo) solves the issue, but it seems an "unfollowed plugin", and moreover, using it I lose all logs about the publishing process, I don't trust it.我知道sbt-maven-resolver这里是 repo)解决了这个问题,但它似乎是一个“未关注的插件”,而且,使用它我丢失了关于发布过程的所有日志,我不相信它。

Did anyone have the same issue and have solved it somehow?有没有人有同样的问题并以某种方式解决了它?

Using CodeArtifact with SBT将 CodeArtifact 与 SBT 结合使用

  1. Setting up SBT with CodeArtifact使用 CodeArtifact 设置 SBT
  2. Publishing Packages with SBT (also avoiding the artifact being in Unfinished state.)使用 SBT 发布包(也避免工件处于Unfinished状态。)

1. Setting up SBT with CodeArtifact 1. 使用 CodeArtifact 设置 SBT

  1. Create a CodeArtifact repository with a Maven upstream.使用 Maven 上游创建 CodeArtifact 存储库。 For this example we're going to use repository maven-test in domain launchops对于这个例子,我们将在域 launchops 中使用存储库 maven-test

  2. Open up the Connection Instructions in the console and choose mvn.在控制台中打开连接说明并选择 mvn。 We will need information from this later.稍后我们将需要这些信息。

  3. Copy the command which exports the "CODEARTIFACT_AUTH_TOKEN" environment variable from the console and run it in your environment.复制从控制台导出“CODEARTIFACT_AUTH_TOKEN”环境变量的命令并在您的环境中运行它。 This will set the "CODEARTIFACT_AUTH_TOKEN" to be the password for our repository, the username is always aws.这会将“CODEARTIFACT_AUTH_TOKEN”设置为我们存储库的密码,用户名始终为 aws。

  4. In the build.sbt file import sbt.Credentials:在 build.sbt 文件中导入 sbt.Credentials:

     import sbt.{Credentials}
  5. Now we need to setup the credentials.现在我们需要设置凭据。 To do this we're first going to read the CODEARTIFACT_AUTH_TOKEN environment variable:为此,我们首先要读取 CODEARTIFACT_AUTH_TOKEN 环境变量:

     val repoPass = sys.env.get("CODEARTIFACT_AUTH_TOKEN").getOrElse("")
  6. Next, we're going to use the previously imported sbt.Credentials to setup a new set of Credentials:接下来,我们将使用之前导入的 sbt.Credentials 来设置一组新的 Credentials:

     credentials += Credentials("launchops/maven-test", "launchops-123456789012.d.codeartifact.us-east-1.amazonaws.com", "aws", repoPass)

The values passed to the Credentials object are ("domain-name/repository-name", "repository hostname without protocol", "username", "password"), with username always being aws and password coming from the repoPass variable we only need to modify the first two to point to our repository.传递给 Credentials 对象的值是 ("domain-name/repository-name", "repository hostname without protocol", "username", "password"),用户名总是 aws,密码来自 repoPass 变量,我们只需要修改前两个以指向我们的存储库。

  1. Now we just need to instruct SBT to use our repository as a resolver.现在我们只需要指示 SBT 使用我们的存储库作为解析器。 The consoles connection instructions will generate Maven settings, for example:控制台连接指令会生成 Maven 设置,例如:

     <repository> <id>launchops--maven-test</id> <url>https://launchops-123456789012.d.codeartifact.us-east-1.amazonaws.com/maven/maven-test/</url> </repository>

We will use these values to create a resolver in our build.sbt file:我们将使用这些值在我们的build.sbt文件中创建一个解析器:

    resolvers += "launchops--maven-test" at "https://launchops-123456789012.d.codeartifact.us-east-1.amazonaws.com/maven/maven-test"

The format of this is "resolvers += "ID From maven configuration in console" at "Repository URL from maven configuration in console".其格式为“来自控制台中 Maven 配置的存储库 URL”中的“解析器 += 来自控制台中的 Maven 配置的 ID”。

  1. To completely disable the use of public Maven repositories (Force CodeArtifact usage) you can add the following line to the build.sbt file:要完全禁用公共 Maven 存储库(强制使用 CodeArtifact),您可以将以下行添加到build.sbt文件:

     externalResolvers := Resolver.combineDefaultResolvers(resolvers.value.toVector, mavenCentral = false)

After performing these setups steps you should be able to run sbt update and observe packages being downloaded through CodeArtifact.执行这些设置步骤后,您应该能够运行sbt update并观察通过 CodeArtifact 下载的包。

Sample build.sbt for reference:示例 build.sbt 供参考:

import sbt.{Credentials, Path}

name := "scala-test"

version := "0.3.0"

scalaVersion := "2.12.6"

organization := "com.abc.def"

val repoPass = sys.env.get("CODEARTIFACT_AUTH_TOKEN").getOrElse("")
credentials += Credentials("launchops/maven-test", "launchops-123456789012.d.codeartifact.us-east-1.amazonaws.com", "aws", repoPass)

resolvers += "launchops--maven-test" at "https://launchops-123456789012.d.codeartifact.us-east-1.amazonaws.com/maven/maven-test"

libraryDependencies ++= Seq(
        "org.scalatest" %% "scalatest" % "3.0.0" % "test",
        "io.nats" % "jnats" % "2.0.0",
        "org.json4s" %% "json4s-native" % "3.6.0"
)

2. Publishing Packages 2. 发布包

Apart from pulling dependencies, SBT can also be used to publish packages.除了拉取依赖之外,SBT 还可以用于发布包。 To have SBT publish to CodeArtifact we first need to set it up in the build.sbt file:要让 SBT 发布到 CodeArtifact,我们首先需要在 build.sbt 文件中设置它:

Add the following to the file:将以下内容添加到文件中:

publishMavenStyle := true
publishTo := Some("launchops--maven-test" at "https://launchops-123456789012.d.codeartifact.us-east-1.amazonaws.com/maven/maven-test")

At this point, technically, running sbt publish will push the package to CodeArtifact, however it will end up in Unfinished state.此时,从技术上讲,运行sbt publish会将包推送到 CodeArtifact,但最终会处于Unfinished状态。 We need to make use of sbt-maven-resolver plugin to help get the package in the correct format: https://github.com/sbt/sbt-maven-resolver我们需要使用 sbt-maven-resolver 插件来帮助以正确的格式获取包: https : //github.com/sbt/sbt-maven-resolver

In the project/plugins.sbt file add the following line:在 project/plugins.sbt 文件中添加以下行:

addSbtPlugin("org.scala-sbt" % "sbt-maven-resolver" % "0.1.0")

Now you can run sbt publish and have the package publish to CodeArtifact successfully.现在您可以运行sbt publish并将包成功发布到 CodeArtifact。 If you see an error make sure that you are using a recent version of SBT.如果您看到错误,请确保您使用的是最新版本的 SBT。

You can achieve the same without using the sbt-maven-resolver plugin by following shariqmaws' answer without the plugin.您可以在不使用 sbt-maven-resolver 插件的情况下通过在没有插件的情况下遵循 shariqmaws 的回答来实现相同的目的。

The publish will result in an artifact in "unpublished" state.发布将导致“未发布”state 中的工件。

Then use the aws codeartifact cli to publish it ( https://docs.aws.amazon.com/codeartifact/latest/ug/maven-curl.html )然后使用 aws codeartifact cli 发布它 ( https://docs.aws.amazon.com/codeartifact/latest/ug/maven-curl.html )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM