Gradle replace resource file with WAR plugin

gradle, resources, war

Solution

If you can't make the decision at runtime (which is the recommended best practice for dealing with environment-specific configuration), `eachFile` may be your best bet:

war {
    rootSpec.eachFile { details -> 
        if (details.name == "database.properties") {
            details.exclude()
        } else if (details.name == "database.properties.production") {
            details.name = "database.properties"
        }
    }
}

PS: Gradle 1.7 adds `filesMatching(pattern) { ... }`, which may perform better than `eachFile`.

Problem

I'm trying to replace a resource file in my WAR plugin task with Gradle. Basically I have two resource files: ``` database.properties database.properties.production ``` What I want to achieve is replace 'database.properties' with 'database.properties.production' in the final WAR file under WEB-INF/classes. I tried a lot of things but the most logical to me was the following which does not work: ``` war { webInf { from ('src/main/resources') { exclude 'database.properties' rename('database.properties.production', 'database.properties') into 'classes' } } } ``` But this causes all other resource files to be duplicate, including a duplicate database.properties (two different files with same name) and still database.properties.production is in the WAR. I need a clean solution without duplicates and without database.properties.production in WAR.

Original source