Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle replace resource file with WAR plugin

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.

like image 648
Ertan D. Avatar asked Feb 15 '23 19:02

Ertan D.


1 Answers

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.

like image 105
Peter Niederwieser Avatar answered Feb 23 '23 20:02

Peter Niederwieser