无法使用sbt.AutoPlugins添加编译依赖项

时间:2014-11-06 02:03:33

标签: scala sbt sbt-plugin

我正在尝试构建一个自动设置scalariform首选项的插件。

我的插件的build.sbt:

name := "my-scalariform"

organization := "com.my"

version := "1.0-SNAPSHOT"

sbtPlugin := true

scalacOptions ++= Seq("-feature", "-deprecation", "-unchecked")

addSbtPlugin("com.typesafe.sbt" % "sbt-scalariform" % "1.3.0")

我的初始插件设计:

package com.my.plugins

import com.typesafe.sbt.SbtScalariform
import com.typesafe.sbt.SbtScalariform.{
  ScalariformKeys,
  scalariformSettings
}
import sbt.AutoPlugin
import sbt.{ Compile, Test }
import sbt.Keys.{ compile, compileInputs }
import scalariform.formatter.preferences.{
  DoubleIndentClassDeclaration,
  FormattingPreferences,
  IndentSpaces,
  IndentWithTabs,
  PreserveDanglingCloseParenthesis
}

object MyScalariformPlugin extends AutoPlugin {

  override def trigger = allRequirements

  lazy val formattingPreferences = {
    import scalariform.formatter.preferences._
    Seq(
      ScalariformKeys.preferences := FormattingPreferences()
        .setPreference(DoubleIndentClassDeclaration, true)
        .setPreference(IndentSpaces, 2)
        .setPreference(IndentWithTabs, false)
        .setPreference(PreserveDanglingCloseParenthesis, true)
    )
  }

  override lazy val projectSettings = scalariformSettings ++ formattingPreferences
}

当我将此插件添加到我的项目中时,我可以看到我的标量设置:

> scalariform-preferences
[info] FormattingPreferences(Map(DoubleIndentClassDeclaration -> true, IndentSpaces -> 2, IndentWithTabs -> false, PreserveDanglingCloseParenthesis -> true))

然而,compileInputs缺少标量格式依赖:

> inspect compile:compile::compileInputs
...
[info] Defined at:
[info]  (sbt.Defaults) Defaults.scala:792
[info] Dependencies:
[info]  compile:compile::incCompileSetup
[info]  compile:compile::streams
[info]  compile:compile::dependencyClasspath
[info]  compile:compile::compileOrder
[info]  compile:compile::scalacOptions
[info]  compile:compile::classDirectory
[info]  compile:compile::javacOptions
[info]  compile:compile::sourcePositionMappers
[info]  compile:compile::compilers
[info]  compile:compile::sources
[info]  compile:compile::maxErrors
[info] Reverse dependencies:
[info]  compile:compile
...

如果我在我的插件中明确添加了标量command overrides作为值,然后将其显式添加到我的项目中,我会得到正确的依赖项:

lazy val commandSettings = Seq(
    compileInputs in (Compile, compile) <<= (compileInputs in (Compile, compile)) dependsOn (ScalariformKeys.format in Compile),
    compileInputs in (Test, compile) <<= (compileInputs in (Test, compile)) dependsOn (ScalariformKeys.format in Test)
  )

依赖关系:

> inspect compile:compile::compileInputs
...
[info] Defined at:
[info]  (sbt.Defaults) Defaults.scala:792
[info]  (com.my.plugins.MyScalariformPlugin) MyScalariformPlugin.scala:22
[info] Dependencies:
[info]  compile:compile::incCompileSetup
[info]  compile:compile::streams
[info]  compile:compile::dependencyClasspath
[info]  compile:scalariformFormat
[info]  compile:compile::compileOrder
[info]  compile:compile::scalacOptions
[info]  compile:compile::classDirectory
[info]  compile:compile::javacOptions
[info]  compile:compile::sourcePositionMappers
[info]  compile:compile::compilers
[info]  compile:compile::sources
[info]  compile:compile::maxErrors
[info] Reverse dependencies:
[info]  compile:compile
...

我尝试使用autoImport自己指定此依赖项,但这会导致错误:

  object autoImport {
    lazy val commandSettings = Seq(
      compileInputs in (Compile, compile) <<= (compileInputs in (Compile, compile)) dependsOn (ScalariformKeys.format in Compile),
      compileInputs in (Test, compile) <<= (compileInputs in (Test, compile)) dependsOn (ScalariformKeys.format in Test)
    )
  }

  import autoImport._

错误:

[error] References to undefined settings:
[error]
[error]   */test:compile::compileInputs from */test:compile::compileInputs ((com.my.plugins.MyScalariformPlugin.autoImport) MyScalariformPlugin.scala:24)
[error]      Did you mean test:compile::compileInputs ?
[error]
[error]   */test:scalariformFormat from */test:compile::compileInputs ((com.my.plugins.MyScalariformPlugin.autoImport) MyScalariformPlugin.scala:24)
[error]      Did you mean test:scalariformFormat ?
[error]
[error]   */compile:compile::compileInputs from */compile:compile::compileInputs ((com.my.plugins.MyScalariformPlugin.autoImport) MyScalariformPlugin.scala:23)
[error]      Did you mean compile:compile::compileInputs ?
[error]
[error]   */compile:scalariformFormat from */compile:compile::compileInputs ((com.my.plugins.MyScalariformPlugin.autoImport) MyScalariformPlugin.scala:23)
[error]      Did you mean compile:scalariformFormat ?
[error]

1 个答案:

答案 0 :(得分:1)

将此行添加到MyScalariformPlugin(不要问我为什么:))

override def requires = plugins.JvmPlugin