Dave
Dave

Reputation: 19150

How do I copy a file into my WAR based on profile?

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?

Upvotes: 2

Views: 955

Answers (2)

Opal
Opal

Reputation: 84786

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.

Upvotes: 1

user3307291
user3307291

Reputation: 803

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.

Upvotes: 0

Related Questions