为什么 sbt 报告自动插件的"error: '.' expected but eof found."?



我试图创建一个具有新AutoPlugin功能的sbt插件,但失败了。

所有代码都在这里:

build.sbt

sbtPlugin := true
name := "my-sbt-plugin"
version := "0.1.0"
organization := "test20140913"

项目/MySbtPlugin.scala

import sbt._
object MySbtPlugin extends AutoPlugin {
  object autoImport {
    lazy val hello = taskKey[Unit]("hello task from my plugin")
  }
  import autoImport._
  val helloSetting = hello := println("Hello from my plugin")
  override def projectSettings = Seq(
    helloSetting
  )
}

项目/构建.scala

import sbt._
object MySbtPluginBuild extends Build {
  lazy val root = project.in(file("."))
  root.enablePlugins(MySbtPlugin)
}

当我在上面运行sbt时,它会报告一个错误:

[info] Done updating.
[info] Compiling 2 Scala sources to /myplugin/project/target/scala-2.10/sbt-0.13/classes...
/sbttest/myplugin/build.sbt:0: error: '.' expected but eof found.
import _root_.sbt.plugins.IvyPlugin, _root_.sbt.plugins.JvmPlugin, _root_.sbt.plugins.CorePlugin, _root_.sbt.plugins.JUnitXmlReportPlugin, MySbtPlugin

回购

您可以克隆它:https://github.com/freewind/my-sbt-plugin,然后运行./sbt以再现它

sbt中的

Project是不可变的,即所有方法都会转换它。sbt会反射性地查找您在值上定义的项目。

基本上:

项目/构建.scala

import sbt._
object MySbtPluginBuild extends Build {
  lazy val root = project.in(file("."))
  // here you're constructing a new project instance which is ignored.
  root.enablePlugins(MySbtPlugin)
}

你所要做的就是将你的项目DSL链接在一起,并将结果分配到val中,这是sbt唯一注意的事情:

import sbt._
object MySbtPluginBuild extends Build {
  lazy val root = project.in(file(".")).enablePlugins(MySbtPlugin)
}

最后,我修复了插件,您可以在这里看到更新的代码:https://github.com/freewind/my-sbt-plugin

问题中有几个问题:

  1. 不需要project/build.scalaenablePlugins的部分只需要添加到使用该插件的项目中,而不应该添加到插件项目本身中。

  2. 对于sbt 0.13.5(我在问题中使用过),插件必须在包内,不能是顶级插件。此问题已在sbt 0.13.6 中修复

  3. 插件无法自动启用,除非我们在插件中添加以下行:

    override def trigger = allRequirements
    

    否则(或者不覆盖trigger),我们必须将中build.sbtBuild.scala中的enablePlugins部分添加到使用此插件的项目中。

  4. 重要提示:如果您在开发过程中创建了另一个项目来本地尝试该插件,则需要在运行sbt之前删除target目录(例如target/project/target)。否则你会有很多奇怪的问题。

最新更新