2015-12-03 101 views
2

我已經創建瞭如下的插件,像之前的那麼多的插件...產生從AutoPlugin資源SBT

/** 
* 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
  3. 當使用run任務時,配置文件不在類路徑中。

注意我也嘗試了十幾個變化這一點,我已經試過進一步:

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,這表明它仍然保持默認值。我的問題是(現在我已經繼續研究這些了),我的更改可以保持(編譯中的generateResources)被應用?

回答

1

如果這個插件需要JvmPlugin。這是因爲JvmPlugin定義了設置依賴關係。沒有它,顯然compile:resourceGenerators設置正在被默認設置所覆蓋,重新定義資源生成器組爲Nil並從那裏建設。

所以解決方法是在AutoPlugin定義中包含以下行。

override def requires = plugins.JvmPlugin