如何从libraryDependencies启用编译器插件?

时间:2014-07-15 10:01:06

标签: sbt

我在库依赖项中有一个编译器插件,并希望启用它。像

这样的东西
autoCompilerPlugins := true

libraryDependencies += compilerPlugin(update.value.allModules.find(_.name contains "continuations-plugin").get)

scalacOptions += "-P:continuations:enable"

给出

/Users/luc/scala/release-sanity-check/build.sbt:20: error: A setting cannot depend on a task
libraryDependencies += compilerPlugin(update.value.allModules.find(_.name contains "continuations-plugin").get)
                                  ^

我可以使用自定义任务执行此操作吗?

val addContinuationsPlugin = taskKey[Unit]("Add continuations plugin")

addContinuationsPlugin := {
  val plugin = update.value.allModules.find(_.name contains "continuations-plugin")
  // add plugin?
}

有问题的存储库:https://github.com/scala/scala-dist-smoketest

3 个答案:

答案 0 :(得分:3)

你可能想要这样的东西(见sbt API

scalacOptions ++= {
  val compileConfig = update.value.configurations.find(_.configuration == "compile").get
  val pluginModule = compileConfig.modules.find(_.module.name contains "continuations-plugin").get
  val pluginFile = pluginModule.artifacts.head._2
  Seq(s"-Xplugin:${pluginFile.getCanonicalPath}", "-P:continuations:enable")
}

compilePlugin方法旨在简化您在使用Ivy解析插件时的任务,并且您可以正确执行此操作。

选项#2是尝试确保它们是您的项目与项目之间的传递compiler-plugin->compiler-plugin配置依赖关系链,您可以在其中发现continuation插件(最终会有compiler-plugin->default(compile)链接)。但是,如果没有看到你的依赖树,我就不能对选项#2提出建议(这是一种更健壮的方法)。

答案 1 :(得分:2)

这是非常棘手的,我不知道我是否可以推荐使用这种技术,而不仅仅是Typesafe的烟雾测试,但这里就是这样。 (不要在家里试试。)

正如您和Jacek所说,libraryDependencies以及插件的版本必须在构建的编译时静态知道。但是,由于可以使用元构建构建构建,因此我们可以将其视为元构建的运行时。我在回答How to share version values between project/plugins.sbt and project/Build.scala?

时已经证明了这一点

因此,使用sbt-buildinfo,我们可以将元构建的任务值提升为正确构建的设置值。接下来的问题是我们如何在元构建中找到continuation插件的版本。同样,这是一个花哨的技巧,但我们可以在元构建中制作虚假的常春藤配置,在那里添加scala-dist,并从externalDependencyClasspath in BogusConfig中删除版本号。更改也会以#1的形式发送。

项目/项目/ buildinfo.sbt

addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.3.2")

项目/项目/ Dependencies.scala

package metabuild

import sbt._

object Dependencies {
  def scalaDistVersion = sys.props("project.version")
}

object Configs {
  lazy val BogusConfig = config("bogus")
}

项目/ plugins.sbt

import metabuild.Dependencies._
import metabuild.Configs._

lazy val continuationPluginVersion = taskKey[String]("version of the continuation plugin")

lazy val build = (project in file(".")).
  configs(BogusConfig).
  settings(inConfig(BogusConfig)(Defaults.configSettings): _*).
  settings(buildInfoSettings: _*).
  settings(addSbtPlugin("com.typesafe.sbt" % "sbt-git" % "0.6.4"): _*).
  settings(
    resolvers += "jgit-repo" at "http://download.eclipse.org/jgit/maven",
    libraryDependencies += "org.scala-lang" % "scala-dist" % scalaDistVersion % BogusConfig,
    sourceGenerators in Compile <+= buildInfo,
    buildInfoKeys := Seq[BuildInfoKey](
      "scalaDistVersion" -> scalaDistVersion,
      continuationPluginVersion
    ),
    buildInfoPackage := "metabuild",
    continuationPluginVersion := {
        val bogusClasspath = (externalDependencyClasspath in BogusConfig).value
        val f = (bogusClasspath find { _.data.getName contains "continuations-plugin" }).get.data
        f.getName.replaceAllLiterally("scala-continuations-plugin_" + scalaDistVersion + "-", "").replaceAllLiterally(".jar", "")
    }
  )

build.sbt

import metabuild.BuildInfo._

// versionWithGit

version := scalaDistVersion

scalaVersion := version.value

libraryDependencies += "org.scala-lang" % "scala-dist" % version.value

autoCompilerPlugins := true

libraryDependencies +=
  compilerPlugin("org.scala-lang.plugins" % ("scala-continuations-plugin_" + version.value) % continuationPluginVersion)

scalacOptions += "-P:continuations:enable"

答案 2 :(得分:1)

“我在库依赖项中有一个编译器插件,并希望添加启用它。”

在sbt官方文档中关注Compiler Plugin Support后,您应该在build.sbt中启用对编译器插件的支持,如下所示:

autoCompilerPlugins := true

然后使用addCompilerPlugin将插件添加为libraryDependencies范围内的依赖项(plugin->default(compile))。

addCompilerPlugin("org.scala-tools.sxr" %% "sxr" % "0.3.0")

通过设置,您可以设置插件:

scalacOptions :=
    scalacOptions.value :+ ("-Psxr:base-directory:" + (scalaSource in Compile).value.getAbsolutePath)
相关问题