如何将自定义独立插件的 build.gradle 属性应用到应用该插件的项目

How to apply build.gradle properties from custom standalone plugin to project that applies that plugin

我有很多项目使用相同的依赖项和插件等等。我准备了独立的 Gradle 插件以避免在所有项目中都这样做,但我遇到了一个问题。

假设我所有的项目都在使用 com.gorylenko.gradle-git-properties

我的插件能够将 git-properties 插件应用到应用我的插件的项目 - 这很好,但我还想为这个插件添加一些属性到目标项目,我的意思是这样的:

gitProperties {
  dateFormat = 'yyyy-MM-dd HH:mm:ss'
  dateFormatTimeZone = 'Europe/Warsaw'
  keys = [
          'git.branch',
          'git.commit.id.abbrev',
          'git.commit.time',
          'git.dirty',
          'git.commit.message.short'
  ]
}

问题:如何将此属性添加到目标 build.gradle?目标项目有可能重用插件 build.gradle 或其他东西的属性吗?

当您的插件配置相关项目并应用 com.gorylenko.gradle-git-properties 时,您将必须配置由底层插件贡献的扩展。

鉴于此插件注册了 GitPropertiesPluginExtension 类型的扩展,您需要访问它然后才能对其进行配置(Java 中的代码):

// After adding the plugin
GitPropertiesPluginExtension gitExtension = project.getExtensions().getByType(GitPropertiesPluginExtension.class);
gitExtension.keys = // Your list of keys