Skip to content

Instantly share code, notes, and snippets.

@aimtiaz11
Last active January 27, 2022 23:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aimtiaz11/f786346e0c0d11a5475cfb64e84e9459 to your computer and use it in GitHub Desktop.
Save aimtiaz11/f786346e0c0d11a5475cfb64e84e9459 to your computer and use it in GitHub Desktop.
Compare Properties Files Using Groovy

Compare application properties files in Groovy

This script below allows you to compare properties files between environments.

If you maintain your application's externalised properties file in a seperate repository with a structure similar to below, you can use this to compare properties file between different environment.

compare-properties.groovy
|- src
|	|- main
|	|	|- resources
|	|	|	|- finance-app
|	|	|	|	|- dev
|	|	|	|	|	|- application.properties
|	|	|	|	|- test
|	|	|	|	|	|- application.properties

The script offers 2 mode for comparison. First is showing properties that missing in one or other environments. Second being the values of matching properties that are different.

Setting up and running the script

Update the name of the properties file and the base path from where the script will be run:

def propertiesFileName = "application.properties"
def basePath = "src/main/resources"

To run the script, place it at the root of the project and run by: groovy compare-properties.groovy

/**
* Usage: Run the script: groovy compare-properties.groovy
* You will be prompted for application name (finance-app), source environment (eg. uat3) and target environment (prod)
*/
// Script configuration
def propertiesFileName = 'application.properties'
def basePath = 'src/main/resources'
// End of script configuration - no more config below this point
def applicationName, src, target, diffMode
def allowedDiffModes = ['1', '2', '3']
if (args.length == 0) {
diffMode = System.console().readLine 'Select mode: \n ' +
'Enter 1 to evaluate missing properties. \n ' +
'Enter 2 to evaluate value mismatches.\n ' +
'Enter 3 to find properties where values are duplicating. \n ' +
'Choice: '
if (!allowedDiffModes.contains(diffMode)) {
println 'Invalid option selected'
System.exit(0)
}
applicationName = System.console().readLine 'Enter application name: '
src = System.console().readLine 'Enter Source Environment: '
if (['1', '2'].contains(diffMode))
target = System.console().readLine 'Enter Target Environment: '
} else {
diffMode = args[0]
applicationName = args[1]
src = args[2]
target = args[3]
}
Properties srcProperties = new Properties()
Properties targetProperties = new Properties()
File srcPropertiesFile = new File("${basePath}/${applicationName}/${src}/${propertiesFileName}")
File targetPropertiesFile = new File("${basePath}/${applicationName}/${target}/${propertiesFileName}")
Closure loadPropertiesFromFile = {
File propertiesFile, Properties loadIntoProps, String env, String appName ->
if (env) {
try {
propertiesFile.withInputStream {
loadIntoProps.load(it)
}
}
catch (FileNotFoundException e) {
println "Environment ${env} not defined for the application ${appName}. \nExiting script. \n"
System.exit(0)
}
}
}
loadPropertiesFromFile(srcPropertiesFile, srcProperties, src, applicationName)
loadPropertiesFromFile(targetPropertiesFile, targetProperties, target, applicationName)
// Only the keys
List srcPropListKeys = srcProperties.collect { return it.key }
List targetPropListKeys = targetProperties.collect { return it.key }
List srcPropList = srcProperties.collect { return ["${it.key}": it.value] }
List targetPropList = targetProperties.collect { return ["${it.key}": it.value] }
Closure evaluateMissingProps = {
println "Properties in ${src} that are not in ${target}: "
(srcPropListKeys - targetPropListKeys).size == 0 ? println(' None') : (srcPropListKeys - targetPropListKeys).each {
println " ${it} \n"
}
println "\n"
println "Properties in ${target} that not in ${src}:"
(targetPropListKeys - srcPropListKeys).size == 0 ? println(' None') : (targetPropListKeys - srcPropListKeys).each {
println " ${it} \n"
}
}
Closure evaluateValueMismatches = {
List diff = (srcPropList - targetPropList)
println "${diff.size} properties in ${src} are different in ${target}: "
diff.size == 0 ? println('None') : diff.each {
it.each { k, v ->
def targetProp = targetProperties.find { it.key == k }
println "-----"
println " $k "
println "\t${src}\t: $v "
println "\t${target}\t: ${targetProp?.value ? targetProp?.value : " ** DOES NOT EXIST IN: ${target}. ** "}"
}
}
}
Closure findDuplicatingProperties = {
srcProperties.groupBy { it.value }.findAll { it.value.size() > 1 }?.each {
it.value.each { println "${it.key}=${it.value}" }
}
}
println '*********** START ***********'
println "Number of properties in ${applicationName}/${src}: ${srcPropListKeys.size}\n"
if (target) println "Number of properties in ${applicationName}/${target}: ${targetPropListKeys.size}\n"
if (diffMode == '1')
evaluateMissingProps()
else if (diffMode == '2')
evaluateValueMismatches()
else if (diffMode == '3')
findDuplicatingProperties()
else
throw new RuntimeException('Invalid mode selected...')
println "\n *********** END *********** \n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment