Gradle replace resource file with WAR plugin

I am trying to replace a resource file in my WAR plugin with Gradle.

I basically have two resource files:

database.properties database.properties.production 

I want to achieve the replacement of 'database.properties' 'database.properties.production' in the last WAR file in the WEB-INF / classes section.

I tried many things, but the most logical for 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 leads to duplication of all other resource files, including duplicates of database.properties (two different files with the 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.

+4
source share
2 answers

If you cannot make a decision at runtime (which is the recommended recommendation for working with environment-specific configuration), eachFile might be the best choice:

 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 work better than eachFile .

+6
source

If you want a solution that works for several archiving tasks, you can change the property files in "build / resources / main" after the processResources task is completed. I am not sure that this is a common practice. I work with two archive tasks, jar and par, which are generated from the build folder, so this worked for me.

In addition, the following solution uses all files that end in ".production".

I tested this solution with Gradle 1.11

 classes << { FileTree tree = fileTree(dir: "build/resources/main").include("*.production") tree.each { File file -> String origName = file.name.substring(0, file.name.length() - ".production".length()) File orig = new File(file.getParent(), origName) orig.delete() file.renameTo(orig) } } 
+1
source

Source: https://habr.com/ru/post/1493434/


All Articles