23

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.

3 Answers 3

6

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 
Sign up to request clarification or add additional context in comments.

1 Comment

Excellent explanation, I was scanning the docs all day and did not find anything.
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.

1 Comment

There's a list of the default available types here: docs.gradle.org/current/dsl/index.html#N104C2
2

Why not just add a println and find out yourself?

task myCopyTask(type: Copy) { ... } println "Type is $myCopyTask.class.name" 

2 Comments

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
But what it is needed for?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.