gpt4 book ai didi

scala - Sbt:如何为所有项目定义任务?

转载 作者:行者123 更新时间:2023-12-01 13:19:17 25 4
gpt4 key购买 nike

我希望能够为我的 sbt.build 中的所有项目定义一个任务。 :

lazy val project1 = project.in(`.` /  "project1)
...
lazy val project2 =
...

lazy val upload = taskKey[Unit]("upload a config file from project to server)
upload := {
val file = baseDirectory.value / "config.json"
...
}

问题是这个定义只有在我调用 sbt upload 时才有效。 ,但我希望能够为每个子项目调用它: sbt project1/uploadsbt project2/upload .

有没有办法做到这一点,而不使用 inputKey ?

最佳答案

Organizing the build :

For more advanced users, another way of organizing your build is to define one-off auto plugins in project/*.scala. By defining triggered plugins, auto plugins can be used as a convenient way to inject custom tasks and commands across all subprojects.



项目/上传插件.scala

package something

import sbt._
import Keys._

object UploadPlugin extends AutoPlugin {
override def requires = sbt.plugins.JvmPlugin
override def trigger = allRequirements

object autoImport {
val upload = taskKey[Unit]("upload a config file from project to server")
}
import autoImport._

override lazy val projectSettings = Seq(
upload := {
val n = name.value
println(s"uploading $n..")
}
)
}

生成.sbt

使用方法如下:
ThisBuild / organization := "com.example"
ThisBuild / scalaVersion := "2.12.5"
ThisBuild / version := "0.1.0-SNAPSHOT"

lazy val root = (project in file("."))
.aggregate(project1, project2)
.settings(
name := "Hello"
)

lazy val project1 = (project in file("project1"))

lazy val project2 = (project in file("project2"))
build.sbt不用提 UploadPlugin ,因为它是一个触发插件。从 shell 你可以调用:
sbt:Hello> project1/upload
uploading project1..
[success] Total time: 0 s, completed Jul 20, 2018
sbt:Hello> project2/upload
uploading project2..
[success] Total time: 0 s, completed Jul 20, 2018

关于scala - Sbt:如何为所有项目定义任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51376982/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com