【问题标题】:Gradle processResources - file contains $ characterGradle processResources - 文件包含 $ 字符
【发布时间】:2016-02-15 05:47:42
【问题描述】:

如何对包含$ 字符的文件执行gradle processResources 而不转义文件中的$


按照Spring Boot reference docs 的建议,我有一些位于/resources/static 文件夹的静态html 文件。但是,当我尝试执行gradle processResources 时,Gradle 会抛出异常

Caused by: org.gradle.api.GradleException: 
Could not copy file '[...]/src/main/resources/static/dollar.html' 
to '[...]/build/resources/main/static/dollar.html'.
[...]
Caused by: groovy.lang.GroovyRuntimeException: 
Failed to parse template script (your template may contain an error 
or be trying to use expressions not currently supported): startup failed:
SimpleTemplateScript7.groovy: 1: illegal string body character after dollar sign;
solution: either escape a literal dollar sign "\$5" 
or bracket the value expression "${5}" @ line 1, column 10.
out.print("""<!DOCTYPE html>

据我了解,出现问题是因为其中一个静态文件中有一个$ 字符,而$ 是处理资源时表达式的保留字符。


建议的解决方案:

  1. 是的,用\$ 转义$(如stacktrace 中所建议的)是可行的,但如果有其他选项可用,我宁愿不更改html 文件。
  2. 试图从进程资源中排除文件会导致问题消失,但也有将文件排除在复制之外的副作用:

    configure(tasks.processResources) {
        exclude 'static/dollar.html'
    }
    
  3. 我还看到您可以过滤已处理的资源。我想这是我想做的,但我没有找到“忽略 $ 过滤器”,有吗?

    configure(tasks.processResources) {
        filesMatching('static/dollar.html') {
            filter = ???
        }
    }
    
  4. 其他建议?


导致问题的dollar.html文件可以简化为:

<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
    <div>Dollar $</div>
</body>

【问题讨论】:

  • processResources 通常只是复制资源,而不做任何其他事情。您可能正在使用 expand (docs.gradle.org/current/dsl/…) 将 ${someProp} 占位符替换为值。向我们展示您的构建文件。
  • @JBNizet 您的假设是正确的,请参阅下面的answer。谢谢!

标签: gradle spring-boot


【解决方案1】:

JB Nizet's comment 提供了宝贵的见解。问题确实是由于使用了expand()(尽管由于它位于父项目的allProjects() 脚本中而不立即可见)。首先添加expand() 的原因是希望在application.properties 文件中填充info.build.* 属性(以便它们可以通过Spring Boot 的info endpoint 获得)。


解决方案:使用filesMatching() 仅对选定的文件使用expand()。下面的sn-p解决了Spring Boot相关的具体问题:

processResources {
    filesMatching('application.properties') {
        expand(project.properties)
    }
}

【讨论】:

  • 如果属性键包含点字符。 ega.b.c.d=yes怎么绑定?我遇到了例外groovy.lang.MissingPropertyException: No such property
  • 我在:root:subprojectA 类路径中有我的application.ymlfileMatching 读取什么路径?另外,如果 YAML 配置有多个配置文件,我应该写什么?
【解决方案2】:

仅供参考:

您也可以将 $ 写为 HTML 实体:&amp;dollar;

【讨论】:

    猜你喜欢
    • 2017-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-04
    • 1970-01-01
    相关资源
    最近更新 更多