【发布时间】:2014-02-11 05:02:41
【问题描述】:
我写了一个 Gradle 插件,它的版本在它的构建脚本中指定。
这个插件有可能在有人使用它时知道它自己的版本吗? (即当它的apply(Project project) 方法被调用时)
【问题讨论】:
我写了一个 Gradle 插件,它的版本在它的构建脚本中指定。
这个插件有可能在有人使用它时知道它自己的版本吗? (即当它的apply(Project project) 方法被调用时)
【问题讨论】:
对于我的插件,我在构建期间将一个名为 Implementation-Version 的字段嵌入到 MANIFEST.MF 文件中。然后我在运行时读取该字段,方法是像这样访问包:
def pkg = MyPlugin.class.getPackage()
return pkg.implementationVersion
或者使用类似的帮助类:https://github.com/nebula-plugins/nebula-core/blob/master/src/main/groovy/nebula/core/ClassHelper.groovy#L16 从清单中获取任意字段。
【讨论】:
jar { manifest { attributes("Implementation-Version": version) } }
您也可以通过以下方式找到版本:
def selfVersion = project.buildscript.configurations.classpath.resolvedConfiguration.resolvedArtifacts.collect {
it.moduleVersion.id }.findAll { it.name == '<name of plugin>' }.first().version
【讨论】: