将日志记录添加到设置键时,设置不能依赖于任务

时间:2019-02-08 04:01:37

标签: scala sbt

我正在更新fm-sbt-s3-resolver中具有sbt plugin的{​​{1}}。在明确添加所需设置作为问题的副作用方面,我已经取得了一些进展:

Logging from an sbt plugin

SettingKey

当我尝试使用object MyPlugin { override def requires = S3ResolverPlugin override def trigger = allRequirements override lazy val globalSettings = Seq( resolvers ++= repos, publishMavenStyle := true, S3ResolverPlugin.autoImport.s3CredentialsProvider := {bucket: String => new AWSCredentialsProviderChain( new EnvironmentVariableCredentialsProvider(), PropertyFilesCredentialProvider.create(bucket, streams.value.log) ) } ) } 添加logging时,sbt会引发错误:

streams.value.log

1 个答案:

答案 0 :(得分:0)

在这种情况下,您需要依赖设置sLog而不是任务streams

import sbt._
import sbt.Keys._

object Logs extends AutoPlugin {

  object autoImport {
    val settingLog = settingKey[Unit]("Uses setting sLog")
    val taskLog = taskKey[Unit]("Uses task streams log")
  }

  import autoImport._

  override def trigger = allRequirements

  override def projectSettings: Seq[Def.Setting[_]] = Seq(
    settingLog := { sLog.value.info("Logging on settings execution") },
    taskLog := { streams.value.log.info("Logging on task execution") }
  )
}

settingLog的不同之处在于它将在sbt启动时记录。

~/w/t/stackoverflow $ sbt
[info] Logging on settings execution

settingLog的重复调用将不再产生任何日志记录。但是taskLog会在您每次调用日志时打印日志