gradle - 使用gradle-git-properties插件在Kotlin DSL中进行GString惰性评估

标签 gradle gradle-kotlin-dsl

我在此插件上使用了Gradle 6.2.2:com.gorylenko.gradle-git-properties(2.2.2版本)。我正在尝试将以下代码段“翻译”为Kotlin DSL:

gitProperties {
  extProperty = "gitProps" // git properties will be put in a map at project.ext.gitProps
}

shadowJar {
  manifest {
    attributes(
      "Build-Revision": "${ -> project.ext.gitProps["git.commit.id"]}"  // Uses GString lazy evaluation to delay until git properties are populated
    )
  }
}

...但这是我到目前为止提出的:

gitProperties {
  extProperty = "gitProps"
  keys = listOf("git.branch", "git.build.host", "git.build.version", "git.commit.id", "git.commit.id.abbrev",
      "git.commit.time", "git.remote.origin.url", "git.tags", "git.total.commit.count")
}

tasks {
  withType<ShadowJar> {
    manifest.attributes.apply {
      put("Build-Revision", "${project.ext.properties["git.commit.id"]}")
    }
  }
}

我无法弄清楚在Kotlin DSL中是否可以使“GString惰性评估”部分正常工作,也不知道gitProps映射如何适合于此。最终,这种方法(我知道这是部分错误的)返回了null。有任何想法吗?

最佳答案

下面的Kotlin语法对我有用:

put("Build-Revision", object {
  override fun toString():String = (project.extra["gitProps"] as Map<String, String>)["git.commit.id"]!!
})

关于gradle - 使用gradle-git-properties插件在Kotlin DSL中进行GString惰性评估,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60743468/

相关文章:

gradle - 如何在Gradle中使用飞行 channel ?第一次迁移取决于Gradle构建

android - java.util.zip.ZipException : duplicate entry: android/support/v7/appcompat/R$anim. 类

gradle - 插件如何提供扩展任务?

gradle - 如何在 Gradle Kotlin DSL 中定义 Jacoco 报告聚合?

java - gradle bootRun : application fail to start with classpath

java - Hystrix仪表板

android - Gradle KTS 构建文件

Android 和 Fabric (Crashlytics) 插件总是生成一个 UUID (Gradle Kotlin DSL)

spring - 在将Spring Boot项目迁移到Gradle Kotlin DSL中摆脱警告

gradle - 如何声明 Gradle 自定义任务的依赖项?