How to include environment-specific resources?


I’m using Gradle 2.7. I would like environment-specific properties files to be included in my classpath for both my unit tests and WAR assembly. As such I have this code …

ext.env = project.hasProperty('env') ? project.env : 'dev'

// src is not default java source dir in gradle
// so change srcDir and resources srcDir 
//according to eclipse project layout  
sourceSets {
  main {
     java {
              srcDir 'src/main/java'
     resources {
       srcDir 'src/main/resources'
       srcDir 'src/main/environment/$env'

However, running a command like

gradle build -Penv=qa

does not include the files from the src/main/environment/qa directory in my classpath for testing and does not package the files into my WAR. What do I need to do to accomplish this?

In the code shown, you’re using single quotes around srcDir 'src/main/environment/$env'. As it stands now, you’re setting the location to ‘src/main/environment/$env’ literally rather than the ‘src/main/environment/qa’ in your example.

You would need double quotes around the string for $env to be evaluated.