22

I can declare a type for a gradle task and doing so seems to inherit some methods. For example:

task myCopyTask(type: Copy){
  from "foo"
  into "bar"
}

So, I think myCopyTask is an instance of org.gradle.api.tasks.Copy class, yes? And if I declare a task without any type, it is an instance of org.gradle.api.DefaultTask? Sorry for the basic question. I have been reading the gradle guide like this page but it isn't clear to me what type: exactly is.

mkobit
  • 43,979
  • 12
  • 156
  • 150
hummingV
  • 1,014
  • 1
  • 11
  • 25

3 Answers3

5

It's already answered but this might help to understand as well.

They're the subclasses of the type Task. Once you define a type for your task, you get to access/set/configure that particular task's properties. In your case, this is a subclass called "Copy" (as you've already kinda figure out).

Note: Tasks are shipped with various plugins or written by you.

stdout
  • 2,471
  • 2
  • 31
  • 40
  • There's a list of the default available types here: https://docs.gradle.org/current/dsl/index.html#N104C2 – tzrlk Nov 21 '21 at 20:19
5

To get the type of an existing task, you can make use of Gradle's built-in help task using the --task command line option. The --task option takes in a task path for any task in the project. Here is an example using the help task on itself:

# ./gradlew help --task help                               

> Task :help                                        
Detailed task information for help                  

Path                                                
     :help                                          

Type                                                
     Help (org.gradle.configuration.Help)           

Options                                             
     --task     The task to show help for.          

Description                                         
     Displays a help message.                       

Group                                               
     help
mkobit
  • 43,979
  • 12
  • 156
  • 150
2

Why not just add a println and find out yourself?

task myCopyTask(type: Copy) {
    ... 
}
println "Type is $myCopyTask.class.name"
mkobit
  • 43,979
  • 12
  • 156
  • 150
lance-java
  • 25,497
  • 4
  • 59
  • 101
  • That's true. Sorry, I didn't think of using println like that. Thanks. And the actual classes are what I thought (or close to) org.gradle.api.tasks.Copy_Decorated org.gradle.api.DefaultTask_Decorated – hummingV Dec 30 '16 at 07:20
  • 2
    But what it is needed for? – Tomasz Gawel Jul 18 '18 at 13:26