You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Gradle中的`this`对象是什么?附示例构建脚本分析需求

Understanding this in Gradle: A Deep Dive with Your Script Example

Great question! Let's break down exactly what this represents in a Gradle build script, using your code as a hands-on example. By the end, you'll have a clear grasp of how this behaves and why your script produces the output it does.

First, the Basics: What is this in Gradle?

In Groovy-based Gradle scripts, this directly refers to the current Project instance when working in the top-level scope of the script. Gradle executes your build script within the context of a Project object, so this and the built-in project variable are two ways to reference the exact same object. That's the core rule to remember.

Let's Analyze Your Script

First, here's your script again for reference:

task hello{ doLast{ println'Hello World!' } }
defaultTasks 'hello'
this.buildDir="binin"
project.buildDir="bout"
ext.abc="test abc"
println "buildDir is: ${this.buildDir}"
println "buildDir is: "+project.buildDir
println "project=${project}, ${project.project}, ${this==project} ${this.equals(project)}"
println "this=${this}, ${this.project}, ${this.project.equals(project)}"
println "this.ext, ${this.ext}, ${this.project.ext}, ${this.ext==this.project.ext}"

Expected Output & Line-by-Line Explanation

When you run this script, here's what you'll see (project name/hash placeholders match typical output):

buildDir is: bout
buildDir is: bout
project=root project 'my-project', root project 'my-project', true true
this=root project 'my-project', root project 'my-project', true
this.ext, org.gradle.api.internal.plugins.DefaultExtraPropertiesExtension@123456, org.gradle.api.internal.plugins.DefaultExtraPropertiesExtension@123456, true

Let's break down each line:

  1. buildDir is: bout
    • You first set this.buildDir="binin", but immediately overwrite that value with project.buildDir="bout". Since this and project are the same object, changing one changes the other. The final value of buildDir is "bout", so both this.buildDir and project.buildDir return that.
  2. buildDir is: bout
    • This confirms the above point: accessing project.buildDir directly gives the same result as this.buildDir because they reference the same property on the same object.
  3. project=root project 'my-project', root project 'my-project', true true
    • project refers to your root project (or subproject if this is a submodule). The project.project property points to the Project instance itself. The true true tells us this and project are exactly the same object—both reference equality (==) and logical equality (equals()) return true.
  4. this=root project 'my-project', root project 'my-project', true
    • this is the Project instance, so this.project also points to itself. The final true confirms this.project matches the top-level project variable.
  5. this.ext, ...@123456, ...@123456, true
    • The ext block stores custom properties in Gradle. Since this and project are the same object, this.ext and project.ext reference the same DefaultExtraPropertiesExtension instance. The equality check returns true because they're identical.

Key Characteristics of this in Gradle

  • Top-level scope = Project instance: In the main body of your build script, this is identical to project—you can use them interchangeably.
  • Nested closures change context: If you're inside a closure like the doLast block of your hello task, this will refer to the task instance instead of the Project. For example, adding println this inside doLast would output the Task object for hello, not the Project.
  • Implicit property resolution: When you access properties directly (like buildDir without this. or project.), Gradle automatically resolves them against the Project instance. So buildDir is shorthand for this.buildDir or project.buildDir.

内容的提问来源于stack exchange,提问作者fatfatson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:32:26