Search code examples
gradlegroovyantbuild.gradlegradlew

Try to ExpandProperties in filesMatching in my gradle build


I have the following gradle build

./build.gradle

defaultTasks 'build'

task build(type: Copy){

    // read the properties from the versions.properties file
    Properties props = new Properties()
    props.load(new FileInputStream("../versions.properties"))
    props.each() { k, v ->
        ant.properties[k] = v
    }

    // copy files and replace placeholders
    from('ansible'){
        filesMatching('**/*.ini') {
            filter(org.apache.tools.ant.filters.ExpandProperties, project: ant.project)
        }
        filesMatching('**/*.yml') {
            println it
            filter(org.apache.tools.ant.filters.ExpandProperties, project: ant.project)
        }
        filesMatching('**/*.xml') {
            println it
            filter(org.apache.tools.ant.filters.ExpandProperties, project: ant.project)
        }
    }

    into 'build/pkg/ansible' 

    includeEmptyDirs = true

}

I want to replace all "${variableName}" with the value defined in ../versions.properties for that variable in all the *.xml, *.ini and *.yml files and copy all files to a new folder.

For example: ../versions.properties

versionA=1.3
versionB=2.3

./ansible/example-file.yml

replace this version. => ${versionA}

expected output in ./build/pkg/ansible/example-file.yml

replace this version. => 1.3

The result of the above build is that all files are copied to the right locations, but no property replacement has been made. :-(

Where's my error?

Cheers, d.


Solution

  • It actually does work as expcected. I missed that the "copy" task of gradle does not overwrite as default.

    Specifying the task as follows, solved my problem:

    task build(type: Copy, overwrite: true){
    ...
    }
    

    Cheers, d.