Excluding certain lines in a file from processResources

I currently have a Gradle project making use of processResources in order to expand certain identifiers in a file. The problem is, said file contains references to a couple of Java classes by package location, and one of these happens to be a static subclass.

my processResources block is as follows:

processResources {
    inputs.property "version", project.version

    filesMatching("info.json") {
        expand "version": project.version
    }
}

The file info.json contains the following:

{
  "schemaVersion": 1,
  ...
  "version": "${version}",
  ...
  "entrypoints": {
    "main": ["com.example.Entrypoint"],
    "client": ["com.example.Entrypoint$Client"],
  ...
}

When attempting to build using these, Gradle has problems dealing with the subclass reference as it mistakes it for a Groovy template expansion and hence cannot find a Client property to expand with.

How might this be worked around such that the subclass reference can be kept? My first instinct would be to try to exclude a certain line from being processed/expanded, though I’m open to suggestions on a more sustainable approach than that.

You can escape the $ using \$. Is that a workable solution?

Sorry for the late reply. Unfortunately, this seems to be an illegal escape sequence, so it isn’t actually workable.

Yes, it is an illegal escape sequence in json, but as a template file it is legal. The resulting json after processResources executes will not contain the \.

It’s not perfect, but it works. Unless that illegal escape in the template is causing you other problems, apart from IDEs highlighting it?

Here’s an alternative escaping that avoids introducing json errors:

    "client": [
      "com.example.Entrypoint<%='$'%>Client"
    ]
1 Like