代码之家  ›  专栏  ›  技术社区  ›  queeg

Gradle模板处理

  •  0
  • queeg  · 技术社区  · 3 年前

    我有一个gradle版本,想让一些文本文件更具动态性,因为它们总是包含当前版本的版本号。一旦文件被视为模板,并且版本号作为变量来自gradle,这应该很容易实现。

    我试过使用 Aaron Nies' template plugin 像这样:

    plugins {
      id 'application'
      id "dev.anies.gradle.template" version "0.0.2"
    }
    ...
    tasks.register("template", TemplateTask) {
      data += [key: "value"]
      from('src/templates')
      into('build/templates')
    }
    

    但Gradle总是抱怨

    Could not get unknown property 'TemplateTask' for project ':app' of type org.gradle.api.Project.
    

    我以为我坚持了文档。这里可能出了什么问题?

    顺便说一句,我现在不必使用Freemarker模板。如果有人演示如何使用可以替换变量的内置模板引擎,我也会接受这个答案。

    0 回复  |  直到 3 年前
        1
  •  0
  •   queeg    3 年前

    好的,我开始使用Ant的ReplaceTokenFilter。毕竟,我在文档中找到了相关行:

    https://docs.gradle.org/current/userguide/working_with_files.html#sec:copying_files 示例36(在复制文件时过滤文件)使用ReplaceTokeFilter。但与我在网上找到的这么多例子不同,它也包含了这一行

    import org.apache.tools.ant.filters.ReplaceTokens
    

    这最终使它对我有用。

    至于FreeMarkerTemplates,我仍然不知道,但我的案子解决了。