// Copyright 2018 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

[[task_configuration_avoidance]]
= Task Configuration Avoidance

We are asking early adopters to try out a new API for creating and configuring tasks to iron out any issues and gather feedback. This chapter provides a quick introduction to the feature and explains some guidelines for migrating your build to use the new API. We intend for this new API to eventually replace the existing API through our usual deprecation process over several major releases.

[NOTE]
====
The new Task configuration API is currently <<feature_lifecycle.adoc#feature_lifecycle,incubating>>. Please be aware that the DSL or API may change in minor Gradle versions.
====

[[sec:what_is_new_task_configuration]]
== What is the new API good for?

In a nutshell, the new API allows builds to avoid the cost of creating and configuring tasks during Gradle's configuration phase when those tasks will never be executed. We identified task configuration as a contributor to overall configuration time. Configuration time affects every build, so making it faster is good for everyone. For example, if you're only compiling, Gradle doesn't need to also configure code quality, testing and publishing tasks. For tasks that aren't used often by developers, configuration can be expensive (e.g., running `git status` or querying a webservice) and can slow down builds that do not need to execute them. This new API avoids configuring those tasks if they are not needed.

This feature is designed to be backwards compatible when mixed with the old and new APIs, so you can gradually migrate your plugins and build scripts to the new API. Instead of immediately creating a task and running all configuration actions, Gradle exposes a link:{javadocPath}/org/gradle/api/provider/Provider.html[Provider] to a task. A task will only be created and configured when the task is required. If at any point, a task registered with the new API is accessed through the old API, the task will be created and configured just as if it had been created with the old API.

Many built-in Gradle plugins have been converted to use this new API already, so you may see some benefits without changing anything.

[[sec:what_do_we_want]]
== What do we want you to do?

We've been using this feature in many of the built-in Gradle plugins, internal Gradle projects and the Gradle build itself. Now we would appreciate your help in evaluating it with your builds as well.

We'd like for you to take a few steps:

- Try your build with the latest nightly (see the version number at the top of this page).
- How many tasks do you see registered with the new API?  See <<#sec:how_do_i_know_its_working,How do I know it's working?>> for how to find this.
- Which task types are most numerous? Which ones are created with the old API? Which ones are registered with the new API, but have been created/configured anyways?
- Following <<#sec:task_configuration_avoidance_migration_guidelines,Pitfalls and migration guidelines>>, change your build scripts to use the new API to avoid creating/configuring tasks that are from the built-in Gradle plugins. Is there an impact to the number of tasks that are created?
- Convert some of your custom plugins/tasks to use the new API.  Is there an impact to the number of tasks using the new API and the number of new API tasks that are created?
- Run the https://github.com/gradle/gradle-profiler[gradle-profiler] to measure the difference between a build with less task configuration and a build where all tasks are configured. What do you see? Use <<#sec:new_task_gradle_profiler_scenario,the scenario file below>>.

[[sec:task_configuration_avoidance_guidelines]]
== Guidelines

[[sec:how_do_i_defer_creation]]
=== How do I defer task creation?

This feature requires build authors to opt-in by migrating task creation from the link:{javadocPath}/org/gradle/api/tasks/TaskContainer.html#create-java.lang.String-[TaskContainer.create(java.lang.String)] APIs to the link:{javadocPath}/org/gradle/api/tasks/TaskContainer.html#register-java.lang.String-[TaskContainer.register(java.lang.String)] APIs. The `register(...)` API registers a task to be created at a later time if and only if the task is needed. The `create(...)` API continues to eagerly create and configure tasks when it is called.

Using the new creation API may not be enough to avoid all task configuration completely. You may need to change other code that configures tasks by name or by type, as explained in the following sections.

[[sec:how_do_i_defer_configuration]]
=== How do I defer task configuration?

Existing APIs like link:{javadocPath}/org/gradle/api/DomainObjectCollection.html#all-org.gradle.api.Action-[DomainObjectCollection.all(org.gradle.api.Action)] and link:{javadocPath}/org/gradle/api/DomainObjectCollection.html#withType-java.lang.Class-org.gradle.api.Action-[DomainObjectCollection.withType(java.lang.Class, org.gradle.api.Action)] will immediately create and configure any registered tasks. To defer task configuration, you will need to migrate to a new API equivalent. See the <<#sec:old_vs_new_configuration_api_overview,table below>> to identify the new API alternative.

[[sec:how_do_i_reference_a_task]]
=== How do I reference a task without creating/configuring it?

Calling link:{javadocPath}/org/gradle/api/provider/Provider.html#get--[Provider.get()] or looking up a task by name with link:{javadocPath}/org/gradle/api/tasks/TaskCollection.html#getByName-java.lang.String-[TaskCollection.getByName(java.lang.String)] will cause the task to be created and configured. Methods like link:{groovyDslPath}/org.gradle.api.Task.html#org.gradle.api.Task:dependsOn(java.lang.Object++[]++)[Task.dependsOn(java.lang.Object...)] and link:{javadocPath}/org/gradle/api/file/ConfigurableFileCollection.html#builtBy-java.lang.Object++...++-[ConfigurableFileCollection.builtBy(java.lang.Object++...++)] work with link:{javadocPath}/org/gradle/api/tasks/TaskProvider.html[TaskProvider] in the same way as link:{groovyDslPath}/org.gradle.api.Task.html[Task], so you do not need to unwrap a `Provider` for explicit dependencies to continue to work.

If you are configuring a task by name, you will need to use the new API equivalent. See the <<#sec:old_vs_new_configuration_api_overview,table below>> to identify the new API alternative.

[[sec:how_do_i_get_a_task]]
=== How to get an instance of a Task?

In the event you still need to get access to a Task instance, you can use link:{javadocPath}/org/gradle/api/tasks/TaskCollection.html#named-java.lang.String-[TaskCollection.named(java.lang.String)] and call link:{javadocPath}/org/gradle/api/provider/Provider.html#get--[Provider.get()].  This will cause the task to be created/configured, but everything should work as it has with the eager APIs.

Please https://github.com/gradle/gradle/issues/5664[contact us] if you find tricky build logic that requires you to get a Task instance.

[[sec:how_do_i_know_its_working]]
=== How do I know if it's working?

We provide a couple of internal flags to print out information after a build.  This will eventually be captured and displayed in an easier to digest form.

.gradle -Dorg.gradle.internal.tasks.stats help
----
Task counts: Old API 188, New API 587, total 775 # 1
Task counts: created 613, avoided 162, %-lazy 21 # 2

Task types that were created with the old API # 3
class org.gradle.api.DefaultTask 31
class org.jetbrains.kotlin.gradle.tasks.KotlinCompile 28
class org.gradle.api.tasks.JavaExec 24
class org.jlleitschuh.gradle.ktlint.KtlintCheck 24
class org.gradle.api.tasks.bundling.Jar 16
class org.gradle.kotlin.dsl.accessors.tasks.PrintAccessors 16
class org.gradle.plugin.devel.tasks.ValidateTaskProperties 15
class org.gradle.another.CoolTask 13
class org.gradle.plugin.devel.tasks.GeneratePluginDescriptors 10
class org.gradle.plugin.devel.tasks.PluginUnderTestMetadata 10
class org.gradle.kotlin.dsl.accessors.tasks.UpdateProjectSchema 1

Task types that were registered with the new API but were created anyways # 4
class org.gradle.api.DefaultTask 142
class org.gradle.api.tasks.Delete 106
class org.gradle.api.tasks.compile.JavaCompile 32
class org.gradle.language.jvm.tasks.ProcessResources 32
class org.gradle.api.tasks.testing.Test 16
class org.gradle.api.tasks.javadoc.Javadoc 16
class org.gradle.plugins.ide.idea.GenerateIdeaModule 15
class org.gradle.plugins.ide.eclipse.GenerateEclipseClasspath 15
class org.gradle.plugins.ide.eclipse.GenerateEclipseJdt 15
class org.gradle.plugins.ide.eclipse.GenerateEclipseProject 15
class org.gradle.api.tasks.compile.GroovyCompile 10
class org.gradle.api.tasks.javadoc.Groovydoc 5
class org.gradle.api.plugins.quality.Checkstyle 4
class org.gradle.api.plugins.quality.CodeNarc 2
----
1. 188 tasks are created with the old API and 587 tasks are registered with the new API for a total of 775 tasks.
2. 613 tasks were created and configured and only 162 tasks were avoided (never created or configured). 21% of all tasks were avoided (higher is better).
3. Lists of the type of tasks that were created with the old API. This is a good list to work down to increase the amount of possible avoidable task configuration.
4. Lists of the type of tasks that were created with the new API but were created/configured anyways. This is a good list to work down to increase the amount of task configuration that is avoided.

These statistics are printed out once per build. Projects with buildSrc and composite builds will display this information multiple times. In a build that uses the new APIs perfectly, we should see 0 tasks created with the old API and only 1 created/configured task because we are only executing the `help` task. If you run other tasks (like `build`), you should expect many more tasks to be created and configured.

You can use the list of task types to guide which tasks would provide the biggest bang for your buck when you migrate to the new API.

To approximate the time it takes to configure a build without executing tasks, you can run `gradle help`. Please use the Gradle Profiler to measure your build as described <<#sec:new_task_gradle_profiler_scenario,further on>>.

[[sec:task_configuration_avoidance_migration_guidelines]]
== Migration Guide

The following sections will go through some <<#sec:task_configuration_avoidance_general, general guidelines>> to adhere when migrating the build logic as well as <<#sec:task_configuration_avoidance_migration_steps, the steps>> we recommend following.
We also cover some <<#sec:task_configuration_avoidance_troubleshooting, troubleshooting>> and <<#sec:task_configuration_avoidance_pitfalls, pitfalls>> to help you work around some issues you may encountered during the migration.

[[sec:task_configuration_avoidance_general]]
=== General
1. [[task_configuration_avoidance_guideline_use_help_task]] **Use `help` task as a benchmark during the migration.**
The `help` task is the perfect candidate to benchmark your migration process.
In a completely lazy build, https://scans.gradle.com/s/o7qmlmmrsfxz4/performance/configuration?openScriptsAndPlugins=WzFd[a build scan should show no task created immediately or created during configuration].
Only a single task, the `help` task, should be created during task graph calculation.
Be mindful of <<#task_configuration_avoidance_pitfall_build_scan_plugin, the pitfall around the lazy API usage in the build scan plugin>>.

2. [[task_configuration_avoidance_guideline_only_mutate_task_object]] **Only mutate the current task inside configuration action.**
Because the task configuration action can now run immediately, later or never, mutating anything other than the current task will expose flaky-like behavior in your build.
In the future, Gradle will consider these flaky-mutations as failures.
It also means the configuration needs to flow from the extensions to the tasks.
Thus, the task should be configured by the extension and not the other way around.

3. [[task_configuration_avoidance_guideline_prefer_small_incremental_change]] **Prefer small incremental changes.**
Smaller changes are easier to sanity check.
If you ever break your build logic, it will be easier to analyze the changelog since the last successful verification.

4. [[task_configuration_avoidance_guideline_validate_build_logic]] **Ensure a good plan is established for validating the build logic.**
Usually, a simple `build` task invocation should do the trick to validate all corner of your build logic.
However, if all automation aspect is centralized around Gradle, you may need to validate other task execution path.

5. [[task_configuration_avoidance_guideline_prefer_automatic_testing]] **Prefer automatic testing to manual testing.**
It’s good practice to write integration test for your build logic using TestKit.

6. [[task_configuration_avoidance_guideline_avoid_task_by_name]] **Avoid referencing a task by name.**
In the majority of the case, referencing a task by name is a fragile pattern and should be avoided.
Although task name is available on the `TaskProvider`, effort should be made to use references from a strongly typed model instead.

7. **Use the lazy task API as much as possible.**
Although realizing a task within a lazy configuration action may not have any immediate impact, it makes misuse of the lazy API more costly.
The lazy task API is an opt-in feature, meaning someone may be realizing task eagerly which would cause the realization to realize other tasks transitively.
Using `TaskProvider` helps create an indirection that protects transitive realization.

8. Some APIs may be disallowed if you try to access them from the new API's configuration blocks. For example,
link:{groovyDslPath}/org.gradle.api.Project.html#org.gradle.api.Project:afterEvaluate(org.gradle.api.Action)[`Project.afterEvaluate()`]
cannot be called when configuring a task registered with the new API. Since `afterEvaluate` is used to delay configuring a `Project`, mixing delayed configuration
with the new API can cause hard to track down configuration errors because tasks registered with the new API are not always configured, but an `afterEvaluate` block
may be expected to always execute.

[[sec:task_configuration_avoidance_migration_steps]]
=== Migration Steps
The first part of the migration process is to go through the code and manually migrate eager task creation and configuration to use lazy APIs.
The following explores the recommended steps for a successful migration.
While going through those steps, keep in mind <<#sec:task_configuration_avoidance_general, the guidelines>>.

[NOTE]
====
Using the new API in a plugin will require users to use Gradle 4.9 or newer. Plugin authors should refer to <<sec:task_configuration_avoidance_backward_compatibility_migration>> section.
====

1. **Migrate task configuration that affects all tasks (`tasks.all {}`) or subsets by type (`tasks.withType(...) {}`).**
This will cause your build to create fewer built-in Gradle task types.
Note that despite some built-in Gradle task aren’t using the lazy APIs, the configuration affecting those task type should still be migrated.
We recommend using regex code search capability offered by modern IDE.
For example, the Intellij regex `\.all\s*((\(?\s*\{)|\(\s*new)` will identify every call to `.all` of a `DomainObjectCollection` and subtype.
We will need to go through those result and identify and migrate only the method calls affecting the `TaskContainer` or `TaskCollection`.
Be aware of the <<#sec:task_configuration_avoidance_pitfalls, common pitfall around deferred configuration>>.

2. **Migrate tasks configured by name.**
Just like the previous point, it will cause your build to create fewer built-in Gradle tasks.
For example, logic that uses `TaskContainer#getByName(String, Closure/Action)` would be converted to `TaskContainer#named(String).configure(Closure/Action)`.
It also include <<#task_configuration_avoidance_pitfalls_hidden_eager_task_realization, task configuration as DSL blocks>>.
Be aware of the <<#sec:task_configuration_avoidance_pitfalls, common pitfall around deferred configuration>>.

3. **Migrate tasks creation to `register(...)`.**
It will cause your build to create fewer tasks in general.
Be aware of the <<#sec:task_configuration_avoidance_pitfalls, common pitfall around deferred configuration>>.

At this point, we should see a decent improvement regarding build configuration.
We recommend <<#sec:how_do_i_know_its_working, profiling your build and note the improvement>>.
Under some circumstance, you may <<task_configuration_avoidance_troubleshooting_no_improvement, notice no improvement>>.
It’s unfortunate, and we feel your frustration. Keep reading onto the next section.

[[sec:task_configuration_avoidance_troubleshooting]]
=== Troubleshooting
* **The build became unstable after migrating to lazy task configuration.**
It’s a common issue that happens during the migration.
Follow these next steps to find out the issue:
  1. Is the build succeed when all tasks are eagerly resolved on creation using the command line flag (`-Dorg.gradle.internal.tasks.eager=true`)?
  Verify that <<#task_configuration_avoidance_guideline_only_mutate_task_object, each task configuration only mutating the task>>.

  2. Was there any build logic accidentally deleted?
  Verify <<#task_configuration_avoidance_guideline_prefer_small_incremental_change, your change log>> for disparity in the configuration that may have been deleted.

  3. It may be a real issue.
  For example, https://github.com/gradle/gradle-native/issues/737[the issue of `FileCollection` using `TaskProvider` was a real discrepancy] found while migrating a build to lazy task API.
  As a rule of thumb, a `TaskProvider` should be accepted everywhere a `Task` instance is accepted.

* **Where a task was resolved?**
As we keep developing the feature, more reporting, and troubleshooting information will be made available to answer this question.
In the meantime, https://gradle.com/enterprise/releases/2018.3#reduce-configuration-time-by-leveraging-task-creation-avoidance[build scan is the best way to answer this question].
Follow these steps:

  a. https://scans.gradle.com/[Create a build scan].
  Execute the Gradle command using the `--scan` flag.

  b. Navigate to the configuration performance tab.
+
.Navigate to configuration performance tab in build scan
image::taskConfigurationAvoidance-navigate-to-performance.png[]
+
    1. Navigate to the performance card from the left side menu.
    2. Navigate to the configuration tab from the top of the performance card.

  c. All the information requires will be presented.
+
.Configuration performance tab in build scan annotated
image::taskConfigurationAvoidance-performance-annotated.png[]
+
    1. Total tasks present when each task are created or not.
      - Created immediately represent all the task created using the eager task APIs.
      - Created during configuration represent all the tasks that were created using the lazy task APIs, but was realized explicitly (via `TaskProvider#get()`) or implicitly using the eager task query APIs.
      - Both "Created immediately" and "Created during configuration" numbers are considered the "bad" numbers that should be minimized as much as possible.
      - Created during task graph calculation number represent all the task created while building the execution task graph.
      Typically, this number should be equal to the number of tasks that is executed.
      - Not created number represents all the tasks that were avoided in this build session.

    2. The next section helps answer the question of where a task was realized. For each scripts, plugins and lifecycle callbacks, the last column represents the tasks that were created either immediately or during configuration.
    Ideally, this column should be empty.
    The migration should be focusing on the build logic under your control.

    3. Extending any scripts, plugins, or lifecycle callbacks subsection shows a break down in which project it was applied.

* [[task_configuration_avoidance_troubleshooting_no_improvement]] **The build performance haven’t improved after migrating to lazy API.**
It is important to note that lazy task configuration is an opt-in feature.
It means the entire configuration chain needs to be lazy to get the benefit.
If at any point, an eager API is used for a specific named task, task type or, the worst, the entire task container, all benefits are mitigated.
The first step is to have a look at the statistics to see how much more the build can be improved.
If the statistic shows the build is completely lazy (work-avoided) https://github.com/gradle/gradle/issues/5664[please contact us] so we can discuss where this feature falls short and improve it for the next Gradle version.

[[sec:task_configuration_avoidance_pitfalls]]
=== Pitfalls

* **Lazy task configuration is an opt-in feature.**
Any eager realization of a task will mitigate the benefit of this new API.
In the far future, eager realizations will become an error, until them, it’s up to the build author to avoid eager realization.

* [[task_configuration_avoidance_pitfalls_hidden_eager_task_realization]] **Beware of the hidden eager task realization.**
Gradle has lots of ways to configure a task eagerly.
`TaskContainer#getByName(String, Action)` is an explicit configuration by name.
Configuring a task as a DSL block is an alias to the previous explicit configuration, meaning a lazy task will be prematurely resolved:
+
[source,groovy]
----
// Given a task lazily created with
tasks.register "someTask"

// Some time later, the task is configured using a DSL block like
someTask {
    // The task is resolved immediately, and this closure is executed immediately
}
----
+
You will have to migrate the DSL task configuration to the following:
+
[source,groovy]
----
tasks.named("someTask").configure {
    // ...
    // Beware of the pitfalls here
}
----
+
While migrating to the lazy task configuration, you may still have eager task configuration that references a task by name using Groovy’s property to task reference syntactic sugar.
If this lookup is done from anything other than a lazy configuration action, the lazy task reference by the name will be prematurely realized:
+
[source,groovy]
----
tasks.register "someTask"

// Sometime later, an eager task is configured like
task aEagerTask {
    // The task is resolved immediately as it will be treated as a property of the project and will result in an eager task lookup by name
    dependsOn someTask
}
----
+
You can solve the following in three ways:

  - **Use `TaskProvider` variable.**
  Useful when the task is referenced multiple time in the same build script.
+
[source,groovy]
----
def someTask = tasks.register "someTask"

task aEagerTask {
    dependsOn someTask
}
----

    - **Migrate consumer task.**
    It will only work under specific circumstances.
    If the task gets resolve later or the realization may happen in a configuration action with no ways of actually knowing where the task is created or configured, this solution won't be helpful.
+
[source,groovy]
----
tasks.register "someTask"

tasks.register("aEagerTask") {
    dependsOn someTask
}
----

    - **Lookup the task lazily.**
    Useful when the task registration isn’t co-located with its eager usage.
+
[source,groovy]
----
tasks.register "someTask"

task aEagerTask {
    dependsOn tasks.named("someTask")
}
----

* **The `jar` task is eagerly realized, always.**
The following https://github.com/gradle/gradle-native/issues/730[issue is tracking this pitfall].
Even though this issue wasn't released with Gradle 4.9, you should reference the `jar` task lazily.

* [[task_configuration_avoidance_pitfall_build_scan_plugin]] **The build scan plugin `buildScanPublishPrevious` task is eager until version 1.15.** Upgrade the build scan plugin in your build to use the latest version.

[[sec:task_configuration_avoidance_backward_compatibility_migration]]
==== Supporting older versions of Gradle

This section describes two ways to keep your plugin backward compatible with older version of Gradle if you must maintain compatibility with versions of Gradle older than 4.9.

[NOTE]
====
Although backward compatibility is good for users, we still recommended to upgrade to newer Gradle releases in a timely manner. This will reduce your maintenance burden.
====

The first method to maintain compatibility is to compile your plugin against the Gradle 4.9 API and conditionally call the right APIs with Groovy (https://github.com/melix/jmh-gradle-plugin/blob/a034aa88805b7a06fa9c5a825d573554b2aa23e2/src/main/groovy/me/champeau/gradle/JMHPlugin.groovy#L289-L296[example]). 

The second method is to use Java reflection to cope with the fact that the APIs are unavailable during compilation (https://github.com/tbroyer/gradle-apt-plugin/tree/432509ec85d1ab49296d4f9b21fad876523c6a8a/src/main/java/net/ltgt/gradle/apt[example]).

It is highly recommended to have cross-version test coverage using <<test_kit.adoc#sub:gradle-runner-gradle-version, TestKit>> and multiple versions of Gradle.

[[sec:old_vs_new_configuration_api_overview]]
== Old vs New API overview

[NOTE]
====
* Methods that take a `groovy.lang.Closure` are covered in the new API with methods taking `org.gradle.api.Action`.
* More convenience methods may be added in the future based on user feedback.
* Some old API methods may never have a direct replacement in the new API.
* Some APIs may be restricted when accessed in a lazy configuration block.
====

[cols="a,a", options="header"]
|===
| Old vs New API
| Description

| Instead of: `task myTask(type: MyTask) {}`
.2+| There is not a shorthand Groovy DSL for using the new API.
| Use: `tasks.register("myTask", MyTask) {}`

| Instead of: link:{javadocPath}/org/gradle/api/tasks/TaskContainer.html#create-java.util.Map-[TaskContainer.create(java.util.Map)]
.2+| Use one of the alternatives below.
| Use: No direct equivalent.

| Instead of: link:{javadocPath}/org/gradle/api/tasks/TaskContainer.html#create-java.util.Map-groovy.lang.Closure-[TaskContainer.create(java.util.Map, groovy.lang.Closure)]
.2+| Use one of the alternatives below.
| Use: No direct equivalent.

| Instead of: link:{javadocPath}/org/gradle/api/tasks/TaskContainer.html#create-java.lang.String-[TaskContainer.create(java.lang.String)]
.2+| This returns a `TaskProvider` instead of a `Task`.
| Use: link:{javadocPath}/org/gradle/api/tasks/TaskContainer.html#register-java.lang.String-[TaskContainer.register(java.lang.String)]

| Instead of: link:{javadocPath}/org/gradle/api/tasks/TaskContainer.html#create-java.lang.String-groovy.lang.Closure-[TaskContainer.create(java.lang.String, groovy.lang.Closure)]
.2+| This returns a `TaskProvider` instead of a `Task`.
| Use: link:{javadocPath}/org/gradle/api/tasks/TaskContainer.html#register-java.lang.String-org.gradle.api.Action-[TaskContainer.register(java.lang.String, org.gradle.api.Action)]

| Instead of: link:{javadocPath}/org/gradle/api/tasks/TaskContainer.html#create-java.lang.String-java.lang.Class-[TaskContainer.create(java.lang.String, java.lang.Class)]
.2+| This returns a `TaskProvider` instead of a `Task`.
| Use: link:{javadocPath}/org/gradle/api/tasks/TaskContainer.html#register-java.lang.String-java.lang.Class-[TaskContainer.register(java.lang.String, java.lang.Class)]

| Instead of: link:{javadocPath}/org/gradle/api/tasks/TaskContainer.html#create-java.lang.String-java.lang.Class-org.gradle.api.Action-[TaskContainer.create(java.lang.String, java.lang.Class, org.gradle.api.Action)]
.2+| This returns a `TaskProvider` instead of a `Task`.
| Use: link:{javadocPath}/org/gradle/api/tasks/TaskContainer.html#register-java.lang.String-java.lang.Class-org.gradle.api.Action-[TaskContainer.register(java.lang.String, java.lang.Class, org.gradle.api.Action)]

| Instead of: link:{javadocPath}/org/gradle/api/tasks/TaskContainer.html#create-java.lang.String-java.lang.Class-java.lang.Object++...++-[TaskContainer.create(java.lang.String, java.lang.Class, java.lang.Object...)]
.2+| This returns a `TaskProvider` instead of a `Task`.
| Use: link:{javadocPath}/org/gradle/api/tasks/TaskContainer.html#register-java.lang.String-java.lang.Class-java.lang.Object++...++-[TaskContainer.register(java.lang.String, java.lang.Class, java.lang.Object...)]

| Instead of: link:{javadocPath}/org/gradle/api/tasks/TaskCollection.html#getByName-java.lang.String-[TaskCollection.getByName(java.lang.String)]
.2+| This returns a `TaskProvider` instead of a `Task`.
| Use: link:{javadocPath}/org/gradle/api/tasks/TaskCollection.html#named-java.lang.String-[TaskCollection.named(java.lang.String)]

| Instead of: link:{javadocPath}/org/gradle/api/tasks/TaskCollection.html#getByName-java.lang.String-groovy.lang.Closure-[TaskCollection.getByName(java.lang.String, groovy.lang.Closure)]
.2+| This returns a `TaskProvider` instead of a `Task`.
| Use: `named(java.lang.String).configure(Action)`

| Instead of: link:{javadocPath}/org/gradle/api/tasks/TaskContainer.html#getByPath-java.lang.String-[TaskContainer.getByPath(java.lang.String)]
.2+| Accessing tasks from another project requires a specific ordering of project evaluation.
| Use: No direct equivalent.

| Instead of: link:{javadocPath}/org/gradle/api/NamedDomainObjectCollection.html#findByName-java.lang.String-[NamedDomainObjectCollection.findByName(java.lang.String)]
.2+| `named(String)` is the closest equivalent, but will fail if the task does not exist. Using `findByName(String)` will cause tasks registered with the new API to be created/configured.
| Use: No direct equivalent.

| Instead of: link:{javadocPath}/org/gradle/api/tasks/TaskContainer.html#findByPath-java.lang.String-[TaskContainer.findByPath(java.lang.String)]
.2+| See `getByPath(String)` above.
| Use: No direct equivalent.

| Instead of: link:{javadocPath}/org/gradle/api/tasks/TaskCollection.html#withType-java.lang.Class-[TaskCollection.withType(java.lang.Class)]
.2+| This is OK to use because it does not require tasks to be created immediately.
| Use: _OK_

| Instead of: `withType(java.lang.Class).getByName(java.lang.String)`
.2+| This returns a `TaskProvider` instead of a `Task`.
| Use: `withType(java.lang.Class).named(java.lang.String)`

| Instead of: link:{javadocPath}/org/gradle/api/DomainObjectCollection.html#withType-java.lang.Class-org.gradle.api.Action-[DomainObjectCollection.withType(java.lang.Class, org.gradle.api.Action)]
.2+| This returns `void`, so it cannot be chained.
| Use: `withType(java.lang.Class).configureEach(org.gradle.api.Action)`

| Instead of: link:{javadocPath}/org/gradle/api/DomainObjectCollection.html#all-org.gradle.api.Action-[DomainObjectCollection.all(org.gradle.api.Action)]
.2+| This returns `void`, so it cannot be chained.
| Use: link:{javadocPath}/org/gradle/api/DomainObjectCollection.html#configureEach-org.gradle.api.Action-[DomainObjectCollection.configureEach(org.gradle.api.Action)]

| Instead of: link:link:{javadocPath}/org/gradle/api/tasks/TaskCollection.html#whenTaskAdded-org.gradle.api.Action-[TaskCollection.whenTaskAdded(org.gradle.api.Action)]
.2+| This returns `void`, so it cannot be chained.
| Use: link:{javadocPath}/org/gradle/api/DomainObjectCollection.html#configureEach-org.gradle.api.Action-[DomainObjectCollection.configureEach(org.gradle.api.Action)]

| Instead of: link:link:{javadocPath}/org/gradle/api/DomainObjectCollection.html#whenObjectAdded-org.gradle.api.Action-[DomainObjectCollection.whenObjectAdded(org.gradle.api.Action)]
.2+| This returns `void`, so it cannot be chained.
| Use: link:{javadocPath}/org/gradle/api/DomainObjectCollection.html#configureEach-org.gradle.api.Action-[DomainObjectCollection.configureEach(org.gradle.api.Action)]

| Instead of: link:{javadocPath}/org/gradle/api/NamedDomainObjectSet.html#findAll-groovy.lang.Closure-[NamedDomainObjectSet.findAll(groovy.lang.Closure)]
.2+| Avoid calling this method. `matching(Spec)` and `configureEach(Action)` are more appropriate in most cases.
| Use: _OK_, with issues.

| Instead of: link:{javadocPath}/org/gradle/api/tasks/TaskCollection.html#matching-groovy.lang.Closure-[TaskCollection.matching(groovy.lang.Closure)]
.2+| This is OK to use because it does not require tasks to be created immediately.
| Use: _OK_

| Instead of: link:{javadocPath}/org/gradle/api/tasks/TaskCollection.html#getAt-java.lang.String-[TaskCollection.getAt(java.lang.String)]
.2+| Avoid calling this directly as it's a Groovy convenience method. The alternative returns a `TaskProvider` instead of a `Task`.
| Use: link:{javadocPath}/org/gradle/api/tasks/TaskCollection.html#named-java.lang.String-[TaskCollection.named(java.lang.String)]

| Instead of: `iterator()` or implicit iteration over the `Task` collection
.2+| Avoid doing this as it requires creating and configuring all tasks. See `findAll(Closure)` above.
| Use: _OK_, with issues.

| Instead of: `remove(org.gradle.api.Task)`
.2+| Avoid calling this. The behavior of `remove` with the new API may change in the future.
| Use: _OK_, with issues.

| Instead of: link:{javadocPath}/org/gradle/api/tasks/TaskContainer.html#replace-java.lang.String-[TaskContainer.replace(java.lang.String)]
.2+| Avoid calling this. The behavior of `replace` with the new API may change in the future.
| Use: _OK_, with issues.

| Instead of: link:{javadocPath}/org/gradle/api/tasks/TaskContainer.html#replace-java.lang.String-java.lang.Class-[TaskContainer.replace(java.lang.String, java.lang.Class)]
.2+| Avoid calling this. The behavior of `replace` with the new API may change in the future.
| Use: _OK_, with issues.

|===

[[sec:new_task_gradle_profiler_scenario]]
== Profiling with a Gradle Profiler Scenario File

The https://github.com/gradle/gradle-profiler[Gradle Profiler] is a tool to measure build times for Gradle builds in a predictable and reproducible manner. The tool automates collecting profiling and benchmark information from a Gradle build and mitigates environmental impacts to measuring build time (like JIT warmups and cached dependencies). Clone and build `gradle-profiler` locally.

To measure the impact of the new API on your build, we've included a sample scenario file you can use. This scenario runs `gradle help` on your build with a special flag to enable/disable the new API to make it easier to measure improvements. `gradle help` approximates the time it takes Gradle to configure your build by running only a single, simple task.

.Save as help.scenario
[source,json]
----
defaults {
    tasks = ["help"]
    warm-ups = 20
}
eagerHelp = ${defaults} {
    gradle-args = ["-Dorg.gradle.internal.tasks.eager=true"]
}
lazyHelp = ${defaults} {
    gradle-args = ["-Dorg.gradle.internal.tasks.eager=false"]
}
----

Run `gradle-profiler` in the root of your build. The results will go into a file called `profile-out-N` where `N` is unique for each invocation.

* When measuring your build with `gradle-profiler`, you should make sure the machine running the benchmark is not also busy doing other things. You may get false positives/negatives if resources are spent doing other things.
* Get a baseline for how long your build takes before making any changes. Run
```
gradle-profiler --benchmark
    --iterations 20
    --gradle-version [some Gradle version]
    --scenario-file help.scenario
    eagerHelp lazyHelp
```
* In the `profile-out-N` directory, `gradle-profiler` will generate a CSV and a HTML file to display the results of the benchmarking.
* After making some changes to decrease the number of tasks that are created and configured, re-run the command above.
* For the Gradle build itself, we saw improvements after 50% of the tasks were no longer configured each time. Your mileage may vary depending on how expensive particular tasks are to create and configure.
* Please provide feedback on https://github.com/gradle/gradle/issues/5664[this issue]. Or send us an email at mailto:performance@gradle.com[performance@gradle.com].
