gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:38:11 24 4
gpt4 key购买 nike

我正在尝试构建一个自动设置一组 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]

最佳答案

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

override def requires = plugins.JvmPlugin

关于scala - 无法使用 sbt.AutoPlugins 添加编译依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26770564/

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