Skip to content

ankitrokdeonsns/java-properties-gradle-plugin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Java Properties Gradle Plugin

This plugin allows you to generate *.properties files for your java projects to be used as a resource.

Many times we need property values to be different based on which environment we are running our application. e.g. On local box all of our external endpoints may point to localhost:8888 while on production they will be like example.something.com

Maintaining multiple property files for the same can be a pain in such cases. And it is very likely that we may miss to add some property in one of our environment specific property files and the hell breaks loose.

Documentation

Simply add apply plugin: "javaproperties" in your build.gradle file.
This adds generateProperties task to the project.
The property files thus generated are placed in $project.projectDir/src/main/resources.

To generate application.properties file we need to keep data-bags in $project.projectDir/conf/data-bags/application directory.
Multiple *.propeties may be generated by adding directories in $project.projectDir/conf/data-bags directory. One for each *.properties file.

There must be a default.json present for each property file which defines all the key-value pairs to be rendered in *.properties file.
Optionally you may have prod.json, pre-prod.json etc. one data-bag per environment which simply define key-value pairs to be overridden for that specific environment.
To generate *.properties for a specific environment simply pass env gradle property to the task as an argument using -P.

Example

Lets say that we want to generate application.properties.
First we create $project.projectDir/conf/data-bags/application directory and add a default.json in it.

Default.json

{
    "threadPoolSize": 2, 
    "cronJobTimings": ["0800", "1400"],
    "external": {
        "url": "localhost:8888"
    }
}

Assuming we have added apply plugin: "javaproperties" in our build.gradle file when we run gradle generateProperties it will create application.properties file as follows.

application.properties

threadPoolSize=2
cronJobTimings=0800,1400
external.url=localhost:8888

Notice how array has been converted into comma separated values and nested JSON keys are concatenated.
Using such nesting helps you to group related properties in a clear, concise manner.

Now lets try to generate properties for prod environment.
First we have to create a prod.json in $project.projectDir/conf/data-bags/application.

prod.json

{
    "external": {
        "url": "some.example.com"
    }
}

Now if we run gradle generateProperties -Penv=prod we get application.properties as follows.

application.properties

threadPoolSize=2
cronJobTimings=0800,1400
external.url=some.example.com

Notice that only the property which is overriden in prod.json is changed and other properties remains as is.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages