Gradle Goodness: Command Line Options For Custom Tasks

Gradle added an incubation feature to Gradle 4.6 to add command line options for custom tasks. This means we can run a task using Gradle and add command line options to pass information to the task. Without this feature we would have to use project properties passed via the -P or --project-property. The good thing about the new feature is that the Gradle help task displays extra information about the command line options supported by a custom task.

To add a command line option we simply use the @Option annotation on the setter method of a task property. We must make sure the argument for the setter method is either a boolean, Boolean, String, enum, List<String> or List<enum>. The @Option annotation requires an option argument with the name of the option as it must be entered by the user. Optionally we can add a description property with a description about the option. It is good to add the description, because the help task of Gradle displays this information and helps the user of our custom task.

Let’s start with a custom task that opens a file in the default application associated for the type of file. The task has own property file of type File. Remember we cannot create an command line option for all property types. To add a command line option we must overload the setFile method to accept a String value. This setter method is used to expose the file property as option.

We use the task in the following build file. We create a task openReadme and set the file property value in the build script. We also create the task open, but don’t set the file property. We will use the command line option file for this task:

To run the open task and set the file command line option we use a double-dash as prefix:

We can more information about the supported options for our task by invoking the help task. We define the task name with the option task:

To define a set of valid values for an option we must add a method to our class that returns a list of valid values annotated with the @OptionValues annotation. We must set the task property name as argument for the annotation. Then when we invoke Gradle’s help task we see a list of valid values. Validation of the property value is not done with the annotation, it is only for informational purposes. So validation must be added to the task code by ourselves.

We rewrite our OpenFile task and add the requirement that only files in the project directory can be opened and the filename must be in upper case. The method availableFiles return all files in the project directory with their name in upper case. The annotation @OptionValues is added to the method. In the task action method openFile we check if the file is valid to be opened

Let’s run the help task and see that available values are shown this time:

Written with Gradle 4.7.

Original post

This entry was posted in Coding, Gradle and tagged by mrhaki. Bookmark the permalink.

About mrhaki

My name is Hubert A. Klein Ikkink also known as mrhaki. I work at the great IT company JDriven. Here I work on projects with Groovy & Grails, Gradle and Spring. At JDriven we focus on SpringSource technologies. All colleagues want to learn new technologies, support craftmanship and are very eager to learn. This is truly a great environment to work in. You can contact me via Google+ or @mrhaki.

Leave a Reply

Your email address will not be published. Required fields are marked *