Eclipse: filtered resources generation for .project file

Gradle (yet?) lacks the functionality of generating the Eclipse filtered resource definitions from the build script. It would be awesome. Unfortunately it is not trivial. Example:

<id>org.eclipse.ui.ide.multiFilter</id>
<arguments>1.0-projectRelativePath-matches-false-false-build</arguments>

The implementation process must start by understanding the id and arguments nodes.

You can always modify .project file manually. My solution (excludes all folders): tasks[‘eclipse’].doLast {

def xml = new XmlParser().parse(new File(project.rootDir, ‘.project’))

xml.append(

new XmlParser().parseText("""

<filter>

<id>1421264300269</id>

<name></name>

<type>26</type>

<matcher>

<id>org.eclipse.ui.ide.multiFilter</id>

<arguments>1.0-name-matches-false-false-*</arguments>

</matcher>

</filter>

</filteredResources>""")

)

def pw = new PrintWriter(new FileWriter(new File(project.rootDir, ‘.project’)))

pw.println(’<?xml version="1.0" encoding="UTF-8"?>’)

def p = new XmlNodePrinter(pw)

p.preserveWhitespace = true

p.print(xml) }

Possibly better to use the withXml hook:

eclipse.project.file.withXml { XmlProvider -> provider
    def node = new XmlParser().parseText("""
        <filter>
            <id>1421264300269</id>
            <name></name>
            <type>26</type>
            <matcher>
                <id>org.eclipse.ui.ide.multiFilter</id>
                <arguments>1.0-name-matches-false-false-*</arguments>
            </matcher>
        </filter>
    """)
    def filteredResources = provider.asElement().getElementsByTagName("filteredResources")[0]
    filteredResources.appendChild(node)
}

How would you choose to generate the id value?

AFAIK it does not matter

I tried using values that are generated when I perform the exclude by hand, and Eclipse is complaining about it.

In fact, since the manually generated values looked very much like a milliseconds epoch time, I tried auto-generating the value using System.getCurrentTime, but I get the same problem:

Ah, i know this error. It is due to XML formatting. id line must look exactly like:

<id>1421264300269</id>

no spaces or CRLF.

p.preserveWhitespace = true

this line is IMPORTANT

Thanks. That was it. I had copied that line exactly, but I forgot there was another existing “eclipse.doLast” later that was re-writing it again without the preserveWhitespace. I fixed those other unrelated .project XmlNodePrinters and then the spacing was coming out correctly.