【问题标题】:How to apply build.gradle properties from custom standalone plugin to project that applies that plugin如何将自定义独立插件中的 build.gradle 属性应用于应用该插件的项目
【发布时间】:2020-03-18 10:47:58
【问题描述】:

我有许多项目使用相同的依赖项和插件等。我准备了独立的 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 或其他东西的属性吗?

【问题讨论】:

    标签: gradle gradle-plugin


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-01
      • 1970-01-01
      • 2021-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-24
      相关资源
      最近更新 更多