DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Introduce a New API Quickly Using Spring Boot and Gradle
  • Transitioning From Groovy to Kotlin for Gradle Android Projects
  • Building AI Applications With Java and Gradle
  • Understanding Dependencies...Visually!

Trending

  • Ten Years of Beam: From Google's Dataflow Paper to 4 Trillion Events at LinkedIn
  • The 7 Pillars of Meeting Design: Transforming Expensive Conversations into Decision Assets
  • Liquibase: Database Change Management and Automated Deployments
  • DuckDB for Python Developers

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.

By 
Hubert Klein Ikkink user avatar
Hubert Klein Ikkink
·
Nov. 16, 16 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
5.4K Views

Join the DZone community and get the full member experience.

Join For Free

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 {
 
    @Model
    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
     */
    @Defaults
    void defaultsVersionFile(
            final VersionFile versionFile) {
 
        // Set default value for version property to.
        versionFile.version = 'unspecified'
    }
 
    @Mutate
    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.

Gradle

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

Opinions expressed by DZone contributors are their own.

Related

  • Introduce a New API Quickly Using Spring Boot and Gradle
  • Transitioning From Groovy to Kotlin for Gradle Android Projects
  • Building AI Applications With Java and Gradle
  • Understanding Dependencies...Visually!

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook