Gradle processResources - 文件包含$ character

mat*_*sev 4 gradle spring-boot

如何gradle processResources$不转义文件的情况下对包含字符$的文件执行?


我有一些静态html文件位于Spring Boot参考文档/resources/static建议的文件夹中.但是,当我尝试执行时,Gradle会抛出异常gradle processResources

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>
Run Code Online (Sandbox Code Playgroud)

据我所知,问题出现是因为其中一个$静态文件中$有一个字符,并且在处理资源时是表达式的保留字符.


建议的解决方案:

  1. 是的,转义$with \$(如堆栈跟踪中的建议)有效,但如果其他选项可用,我宁愿不更改html文件.
  2. 尝试从进程资源中排除该文件会导致问题消失,但会产生副作用:排除文件被复制:

    configure(tasks.processResources) {
        exclude 'static/dollar.html'
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 我还看到你可以过滤处理过的资源.我想这就是我想做的但我没有找到"忽略$ filter",有没有?

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


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

<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
    <div>Dollar $</div>
</body>
Run Code Online (Sandbox Code Playgroud)

mat*_*sev 14

JB Nizet的评论提供了宝贵的见解.问题确实是由于使用expand()(虽然它不是立即可见,因为它位于allProjects()父项目的脚本中).首先expand()添加的原因是希望在文件中填充info.build.*属性application.properties(以便它们可以通过Spring Boot的信息端点获得).


解决方案:filesMatching()仅用于expand()选定的文件.以下代码段解决了与Spring Boot相关的特定问题:

processResources {
    filesMatching('application.properties') {
        expand(project.properties)
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 如果属性键包含点char.例如`abcd = yes`如何绑定它?我遇到异常`groovy.lang.MissingPropertyException:没有这样的属性` (3认同)