从sbt中的AutoPlugin生成资源

时间:2015-12-03 21:19:02

标签: sbt sbt-plugin

我创建了以下插件,就像之前的插件一样......

/**
 * This plugin automatically generates a version number based on the configured
 * minor version and today's date and time.
 */
object DateVersionPlugin extends AutoPlugin {
  //override def trigger = allRequirements

  def dateFormat (fmt : String) =
    new java.text.SimpleDateFormat(fmt).format(
      new java.util.Date()
    )

  def versionNumber (majorVersion      : String,
                     versionTrimFront  : Int,
                     versionDateFormat : String) =
    "%s.%s".format(
      majorVersion, dateFormat(versionDateFormat).substring(versionTrimFront)
    )


  /**
   * Defines all settings/tasks that get automatically imported,
   * when the plugin is enabled
   */
  object autoImport {
    /**
     * The number of values to trim off the front of the date string.
     *
     * This is used to achieve a date string which doesn't include the
     * present millenium.  The century, a stretch, can be imagined as
     * conceivable - but few civilizations have lasted multiple millennia.
     */
    lazy val versionTrimFront    = settingKey[Int]("Number of characters to remove from front of date")

    /**
     * The format to use for generating the date-part of this version number.
     */
    lazy val versionDateFormat   = settingKey[String]("The date format to use for versions")

    /**
     * The major version to place at the front of the version number.
     */
    lazy val versionMajor        = settingKey[String]("The major version number, default 0")

    /**
     * The filename of the generated resource.
     */
    lazy val versionFilename     = settingKey[String]("The filename of the file to generate")

    /**
     * The name of the property to place in the version number.
     */
    lazy val versionPropertyName = settingKey[String]("The name of the property to store as version")

    /**
     * Generate a version.conf configuration file.
     *
     * This task generates a configuration file of the name specified in the
     * settings key.
     */
    lazy val generateVersionConf = taskKey[Seq[File]]("Generates a version.conf file.")
  }

  import autoImport._

  /**
   * Provide default settings
   */
  override def projectSettings: Seq[Setting[_]] = Seq(
    versionFilename     := "version.conf",
    versionPropertyName := "version",
    versionDateFormat   := "YY.D.HHmmss",
    versionTrimFront    := 0,
    versionMajor        := "0",

    (version in Global) := versionNumber(versionMajor.value,
      versionTrimFront.value, versionDateFormat.value),

    generateVersionConf <<=
      (resourceManaged in Compile, version, versionFilename, versionPropertyName, streams) map {
        (dir, v, filename, propertyName, s) =>
          val file = dir / filename

          val contents = propertyName + " = \"" + v.split("-").head + "\""

          s.log.info("Writing " + contents + " to " + file)

          IO.write(file, contents)

          Seq(file)
      },

    resourceGenerators in Compile += generateVersionConf.taskValue
  )
}

generate-version-conf任务的行为符合要求,生成我正在寻找的文件。使用此插件的项目会按预期更新version设置。但是以下正在发生,我不清楚为什么:

  1. 配置文件不是由compile生成的。
  2. 配置文件未由package打包在jar中。
  3. 使用run任务时,配置文件不在类路径中。
  4. 注意我还尝试过十几种变体,我进一步尝试过:

      resourceGenerators in Compile <+= generateVersionConf
    

    根据我的理解,它应该导致或多或少相同的行为。

    检查此运行时属性,我看到一些设置已成功应用:

    > inspect version
    [info] Setting: java.lang.String = 0.15.338.160117
    [info] Description:
    [info]  The version/revision of the current module.
    [info] Provided by:
    [info]  */*:version
    [info] Defined at:
    [info]  (com.quantcast.sbt.version.DateVersionPlugin) DateVersionPlugin.scala:101
    [info] Reverse dependencies:
    [info]  *:isSnapshot
    [info]  *:generateVersionConf
    [info]  *:projectId
    [info] Delegates:
    [info]  *:version
    [info]  {.}/*:version
    [info]  */*:version
    [info] Related:
    [info]  */*:version
    

    然而,compile:resourceGenerators并非如此,这表明它仍然保留默认值。

    > inspect compile:resourceGenerators
    [info] Setting: scala.collection.Seq[sbt.Task[scala.collection.Seq[java.io.File]]] = List(Task(_))
    [info] Description:
    [info]  List of tasks that generate resources.
    [info] Provided by:
    [info]  {file:/home/scott/code/quantcast/play/sbt-date-version/sbt-test/}root/compile:resourceGenerators
    [info] Defined at:
    [info]  (sbt.Defaults) Defaults.scala:207
    [info]  (sbt.Defaults) Defaults.scala:208
    [info] Dependencies:
    [info]  compile:discoveredSbtPlugins
    [info]  compile:resourceManaged
    [info] Reverse dependencies:
    [info]  compile:managedResources
    [info] Delegates:
    [info]  compile:resourceGenerators
    [info]  *:resourceGenerators
    [info]  {.}/compile:resourceGenerators
    [info]  {.}/*:resourceGenerators
    [info]  */compile:resourceGenerators
    [info]  */*:resourceGenerators
    [info] Related:
    [info]  test:resourceGenerators
    

    我的问题是(现在我继续研究这个问题),是什么可以保持我对(编译中的generateResources)的更改不被应用?

1 个答案:

答案 0 :(得分:1)

如果这个插件需要JvmPlugin。这是因为JvmPlugin定义了设置依赖项。没有它,显然compile:resourceGenerators设置被默认值覆盖,将资源生成器集重新定义为Nil并从那里构建。

因此,解决方案是在AutoPlugin定义中包含以下行。

  override def requires = plugins.JvmPlugin