29

Gradle is a well documented project, but when I search for the docs for tasks.withType(), I only find the API docs.

Isn't this an official part of Gradle?

4
  • If you can find the official API docs, why shouldn't this be an official part? Commented Aug 25, 2017 at 13:14
  • 3
    because I expected that this concept would be also explained in the general docs... Commented Aug 25, 2017 at 14:19
  • 4
    Gradle docs are one of the worst... at least for search purposes Commented Oct 4, 2019 at 13:18
  • Gradle API docs are ok once you know where to start. Starting with org.gradle.api.Project: This interface is the main API to interact with Gradle from inside the build file build.gradle.kts. So inside build.gradle.ktsI call getTasks() method to obtain and interact with tasks of this project. But alternatively looking into org.gradle.api.tasks.TaskCollection: You can obtain a TaskContainer instance by calling Project.getTasks(), or using the tasks property in your build script so I continue using the tasksproperty ... etc. etc. Commented Nov 24, 2024 at 10:17

1 Answer 1

28

One of the fundamental concepts in Gradle is that everything is executed against a Project instance. Once you know that, you can work through the javadoc to find what you want. There's a bit of "magic" to be wary of too (a topic for another conversation) and also groovy syntax to understand .

So tasks.withType(...) can be looked up in the javadocs as Project.getTasks().withType(...).

You'll notice that Project.getTasks() returns a TaskCollection (which you found in your googling)

* edit *

There's a mention here in the docs which links to the TaskContainer javadocs

A project is essentially a collection of Task objects. Each task performs some basic piece of work, such as compiling classes, or running unit tests, or zipping up a WAR file. You add tasks to a project using one of the create() methods on TaskContainer, such as TaskContainer.create(java.lang.String). You can locate existing tasks using one of the lookup methods on TaskContainer, such as TaskCollection.getByName(java.lang.String)

Sign up to request clarification or add additional context in comments.

4 Comments

So, it seems that it is true that there are only API-Docs and no User-Docs. It seems that also books don't mention it... :-|
See my edit, there's a mention in the Project docs which links to TaskContainer javadocs
@lance-java Groovy, which is the language used by Gradle, uses the JavaBean properties convention, getFoo(), setFoo(val). In this case, Project.tasks is a plain old JavaBean style property, which would translate to Project.getTasks() in Java. In Groovy, properties are accessed by name and the getter/setter is called transparently, groovy-lang.org/…
I think they should update the API-doc ... You add tasks to a project using one of the create() methods on TaskContainer, such as TaskContainer.create(java.lang.String) ... and mention the "Task Configuration Avoidance" ... You add tasks to a project using one of the create() methods on TaskContainer, such as TaskContainer.create(java.lang.String) BUT instead try to use TaskContainer.register​(java.lang.String) which is generally more efficient to use because it will defer creation until required (for more information see Task Configuration Avoidance) ...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.