6

I want to write a gradle build script, which works in different environments (development, live). In each environment I have to load different property sets (target directories, databases, etc.).

Is there a gradle way to read a property file dependent on the environment or a profile?

Lorim
  • 547
  • 2
  • 6
  • 14

3 Answers3

9

You may want to check out the Gradle Properties Plugin.

Include plugin:

plugins {
  id 'net.saliman.properties' version '1.4.2'
}

Create property files:

gradle-dev.properties

or

gradle-prod.properties

Call gradle:

gradle myTask -PenvironmentName=dev
gradle myTask -PenvironmentName=prod
Lorim
  • 547
  • 2
  • 6
  • 14
Mark Vieira
  • 13,198
  • 4
  • 46
  • 39
3

Also have a look at gradle's equivalent of maven's profiles.

Community
  • 1
  • 1
Opal
  • 81,889
  • 28
  • 189
  • 210
2

I had the same problem, but I use spring and configured it to read properties from classpath:application.properties

In this case you can add this to gradle.build with java plugin

if (project.hasProperty('env')) {
    println "Target environment: $env"
    sourceSets.main.resources.srcDir "src/main/environment/$env"
}

this for different folders based on environment

So for adding resources based on environment 'dev' you should have 'src/main/environment/dev' folder (with properties file), and invoke gradle: gradle myTask -Penv=dev

Pavel Kolmykov
  • 301
  • 2
  • 8