Search

Dark theme | Light theme

May 11, 2016

Grails Goodness: Change Version For Dependency Defined By BOM

Since Grails 3 we use Gradle as the build system. This means we also use Gradle to define dependencies we need. The default Gradle build file that is created when we create a new Grails application contains the Gradle dependency management plugin via the Gradle Grails plugin. With the dependency management plugin we can import a Maven Bill Of Materials (BOM) file. And that is exactly what Grails does by importing a BOM with Grails dependencies. A lot of the versions of these dependencies can be overridden via Gradle project properties.

To get the list of version properties we write a simple Gradle task to print out the values:

// File: build.gradle
...
task dependencyManagementProperties << {
    description = 'Print all BOM properties to the console'

    // Sort properties and print to console.
    dependencyManagement
        .importedProperties
        .toSorted()
        .each { property -> println property }
}
...

When we run the task we get an overview of the properties:

$ gradle dependencyManagementProperties
:dependencyManagementProperties
activemq.version=5.12.3
antlr2.version=2.7.7
artemis.version=1.1.0
aspectj.version=1.8.8
atomikos.version=3.9.3
bitronix.version=2.1.4
cassandra-driver.version=2.1.9
commons-beanutils.version=1.9.2
commons-collections.version=3.2.2
commons-dbcp.version=1.4
commons-dbcp2.version=2.1.1
commons-digester.version=2.1
commons-pool.version=1.6
commons-pool2.version=2.4.2
crashub.version=1.3.2
derby.version=10.12.1.1
dropwizard-metrics.version=3.1.2
ehcache.version=2.10.1
elasticsearch.version=1.5.2
embedded-mongo.version=1.50.2
flyway.version=3.2.1
freemarker.version=2.3.23
gemfire.version=8.1.0
glassfish-el.version=3.0.0
gradle.version=1.12
groovy.version=2.4.6
gson.version=2.3.1
h2.version=1.4.191
...
BUILD SUCCESSFUL

Total time: 1.316 secs

For example if we want to change the version of the PostgreSQL JDBC driver that is provided by the BOM we only have to set the Gradle project property postgresql.version either in our build file or in the properties file gradle.properties:

// File: build.gradle
...
// Change version of PostgreSQL driver
// defined in the BOM.
ext['postgresql.version'] = '9.4.1208'
...
dependencies {
    ...
    // We don't have to specify the version
    // of the dependency, because it is 
    // resolved via the dependency management
    // plugin.
    runtime 'org.postgresql:postgresql'
}
...

Another way to change the version for a dependency defined in the BOM is to include a dependency definition in the dependencyManagement configuration block. Let's see what it looks like for our example:

// File: build.gradle
...
dependencyManagement {
    imports {
        mavenBom "org.grails:grails-bom:$grailsVersion"
    }
    dependencies {
        // Dependencies defined here overrule the
        // dependency definition from the BOM.
        dependency 'org.postgresql:postgresql:9.4.1208'
    }
    applyMavenExclusions false
}

dependencies {
    ...
    // We don't have to specify the version
    // of the dependency, because it is 
    // resolved via the dependency management
    // plugin.
    runtime 'org.postgresql:postgresql'
}
...

To see the actual version that is used we can run the task dependencyInsight:

$ gradle dependencyInsight --dependency postgres --configuration runtime
:dependencyInsight
org.postgresql:postgresql:9.4.1208 (selected by rule)

org.postgresql:postgresql: -> 9.4.1208
\--- runtime

BUILD SUCCESSFUL

Total time: 1.312 secs

This is just another nice example of the good choice of the Grails team to use Gradle as the build system.

Written with Grails 3.1.6