Over a million developers have joined DZone.

Gradle Goodness: Set Default Values with Rule-Based Model Configuration

DZone's Guide to

Gradle Goodness: Set Default Values with Rule-Based Model Configuration

When using a rule-based model configuration with Gradle, it is possible to assign default values to objects. Read on to find out how.

· Java Zone
Free Resource

Never build auth again! The Okta Developer Platform makes it simple to implement authentication, authorization, MFA and more in Java applications. Get started with the free API.

When we use rule-based model configuration in our Gradle project, we can give Gradle rules on how to manage objects from the model space. These rules are defined in a class that extends RuleSource. When we want to set some default values for properties of a model object (in Gradle terms this is a subject) we can use the @Defaults annotation. Rules annotated with @Defaults are invoked right after the object is created and before any other methods that can mutate the state of the object.

The method, to set the default values, must have the type of the object as the first parameter. Other parameters are considered input parameters and can be used to set a default value based on other model objects.

In a previous post, we worked with a managed object VersionFile. Let's add a default value for the version property. We want the version default value to be the unspecified:

// File: buildSrc/src/main/groovy/mrhaki/gradle/VersionFileTaskRules.groovy
package mrhaki.gradle
import org.gradle.api.Task
import org.gradle.model.Defaults
import org.gradle.model.Model
import org.gradle.model.ModelMap
import org.gradle.model.Mutate
import org.gradle.model.Path
import org.gradle.model.RuleSource
class VersionFileTaskRules extends RuleSource {
    void versionFile(final VersionFile versionFile) { }
     * Method to set default values for {@link VersionFile} object
     * created by the {@link #versionFile} method.
     * @param versionFile First argument is the type we want to set default values for
    void defaultsVersionFile(
            final VersionFile versionFile) {
        // Set default value for version property to.
        versionFile.version = 'unspecified'
    void createVersionFileTask(final ModelMap<Task> tasks, final VersionFile versionFile) {
        tasks.create('generateVersionFile', VersionFileTask) { task ->
            task.version = versionFile.version
            task.outputFile = versionFile.outputFile

When we run the model task, we can see Gradle knows the defaultsVersionFile methods was used to change the state of the VersionFile instance:

$ gradle -q model
+ versionFile
      | Type:           mrhaki.gradle.VersionFile
      | Creator:        VersionFileTaskRules#versionFile(VersionFile)
      | Rules:
         ⤷ VersionFileTaskRules#defaultsVersionFile(VersionFile)
    + outputFile
          | Type:       java.io.File
          | Value:      /Users/mrhaki/Projects/mrhaki.com/blog/posts/samples/gradle/versionrule/build/version.txt
          | Creator:    VersionFileTaskRules#versionFile(VersionFile)
    + version
          | Type:       java.lang.String
          | Value:      null
          | Creator:    VersionFileTaskRules#versionFile(VersionFile)

Written with Gradle 3.2.

Build and launch faster with Okta’s user management API. Register today for the free forever developer edition!

gradle ,java ,rule based model configuration ,object management

Published at DZone with permission of Hubert Klein Ikkink, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}