首页 > 解决方案 > sbt 说“不是一个有效的命令”,但启用了插件

问题描述

我有一个名为 sonar 的插件,它是作为 AutoPlugin 开发和发布的:

object Sonar extends AutoPlugin {

  object autoImport {
    lazy val sonar = taskKey[Unit]("sonar")
  }

  import autoImport._

  override def trigger = allRequirements

  lazy val sonarTask = Def.task {

      <snip task code here which runs sonarqube scanner>

  }

}

然后我们有一个使用该插件的项目,plugins.sbt它看起来像这样:

resolvers ++= Seq(
  "Nexus Snapshot repo" at "url here"
)

credentials += Credentials(Path.userHome / ".ivy2" / ".credentials")

addSbtPlugin("packagename" % "sonar" % "1.0-SNAPSHOT")

像这样 build.sbt :

name := "hashing-library"

organization := "org name here"

scalaVersion := "2.12.6"
autoScalaLibrary := false
crossPaths := false


resolvers += "Nexus Release repo" at "https://nexusurl/content/repositories/releases/"

resolvers += "nexus Snapshot repo" at "https://nexusurl/content/repositories/snapshots/"

credentials += Credentials(Path.userHome / ".ivy2" / ".credentials")

// Publishing options:
publishMavenStyle := true
publishArtifact in Test := false
pomIncludeRepository := { x => false }

publishTo := {
  val nexus = "nexus url here"
  if (isSnapshot.value)
    Some("sonatype-snapshots" at nexus + "content/repositories/snapshots")
  else
    Some("sonatype-releases" at nexus + "content/repositories/releases")
}

当我尝试运行 sbt 插件时,它说插件已启用:

<snip output>
packagename.sonar.Sonar: enabled in hashingLibrary

那么为什么我的插件已启用,但我无法运行sbt sonar?当我这样做时,它说:

[info] Set current project to hashing-library (in build file:/home/work/hashing-library/)
[error] Not a valid command: sonar
[error] Not a valid project ID: sonar
[error] Expected ':'
[error] Not a valid key: sonar
[error] sonar
[error]   

(显然组织名称和网址已被删除以保护我的客户的机密性,但希望这不会影响我的问题!)

标签: sbt

解决方案


sonar您的插件缺少的部分是任务sonarTask任务实现之间的连接,即您需要在某处说键设置为实现的值。您通常通过覆盖插件中的项目设置来做到这一点:

override def projectSettings = Seq(
  sonar := sonarTask.value
)

推荐阅读