2015-12-03 143 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