2

I’m using Gradle 2.7 on Mac Yosemite. I have the following files:

src/main/environment/dev/context.xml
src/main/environment/qa/context.xml
src/main/environment/prod/context.xml

What I would like is if I run a build gradle -Pqa build, the appropriate context.xml file above is copied into my WAR (into the WEB-INF/classes directory is fine). How do I set this up with gradle?

Opal
  • 81,889
  • 28
  • 189
  • 210
Dave
  • 15,639
  • 133
  • 442
  • 830

2 Answers2

1

There're many ways of solving the problem. You can configure sourceSets, or include or exclude particular resources when building war file. You can also have single context.xml and perform resource filtering with ReplaceTokens filter.

I've chosen sourceSets:

apply plugin: 'war'

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

sourceSets {
   main {
      resources {
         srcDir "src/main/environment/$env"
      }
   }
}

The trick is to include/process only the env being passed. If no env is passed dev will be picked for further processing. Have a look a the demo.

Opal
  • 81,889
  • 28
  • 189
  • 210
0

You would have to do that using the environment variable. Have the system properties in a file. Read them in the build.gradle and based on it include the context.xml into the war.

user3307291
  • 708
  • 1
  • 8
  • 11
  • Having trouble with "Have the system properties in a file. ". Please provide an example as to what I need to add to my build.gradle file to make this possible. – Dave Oct 27 '15 at 18:42