Using Groovy and Gradle for Configuration Synchronization and Database Integration

This article explains how Groovy, combined with Gradle, can serve as a native application server to synchronize environment‑specific property files with a MySQL registry, demonstrating practical build‑script configuration, file‑system traversal, and Groovy‑SQL database operations for robust CI/CD workflows.

FunTester
FunTester
FunTester
Using Groovy and Gradle for Configuration Synchronization and Database Integration

Groovy is described as a mature, enterprise‑grade language that runs on the JVM and is widely adopted for scripting, testing, and build automation, especially within the Spring ecosystem.

The author presents a recent use case where Gradle, being Groovy‑native, is leveraged to build a "key‑value" configuration registry: property files for each application are stored in environment‑specific directories and need to be kept in sync with a database whenever they change in source control.

By defining a JavaExec task in build.gradle, the Gradle build can invoke a Groovy script that performs the synchronization, allowing Jenkins or any CI server to run the task without requiring a separate Gradle installation.

apply plugin: 'groovy'

repositories {
    mavenCentral()
    mavenLocal()
}

// Declare dependencies
dependencies {
    compile localGroovy()
    compile("mysql:mysql-connector-java:5.1.35")
    compile("com.h2database:h2:1.4.187")
    testCompile("junit:junit:4.12")
}

// Task to run the Groovy sync script
task runScript(type: JavaExec) {
    description 'Run a Groovy script to sync the environment config registry with the properties files in source control'
    classpath = sourceSets.main.runtimeClasspath
    main 'com.mypackage.SyncScript'
    args Arrays.asList('jdbc:mysql://registry/db', 'com.mysql.jdbc.Driver', 'user', 'password').toArray()
}

defaultTasks 'runScript'

The Groovy synchronization script iterates over each environment directory and each properties file, loads the file into a java.util.Properties object, and then uses groovy.sql.Sql to upsert the key‑value pairs into a MySQL table.

// Iterate through each per‑environment directory
new File('config').eachDir { File environmentDirectory ->
    // Iterate through each per‑application properties file
    environmentDirectory.eachFileMatch FileType.FILES, ~/.+\.properties/, { File applicationFile ->
        def environment = environmentDirectory.name
        def application = applicationFile.name.replace('.properties', '')
        println "Processing properties for env: '$environment', application: '$application'"
        def properties = new Properties()
        applicationFile.withInputStream { stream -> properties.load(stream) }
        // ... further processing ...
    }
}

Database interaction uses a multi‑line SQL string and sets ResultSet.CONCUR_UPDATABLE so rows can be updated, inserted, or deleted directly from the result set, illustrating how Groovy’s groovy.sql.Sql simplifies JDBC boilerplate.

database = groovy.sql.Sql.newInstance(jdbcUrl, jdbcUsername, jdbcPassword, jdbcDriver)
database.resultSetConcurrency = ResultSet.CONCUR_UPDATABLE

properties.entrySet().each {
    def name = it.key
    def value = it.value
    def existingRecordQuery = '''
SELECT environment, service, property_name, property_value FROM environment_properties
WHERE environment = ? AND service = ? AND property_name = ?
''' 
    database.query(existingRecordQuery, [environment, service, name]) { ResultSet rs ->
        if (rs.next()) {
            def existingValue = rs.getString('property_value')
            if (!existingValue.equals(value)) {
                rs.updateString('property_value', value)
                rs.updateRow()
            }
        } else {
            rs.moveToInsertRow()
            rs.updateString('environment', environment)
            rs.updateString('service', service)
            rs.updateString('property_name', name)
            rs.updateString('property_value', value)
            rs.insertRow()
        }
    }
}

The article concludes that, although the scenario is specific, it showcases broadly useful concepts: Groovy’s expressive syntax, its seamless integration with Gradle, and the power of Groovy‑SQL for reducing boilerplate in database‑driven automation tasks.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JVMBuild AutomationsqlConfiguration ManagementGradleGroovy
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.