简体   繁体   English

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

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

I have implemented a simple sbt plugin which I published locally to my.ivy repository.我已经实现了一个简单的 sbt 插件,我在本地发布到 my.ivy 存储库。 I wanted to use this plugin on one of my project for which I added it in my plugins.sbt file.我想在我的一个项目中使用这个插件,我将它添加到我的 plugins.sbt 文件中。 I then tried to run this from the console and it failed with the following error:然后我尝试从控制台运行它,但失败并出现以下错误:

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]  

   ^

As it can be seen that I'm trying to add a common scalafmt.conf file which I would publish as a plugin and include it from the individual projects scalafmt.conf file.可以看出,我正在尝试添加一个通用的 scalafmt.conf 文件,我会将其发布为插件并将其包含在各个项目的 scalafmt.conf 文件中。 To run this I just navigated to the project where I'm using this plugin and it failed.要运行它,我只是导航到我正在使用这个插件的项目,但它失败了。 Here is what I have in my plugin code:这是我的插件代码中的内容:

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")
      )
  }
}

And the build.sbt that builds this Plugin looks like this:构建此插件的 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"
  )

Any clues as to what I'm doing wrong?关于我做错了什么的任何线索?

EDIT: I'm now facing this error:编辑:我现在面临这个错误:

[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

Here is the generated.scalafmt-common.conf which I can see in my plugin:这是我可以在我的插件中看到的 generated.scalafmt-common.conf:

version = 3.6.1
maxColumn = 120

Here is how I managed to get this fixed:这是我设法解决此问题的方法:

I originally had the implementation located under project, but after moving it into src/main/scala/my/package ensured that when I built the plugin JAR, it was indeed packaged into the JAR file.我最初的实现位于项目下,但在将其移动到 src/main/scala/my/package 后确保当我构建插件 JAR 时,它确实被打包到 JAR 文件中。

I then had to add this plugin in the build.sbt of my project where I'm using it like:然后我不得不在我使用它的项目的 build.sbt 中添加这个插件,如下所示:

.enablePlugins(MyScalafmtPlugin)

This made sure that the PlugIn was referenced and it used the common scalafmt.conf file and started to format all the.scala files using the base conf.这确保了插件被引用并且它使用通用的 scalafmt.conf 文件并开始使用基本 conf 格式化所有 .scala 文件。

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

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