gpt4 book ai didi

scala - SBT 项目中的条件设置

转载 作者:行者123 更新时间:2023-12-02 00:31:55 26 4
gpt4 key购买 nike

我在 SBT (1.1.6) 中有一个多模块项目。

我的示例模块定义:

lazy val `example` = (project in file("example"))
.settings(
scalaVersion := "2.11.12",
crossScalaVersions := Seq("2.10.7", "2.11.12"),
libraryDependencies ++= Seq( ... )
)

现在我的意图是禁用 2.11 jar 的发布,设置如下:

lazy val dontPublish: Seq[Def.Setting[_]] = Seq(
publish := {},
publishLocal := {},
publishM2 := {},
publishArtifact := false,
publishTo := None
).flatten

我可以阻止所有情况,所以我希望一些简单的事情,比如:

lazy val `example` = (project in file("example"))
.settings(
scalaVersion := "2.11.12",
crossScalaVersions := Seq("2.10.7", "2.11.12"),
(if (scalaBinaryVersion.value == "2.11") dontPublish else Seq.empty),
libraryDependencies ++= Seq( ... )
)

可以工作,但是...我得到一个错误:

/path/build.sbt:371: error: `value` can only be used within a task or setting macro, such as :=, +=, ++=, Def.task, or Def.setting.
(if (scalaBinaryVersion.value == "2.11") dontPublish else Seq.empty).flatten,
^
[error] sbt.compiler.EvalException: Type error in expression

问题:

如何根据条件(例如,Scala 版本)添加设置?

最佳答案

我最终改编了 https://gist.github.com/joescii/4431cb2185eddb699dfb79b9c910e333SBT 1.1.6

我的解决方案:

val neverPublish = dontPublishIf(scalaVersion)(_ => true)

def dontPublishIf[P](key: SettingKey[P])(p: P => Boolean): Seq[Def.Setting[_]] = {
Seq(
//@formatter:off
skip in publish := p(key.value),
skip in publishLocal := p(key.value),
skip in publishM2 := p(key.value)
//@formatter:on
) ++ dontPublishAssemblyIf(key)(p)
}

def isAssemblyArtifact(a: Artifact): Boolean =
a.classifier.contains("assembly")

def dontPublishAssemblyIf[P](key: SettingKey[P])(p: P => Boolean): Seq[Def.Setting[_]] = {
Seq(
// remove added assembly artifacts and publishTasks
//@formatter:off
artifacts := artifacts .value.filterNot { a => p(key.value) && isAssemblyArtifact(a) },
packagedArtifacts := packagedArtifacts.value.filterNot { case (a, _) => p(key.value) && isAssemblyArtifact(a) }
//@formatter:on
)
}

我很惊讶 SBT 真的这么难用,但在这次冒险中,我对它的内部结构有了更多的了解。

如果有人遇到类似问题,我会发帖。

关于scala - SBT 项目中的条件设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51523064/

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