Gradle中的`this`对象是什么?附示例构建脚本分析需求
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:
buildDir is: bout- You first set
this.buildDir="binin", but immediately overwrite that value withproject.buildDir="bout". Sincethisandprojectare the same object, changing one changes the other. The final value ofbuildDiris "bout", so boththis.buildDirandproject.buildDirreturn that.
- You first set
buildDir is: bout- This confirms the above point: accessing
project.buildDirdirectly gives the same result asthis.buildDirbecause they reference the same property on the same object.
- This confirms the above point: accessing
project=root project 'my-project', root project 'my-project', true trueprojectrefers to your root project (or subproject if this is a submodule). Theproject.projectproperty points to the Project instance itself. Thetrue truetells usthisandprojectare exactly the same object—both reference equality (==) and logical equality (equals()) return true.
this=root project 'my-project', root project 'my-project', truethisis the Project instance, sothis.projectalso points to itself. The finaltrueconfirmsthis.projectmatches the top-levelprojectvariable.
this.ext, ...@123456, ...@123456, true- The
extblock stores custom properties in Gradle. Sincethisandprojectare the same object,this.extandproject.extreference the sameDefaultExtraPropertiesExtensioninstance. The equality check returns true because they're identical.
- The
Key Characteristics of this in Gradle
- Top-level scope = Project instance: In the main body of your build script,
thisis identical toproject—you can use them interchangeably. - Nested closures change context: If you're inside a closure like the
doLastblock of yourhellotask,thiswill refer to the task instance instead of the Project. For example, addingprintln thisinsidedoLastwould output theTaskobject forhello, not the Project. - Implicit property resolution: When you access properties directly (like
buildDirwithoutthis.orproject.), Gradle automatically resolves them against the Project instance. SobuildDiris shorthand forthis.buildDirorproject.buildDir.
内容的提问来源于stack exchange,提问作者fatfatson

