【问题标题】:Building Custom SBT plugin, getting error: object sbt is not a member of package com.typesafe构建自定义 SBT 插件,出现错误:对象 sbt 不是包 com.typesafe 的成员
【发布时间】:2016-02-23 23:46:46
【问题描述】:

tl;dr 我正在尝试让我的自定义插件 a) 自动提取 GitVersioning 插件,并且 b) 可以访问变量,例如 git.baseVersion。以我知道的最佳方式执行此操作会导致导入错误。

我正在尝试编写一个自定义插件,类似于one here

我的BaseSettingsPlugin.scala 看起来像:

package com.sp.sbt

import sbt.Keys._
import com.typesafe.sbt.GitVersioning
import sbt._


/**
 * Provides base settings for all of our projects
 */
object BaseSettingsPlugin extends AutoPlugin {

/**
 * Defines all settings/tasks that get automatically imported,
 * when the plugin is enabled
 */
  object autoImport {
    lazy val libraryVersions = settingKey[Map[Symbol, String]]("Common versions to be used for dependencies")
  }

  import autoImport._

  // allow the plug-in to be included automatically
  override def trigger: PluginTrigger = allRequirements

  override def requires = GitVersioning

  /**
   * Provide default settings
   */
  override def projectSettings: Seq[Setting[_]] = Seq(
    scalaVersion := "2.11.7",

    organization := "com.sp",

    libraryVersions := Map(
      'bijection          ->            "0.8.1",
      'elastic4s          ->            "1.7.4",
      'ficus              ->            "1.1.2",
      'finatra            ->            "2.1.0",
      'hikari             ->            "2.4.1",
      'h2                 ->            "1.4.189",
      'mysql              ->            "5.1.36",
      'redis              ->            "6.29.0",
      'scala              ->            scalaVersion.value,
      'scalatest          ->            "2.2.3",
      'scoverage          ->            "1.1.1",
      'slick              ->            "3.1.1",
      'slickJoda          ->            "2.1.0",
      'specs2             ->            "2.3.13",
      'swagger            ->            "0.3.0",
      'typesafe           ->            "1.3.0"
    )
  )
}

当我尝试构建自定义插件时,我收到以下错误消息:

[error] BaseSettingsPlugin.scala:10: object sbt is not a member of package com.typesafe
[error] import com.typesafe.sbt.GitVersioning
[error]                     ^
[error] BaseSettingsPlugin.scala:40: not found: value GitVersioning
[error]   override def requires = GitVersioning

第二条错误消息是意料之中的,因为导入语句失败。

如果我将import com.typesafe.sbt.GitVersioning 移动到我的build.sbt 文件中,我将不再收到导入错误,但我会继续收到第二个(未找到)错误。

我真正想做的是让它在我的自定义插件中包含以下代码块:

git.useGitDescribe := true, // if this is true, then it uses the version number suggested by $> git describe
  // if this is false, it combines the baseVersion and the commit hash of the current commit
git.baseVersion := "0.0.0",
git.gitUncommittedChanges := false,
git.gitTagToVersionNumber := {
  case VersionRegex(v,"") => Some(v)
  case VersionRegex(v,"SNAPSHOT") => Some(s"$v-SNAPSHOT")
  case VersionRegex(v,s) => Some(s"$v-$s-SNAPSHOT")
  case _ => None

}

另外,为了澄清,这里是build.sbt

// we hide the existing definition for setReleaseVersion to replace it with our own
import sbtrelease.ReleaseStateTransformations.{setReleaseVersion=>_,_}
import com.typesafe.sbt.GitVersioning

sbtPlugin := true

scalaVersion := "2.10.4"            // This overrides the scalaVersion from BaseSettingsPlugin

name := "base-settings"

libraryDependencies <++= libraryVersions { v => Seq(
  "org.scala-lang" % "scala-compiler" % v('scala),
  "org.scala-lang" % "scala-library" % v('scala),
  "com.typesafe" % "config" % v('typesafe)
)}

lazy val root = project
  .in(file("."))
  .enablePlugins(GitVersioning)
  .settings(packageSettings: _*)

【问题讨论】:

  • 我认为你在 build.sbt 中缺少 sbtPlugin := true
  • @m-z 我在build.sbt 中有sbtPlugin := true,但没有发布文件的内容。我已经更新了。
  • (以防万一)您是否真的将插件作为依赖项添加到您的 sbt 或插件配置中?

标签: scala sbt


【解决方案1】:

我需要确保 GitVersioning 插件不仅可用于构建过程,而且还可用于我的插件。

build.sbt,我补充说:

addSbtPlugin("com.typesafe.sbt" % "sbt-git" % "0.8.5")

执行此操作后,sbt-git 可用于我的插件。

【讨论】:

  • 你有一个专用文件可以在项目目录中添加名为plugins.sbt的插件
猜你喜欢
  • 2020-10-27
  • 1970-01-01
  • 2020-04-20
  • 2011-12-06
  • 2011-03-28
  • 2021-03-31
  • 1970-01-01
  • 2018-07-24
  • 2014-12-05
相关资源
最近更新 更多