'Create a custom task in Gradle wrapping an existing one

I'm using Gradle to manage the SDLC of a Java project which takes some command-line parameters to change its default behaviour. For example, with the option -t the java app times its own execution time.

Doing it with Gradle is possible by running:

$ ./gradlew run --args="-t"

I wonder if there's a way in Gradle to define a custom task (let's say I wanna call it time) which basically would wrap over the run task and automatically append the --args="-t" option.

To be pragmatic, I would like to achieve with:

$ ./gradlew time

the same behaviour achieved with run --args="-t".

Thank you.



Solution 1:[1]

You can do this indeed. Since the run task is of type JavaExec you could just create your own task that extends from JavaExec:

task time(type: JavaExec) {
  group = 'run'
  description = 'Run the application with measuring the execution time'

  classpath sourceSets.main.runtimeClasspath
  mainClass = 'com.yourcompany.MainClass'
  args '-t'
}

You can read more about application plugin (and the run task) here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 romtsn