如何从现有任务创建自定义 sbt 任务

How can I create a custom sbt task from existing tasks

我想创建一个使用 publishLocal in Dockerlogin in ecrpublishLocal in Docker 的自定义 sbt 任务。

另请注意,我仅在自定义任务上修改了 "publishLocal in Docker" 设置。

我尝试编写下面的代码,但没有成功。

val releaseDev = taskKey[Unit]("push docker image")
releaseDev := (push in ecr).value
releaseDev := releaseDev.dependsOn(publishLocal in Docker).value
releaseDev := (projectSettings ++ Seq(repositoryName in ecr := (packageName in Docker).value + "-stg" + ":" + (version in Docker).value))

以下是如何从您的自定义任务中触发现有任务。从你的问题来看,你希望它们以某种特定的顺序被触发,所以这就是你需要做的

lazy val releaseDev = taskKey[Unit]("push docker image")
releaseDev := Def.sequential(push in ecr, publishLocal in Docker).value

现在,当您触发 releaseDev 时,它会先转 push in ecr,然后 publishLocal in Docker

注意:不要忘记在您的 taskKey 定义中添加 lazy,因为它可能会导致一些奇怪的初始化问题。

谢谢马里奥斯。 我尝试编写下面的代码,并正确处理了任务依赖性。 但是 repositoryName 用于项目设置之一。

// docker publish settings
import com.amazonaws.regions.{Region, Regions}

region in ecr := Region.getRegion(Regions.AP_NORTHEAST_1)
repositoryName in ecr := (packageName in Docker).value + ":" + (version in Docker).value
localDockerImage in ecr := (packageName in Docker).value + ":" + (version in Docker).value

// Publisher Setting
//~~~~~~~~~~~~~~~~~~~
import ReleaseTransformations._

releaseProcess := Seq[ReleaseStep](
  checkSnapshotDependencies,
  inquireVersions,
  runClean,
  runTest,
  setReleaseVersion,
  commitReleaseVersion,
  tagRelease,
  ReleaseStep(state => Project.extract(state).runTask(publishLocal in Docker, state)._1),
  ReleaseStep(state => Project.extract(state).runTask(login in ecr, state)._1),
  ReleaseStep(state => Project.extract(state).runTask(push in ecr, state)._1),
  setNextVersion,
  commitNextVersion,
  pushChanges
)

lazy val releaseDev = taskKey[Unit]("push docker image")
releaseDev := Def.sequential( publishLocal in Docker, login in ecr, push in ecr).value
(repositoryName in ecr) in releaseDev := (packageName in Docker).value + "-stg" + ":" + (version in Docker).value