DirtyMikeAndTheBoys
DirtyMikeAndTheBoys

Reputation: 1077

How to select the right config file with Gradle?

My app has a general project structure like so:

src/main/groovy
    <Groovy files>
src/main/resources
    config.dev.xml
    config.qa.xml
    config.live.xml
build.gradle
gradle.properties
settings.gradle

In my Gradle build I use the Shadow Plugin to produce a "fat JAR" (standalone executable) by running:

gradle clean build shadowJar

This produces a new build directory like so:

src/main/groovy
    <Groovy files>
src/main/resources
    config.dev.xml
    config.qa.xml
    config.live.xml
build.gradle
gradle.properties
settings.gradle
build/
    distributions/
        myapp-standalone.jar

I would like the ability to specify which config file should be copied to this build/distributions directory, and I would like to specify this in the command itself. So something like:

gradle clean build shadowJar -Pconfig=dev

or:

gradle clean build shadowJar copyLive

etc. But I'm new to Gradle and not sure what the conventions are here, or what the best approach would be. The end result would be (in the case of selecting config.dev.xml), a build that produces:

build/
    distributions/
        myapp-standalone.jar
        config.dev.xml

Same for QA, LIVE, etc...

Ideas?

Upvotes: 0

Views: 908

Answers (2)

SuperAndrew
SuperAndrew

Reputation: 3073

try to use as 'The End' said projectProperty.

I think better option for you will be to put xml configuration inside jar. I guess that you want to have another config file in case of dev, production or etc. environment.

The structure would look like :

build/
    distributions/
        myapp-standalone.jar
                  src/...
                  resources/
                      config.dev.xml

and exaple how to get such effect:

jar{
    def config = 'dev'

    if (project.hasProperty('config')) {
        config = project.getProperty('config')
    }

    from ('src/main/resources/*config*.xml'){
        into 'resources'
    }

}

if you dont want to have config file in jar, just remove tag 'jar' and write some task for example:

task copyConfigFiles{
  // here put your copy code
}

jar.dependsOn copyConfigFiles

Upvotes: 0

The End
The End

Reputation: 709

As you can read here you can set properties with gradle clean build shadowJar -Pconfig=dev and then access it with config. When you want to read it, you can do something like this:

if (project.hasProperty('config') && config == dev) {
    println config
}

Upvotes: 1

Related Questions