Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle processResources - file contains $ character

How can you execute gradle processResources on files that contain $ characters without escaping the $ in the files?


I have some static html files located the /resources/static folder as suggested by the Spring Boot reference docs. However, when I try to execute gradle processResources, Gradle throws an exception

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>

As far as I understand, the problem occurs because there is a $ character in one of the static files and $ is a reserved character for expressions when processing resources.


Proposed solutions:

  1. Yes, escaping the $ with \$ (as suggested in the stacktrace) works, but I rather not change the html file if other options are available.
  2. Trying to exclude the file from process resources causes the problem to disappear, but has the side effect of also excluding the file from being copied:

    configure(tasks.processResources) {
        exclude 'static/dollar.html'
    }
    
  3. I have also seen that you can filter processed resources. I guess that this is what I would like to do but I have not found a "ignore $ filter", is there any?

    configure(tasks.processResources) {
        filesMatching('static/dollar.html') {
            filter = ???
        }
    }
    
  4. Other suggestions?


The dollar.html file that causes the problem can be simplified to:

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

like image 436
matsev Avatar asked Nov 13 '15 15:11

matsev


1 Answers

JB Nizet's comment provided valuable insight. The problem was indeed due to the usage of expand() (although not immediately visible since it was located in an allProjects() script in the parent project). The reason why expand() was added in the first place was the desire to populate the info.build.* properties in the application.properties file (so that they are available through Spring Boot's info endpoint).


Solution: Use filesMatching() to only expand() selected files. The following snippet solved the specific problem related to Spring Boot:

processResources {
    filesMatching('application.properties') {
        expand(project.properties)
    }
}
like image 168
matsev Avatar answered Oct 10 '22 02:10

matsev