簡體   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