繁体   English   中英

SBT 本地发布的插件包含在项目中失败并报错

[英]SBT Plugin Published Locally Fails with an Error When Included in a Project

我已经实现了一个简单的 sbt 插件,我在本地发布到 my.ivy 存储库。 我想在我的一个项目中使用这个插件,我将它添加到我的 plugins.sbt 文件中。 然后我尝试从控制台运行它,但失败并出现以下错误:

me@me-InfinityBook-S-14-v5:~/scala-projects/open-electrons/oscp-scala$ sbt scalafmt
[info] welcome to sbt 1.6.2 (Private Build Java 1.8.0_352)
[info] loading settings for project oscp-scala-build from plugins.sbt ...
[info] loading project definition from /home/me/scala-projects/open-electrons/oscp-scala/project
[info] loading settings for project oscpScala from build.sbt ...
[info] set current project to oscpScala (in build file:/home/me/scala-projects/open-electrons/oscp-scala/)
[warn] there's a key that's not used by any other settings/tasks:
[warn]  
[warn] * ThisBuild / publishMavenStyle
[warn]   +- /home/me/scala-projects/open-electrons/oscp-scala/build.sbt:13
[warn]  
[warn] note: a setting might still be used by a command; to exclude a key from this `lintUnused` check
[warn] either append it to `Global / excludeLintKeys` or call .withRank(KeyRanks.Invisible) on the key
[error] Not a valid command: scalafmt (similar: last)
[error] Not a valid project ID: scalafmt (similar: oscpScala)
[error] Expected ':'
[error] Not a valid key: scalafmt (similar: scalaHome, scalaArtifacts, scalaInstance)
[error] scalafmt
[error]  

   ^

可以看出,我正在尝试添加一个通用的 scalafmt.conf 文件,我会将其发布为插件并将其包含在各个项目的 scalafmt.conf 文件中。 要运行它,我只是导航到我正在使用这个插件的项目,但它失败了。 这是我的插件代码中的内容:

import sbt._
object MyScalafmtPlugin extends AutoPlugin {
  override def trigger = allRequirements
  override def requires = plugins.JvmPlugin
  override def buildSettings: Seq[Def.Setting[_]] = {
    SettingKey[Unit]("scalafmtGenerateConfig") :=
      IO.write(
        // writes to file once when build is loaded
        file(".scalafmt-common.conf"),
        ("version = 3.6.1\n" +
          "maxColumn = 120"
          ).stripMargin.getBytes("UTF-8")
      )
  }
}

构建此插件的 build.sbt 如下所示:

lazy val root = (project in file(".")).
  settings(
    name := "openelectrons-scalafmt-common-sbt-plugin",
    version := "0.0.1",
    organization := "com.openelectrons",
    scalaVersion := "2.12.17",
    sbtPlugin := true,
    sbtVersion := "1.6.2"
  )

关于我做错了什么的任何线索?

编辑:我现在面临这个错误:

[error] org.scalafmt.sbt.ScalafmtSbtReporter$ScalafmtSbtError: scalafmt: missing setting 'version'. To fix this problem, add the following line to .scalafmt.conf: 'version=3.3.0'. [/home/me/scala-projects/open-electrons/oscp-scala/.scalafmt.conf]
[error] org.scalafmt.sbt.ScalafmtSbtReporter$ScalafmtSbtError: scalafmt: missing setting 'version'. To fix this problem, add the following line to .scalafmt.conf: 'version=3.3.0'. [/home/me/scala-projects/open-electrons/oscp-scala/.scalafmt.conf]
[error] (oscp-messages / Compile / scalafmt) org.scalafmt.sbt.ScalafmtSbtReporter$ScalafmtSbtError: scalafmt: missing setting 'version'. To fix this problem, add the following line to .scalafmt.conf: 'version=3.3.0'. [/home/me/scala-projects/open-electrons/oscp-scala/.scalafmt.conf]
[error] (oscp-j-api / Compile / scalafmt) org.scalafmt.sbt.ScalafmtSbtReporter$ScalafmtSbtError: scalafmt: missing setting 'version'. To fix this problem, add the following line to .scalafmt.conf: 'version=3.3.0'. [/home/me/scala-projects/open-electrons/oscp-scala/.scalafmt.conf]
[error] Total time: 0 s, completed Dec 30, 2022 10:10:21 PM

这是我可以在我的插件中看到的 generated.scalafmt-common.conf:

version = 3.6.1
maxColumn = 120

这是我设法解决此问题的方法:

我最初的实现位于项目下,但在将其移动到 src/main/scala/my/package 后确保当我构建插件 JAR 时,它确实被打包到 JAR 文件中。

然后我不得不在我使用它的项目的 build.sbt 中添加这个插件,如下所示:

.enablePlugins(MyScalafmtPlugin)

这确保了插件被引用并且它使用通用的 scalafmt.conf 文件并开始使用基本 conf 格式化所有 .scala 文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM