# What this type using java code in the build script and project settings

**URL:** https://discuss.gradle.org/t/what-this-type-using-java-code-in-the-build-script-and-project-settings/43263
**Category:** Help/Discuss
**Created:** [July 4, 2022, 6:21pm UTC](https://discuss.gradle.org/t/what-this-type-using-java-code-in-the-build-script-and-project-settings/43263 "2022-07-04T18:21:09Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![justsomeone](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@justsomeone](https://discuss.gradle.org/u/justsomeone)
#### Post date: [July 4, 2022, 6:21pm UTC](https://discuss.gradle.org/t/what-this-type-using-java-code-in-the-build-script-and-project-settings/43263/1 "2022-07-04T18:21:09Z")

</div>

Hello

i am using java code instead of groovy so want to confirm that

1. inside the settings.gradle `this` will be an instance of this interface  
[Settings (Gradle API 7.4.2)](https://docs.gradle.org/current/javadoc/org/gradle/api/initialization/Settings.html)

am i right ?

1. inside the gradle.build what `this` type i tried to get the class but it output build\_6ineg7nk53oil05rsnh0evd1j but i can not find the build class in the javadoc api  
so which type this represent

2. using the command line gradle it run the task that start with this taskname it does not check if the full name match

so for example

```gradle
final Project currentProject = getProject()
final TaskContainer taskContainer = currentProject.getTasks()
final TaskProvider<Task> testTask = taskContainer.register("test")
testTask.get().doLast(action -> System.out.println("This is executed during the execution phase."))

```

running the following command

```gradle
gradle test
gradle te
gradle tes

```

all make the test task run the only one that does not work

does that mean gradle run the closest task to the given name and if yes could i get how it pick task

i also notice it do the same for `gradle ta` and `gradle tas` both run `gradle task`

thanks for your time and have a nice day 🙂

---

<div class="post-metadata">

### Author: ![Vampire](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/vampire/32/9082_2.png) [@Vampire](https://discuss.gradle.org/u/Vampire)
#### Post date: [July 5, 2022, 12:50am UTC](https://discuss.gradle.org/t/what-this-type-using-java-code-in-the-build-script-and-project-settings/43263/2 "2022-07-05T00:50:52Z")

</div>

> 1. inside the settings.gradle `this` will be an instance of this interface  
> [Settings (Gradle API 7.4.2)](https://docs.gradle.org/current/javadoc/org/gradle/api/initialization/Settings.html)
> 
> am i right ?

No, the script is compiled to a class that represents the script and that is the type of `this`.  
But most things will be forwarded to an instance of `Settings`.  
Just like with `Project` for build scripts and `Gradle` for init scripts.

> inside the gradle.build what `this` type i tried to get the class but it output build\_6ineg7nk53oil05rsnh0evd1j but i can not find the build class in the javadoc api  
> so which type this represent

See above

> ```gradle
> final TaskProvider<Task> testTask = taskContainer.register("test")
> testTask.get().doLast(action -> System.out.println("This is executed during the execution phase."))
> 
> ```

There is not much sense in using the task configuration avoidance api (`taskContainer.register`) if you right away use `get()` and thus cause the task to be realized immediately.  
Either use `configure(...)` or give a configuration closuer directly with the `register` call.

> all make the test task run the only one that does not work

Is this sentence incomplete?  
Is there for example “is t” missing or so?

> does that mean gradle run the closest task to the given name and if yes could i get how it pick task

If what you give is an excat name, it is used, otherwise if the part you give is able to uniquely identify a task, it is run, otherwise it tells you that it is ambiguous and tells you which task names do match. It also understands camelCase like `aBC` for `anotherBetterCodetask`.

> i also notice it do the same for `gradle ta` and `gradle tas` both run `gradle task`

No, it runs the `tasks`, not `task`, unless there is also a `task` task, then it will complain.

---

<div class="post-metadata">

### Author: ![justsomeone](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@justsomeone](https://discuss.gradle.org/u/justsomeone)
#### Post date: [July 5, 2022, 1:19am UTC](https://discuss.gradle.org/t/what-this-type-using-java-code-in-the-build-script-and-project-settings/43263/3 "2022-07-05T01:19:02Z")

</div>

thanks a lot for your time and help

i am still new to gradle

> [@Vampire](#):
>
> No, the script is compiled to a class that represents the script and that is the type of `this`.  
> But most things will be forwarded to an instance of `Settings`.

from the api page [Script (Gradle API 8.4)](https://docs.gradle.org/current/javadoc/org/gradle/api/Script.html)  
there no getRootProject method

for example [Build Lifecycle](https://docs.gradle.org/current/userguide/build_lifecycle.html#sec:settings_file)

to change the groovy code to java here what i did

for the settings.gradle file

```gradle
this.getRootProject().setName("basic")

System.out.println("This is executed during the initialization phase.")

```

for build.gradle

```gradle
System.out.println("This is executed during the configuration phase.")

final Project currentProject = getProject()

final TaskContainer taskContainer = currentProject.getTasks()

final TaskProvider<Task> configuredTask = taskContainer.register("configured", action ->
        System.out.println("This is also executed during the configuration phase, because :configured is used in the build.")
)

final TaskProvider<Task> testTask = taskContainer.register("test")
testTask.get().doLast(action -> System.out.println("This is executed during the execution phase."))

final TaskProvider<Task> testBothTask = taskContainer.register("testBoth", action ->
        System.out.println("This is executed during the configuration phase as well, because :testBoth is used in the build."))

testBothTask.get().doFirst(action -> System.out.println("This is executed first during the execution phase."))

testBothTask.get().doLast(action -> System.out.println("This is executed last during the execution phase."))

```

> [@Vampire](#):
>
> Either use `configure(...)` or give a configuration closuer directly with the `register` call.

each time i click on closure the site send me to groovy api page 😉

> [@Vampire](#):
>
> Is this sentence incomplete?  
> Is there for example “is t” missing or so?

sorry my bad meant that all run test task except when call `gradle t`

but you explain how it work here

> [@Vampire](#):
>
> If what you give is an excat name, it is used, otherwise if the part you give is able to uniquely identify a task, it is run, otherwise it tells you that it is ambiguous and tells you which task names do match. It also understands camelCase like `aBC` for `anotherBetterCodetask`.

thanks a lot

> [@Vampire](#):
>
> No, it runs the `tasks`, not `task`, unless there is also a `task` task, then it will complain.

you are correct my bad

some stuff i can not map to java code for example  
[https://docs.gradle.org/current/userguide/tutorial\_using\_tasks.html#sec:using\_ant\_tasks\_tutorial](https://docs.gradle.org/current/userguide/tutorial_using_tasks.html#sec:using_ant_tasks_tutorial)

can not figure how to map this statement  
`ant.loadfile(srcFile: file, property: file.name)`  
to java

is it cause i lack some knowledge or there some constrain/limitations if used java code

the problem that looks like i am solo on that way as there no help from the search engine also 🙂

thanks for your time and help and sorry for long post and comment 🙂

---

<div class="post-metadata">

### Author: ![Vampire](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/vampire/32/9082_2.png) [@Vampire](https://discuss.gradle.org/u/Vampire)
#### Post date: [July 6, 2022, 12:35am UTC](https://discuss.gradle.org/t/what-this-type-using-java-code-in-the-build-script-and-project-settings/43263/4 "2022-07-06T00:35:33Z")

</div>

> [@justsomeone](#):
>
> > [@Vampire](#):
> >
> > No, the script is compiled to a class that represents the script and that is the type of `this`.  
> > But most things will be forwarded to an instance of `Settings`.
> 
> from the api page [Script (Gradle API 7.4.2)](https://docs.gradle.org/current/javadoc/org/gradle/api/Script.html)  
> there no getRootProject method

Correct. So?

> [@justsomeone](#):
>
> for example [Build Lifecycle](https://docs.gradle.org/current/userguide/build_lifecycle.html#sec:settings_file)
> 
> to change the groovy code to java here what i did
> 
> for the settings.gradle file
> 
> ```gradle
> this.getRootProject().setName("basic")
> 
> System.out.println("This is executed during the initialization phase.")
> 
> ```

As long as you are within `build.gradle` or `settings.gradle`, you are **not** writing Java code, you are writing Groovy code.  
Groovy aimes to be **the** scripting language alongside of Java and thus usually supports most Java language constructs, so you can write Groovy code that majorly looks ike Java code.  
But you are still writing Groovy code.  
And Groovy is a highly dynamic language with duck-typing and delegating and many magic things.  
If you call `getRootProject()` on `this` it is not actually served by the script instance itself, but forwareded to the delegate of that script, so a `Settings` instance for the settings script, a `Plugin` instance for the build script, a `Gradle` instance for an init script.

> [@justsomeone](#):
>
> for build.gradle
> 
> ```gradle
> System.out.println("This is executed during the configuration phase.")
> 
> final Project currentProject = getProject()
> 
> final TaskContainer taskContainer = currentProject.getTasks()
> 
> final TaskProvider<Task> configuredTask = taskContainer.register("configured", action ->
> System.out.println("This is also executed during the configuration phase, because :configured is used in the build.")
> )
> 
> final TaskProvider<Task> testTask = taskContainer.register("test")
> testTask.get().doLast(action -> System.out.println("This is executed during the execution phase."))
> 
> final TaskProvider<Task> testBothTask = taskContainer.register("testBoth", action ->
> System.out.println("This is executed during the configuration phase as well, because :testBoth is used in the build."))
> 
> testBothTask.get().doFirst(action -> System.out.println("This is executed first during the execution phase."))
> 
> testBothTask.get().doLast(action -> System.out.println("This is executed last during the execution phase."))
> 
> ```
> 
> > [@Vampire](#):
> >
> > Either use `configure(...)` or give a configuration closuer directly with the `register` call.
> 
> each time i click on closure the site send me to groovy api page 😉

So?  
`Closure` is a Groovy class.  
But there are usually also variants that take `Action` which you can give as Java lambda.

> [@justsomeone](#):
>
> some stuff i can not map to java code for example  
> [Build Script Basics](https://docs.gradle.org/current/userguide/tutorial_using_tasks.html#sec:using_ant_tasks_tutorial)
> 
> can not figure how to map this statement  
> `ant.loadfile(srcFile: file, property: file.name)`  
> to java

That syntax is Groovy syntactic sugar to supply a `Map` with two entries.  
So if you would use Java syntax, you would build the map and give that to the method.

---

<div class="post-metadata">

### Author: ![justsomeone](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@justsomeone](https://discuss.gradle.org/u/justsomeone)
#### Post date: [July 6, 2022, 3:06pm UTC](https://discuss.gradle.org/t/what-this-type-using-java-code-in-the-build-script-and-project-settings/43263/5 "2022-07-06T15:06:58Z")

</div>

> [@Vampire](#):
>
> Groovy aimes to be **the** scripting language alongside of Java and thus usually supports most Java language constructs, so you can write Groovy code that majorly looks ike Java code.  
> But you are still writing Groovy code.

ok thanks

> [@Vampire](#):
>
> That syntax is Groovy syntactic sugar to supply a `Map` with two entries.  
> So if you would use Java syntax, you would build the map and give that to the method.

but what ant from the api (i see getAnt and ant method so which one ) and i can not find loadfile where it come from ?

that my issue not the the file and name parameter

thanks again and have a nice day 🙂

---

<div class="post-metadata">

### Author: ![Chris\_Dore](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/chris_dore/32/7592_2.png) [@Chris\_Dore](https://discuss.gradle.org/u/Chris_Dore)
#### Post date: [July 6, 2022, 4:29pm UTC](https://discuss.gradle.org/t/what-this-type-using-java-code-in-the-build-script-and-project-settings/43263/6 "2022-07-06T16:29:51Z")

</div>

> [@justsomeone](#):
>
> but what ant from the api (i see getAnt and ant method so which one ) and i can not find loadfile where it come from ?

Accessing the `ant` property from Groovy results in `getAnt()` being called. This returns an `AntBuilder`. `AntBuilder` uses the dynamic features of Groovy to allow Ant tasks to be called as methods. Since these Ant task methods are dynamic they do not appear in any JavaDoc.

The `loadfile` method calls Ant’s [loadfile task](https://ant.apache.org/manual/Tasks/loadfile.html).

Some more docs on Gradle and Ant are [here](https://docs.gradle.org/current/userguide/ant.html).

---

<div class="post-metadata">

### Author: ![justsomeone](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@justsomeone](https://discuss.gradle.org/u/justsomeone)
#### Post date: [July 6, 2022, 4:48pm UTC](https://discuss.gradle.org/t/what-this-type-using-java-code-in-the-build-script-and-project-settings/43263/7 "2022-07-06T16:48:07Z")

</div>

thanks a lot @Chris_Dore that make sense 🙂

---

<div class="post-metadata">

### Author: ![Vampire](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/vampire/32/9082_2.png) [@Vampire](https://discuss.gradle.org/u/Vampire)
#### Post date: [July 7, 2022, 12:28am UTC](https://discuss.gradle.org/t/what-this-type-using-java-code-in-the-build-script-and-project-settings/43263/8 "2022-07-07T00:28:19Z")

</div>

While Chris is right and this again is much Groovy black magic that you should not really try to Java-syntaxify, you can do things like `project.getAnt().invokeMethod("loadfile", mapWithTheArguments)`.

---

<div class="post-metadata">

### Author: ![justsomeone](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@justsomeone](https://discuss.gradle.org/u/justsomeone)
#### Post date: [July 7, 2022, 1:33pm UTC](https://discuss.gradle.org/t/what-this-type-using-java-code-in-the-build-script-and-project-settings/43263/9 "2022-07-07T13:33:15Z")

</div>

thanks a lot @Vampire

sorry i will keep annoy everyone in the forum with tons of questions 😄

for example 6 in this page [Build Script Basics](https://docs.gradle.org/current/userguide/tutorial_using_tasks.html#sec:task_dependencies)

this one

```gradle

tasks.register('taskX') {
    dependsOn 'taskY'
    doLast {
        println 'taskX'
    }
}
tasks.register('taskY') {
    doLast {
        println 'taskY'
    }
}

```

if i used java way

```gradle
final TaskContainer taskContainer = getProject().getTasks()

final Task taskX = taskContainer.register("taskX").get()

/* if you tried to add the 
  taskX.dependsOn(taskY)
  or
  taskX.dependsOn(taskContainer.getByName("taskY"))
  both will not work 
  not sure if there another way or not
 but the next one is a work around
/

taskContainer.whenTaskAdded(task -> {
    if (((Task) task).getName().equals("taskY")) {
        taskX.dependsOn(task)
    }
})

taskX.doLast(task -> System.out.println("taskX"))

final Task taskY = taskContainer.register("taskY").get()

taskY.doLast(task -> System.out.println("taskY"))

```

does that mean java code could affect the code capability or i miss something

thanks again for all your time and help and sorry for sending million of question can not promise i will stop posting questions 😂

and have a nice day 🙂

---

<div class="post-metadata">

### Author: ![Vampire](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/vampire/32/9082_2.png) [@Vampire](https://discuss.gradle.org/u/Vampire)
#### Post date: [July 7, 2022, 3:55pm UTC](https://discuss.gradle.org/t/what-this-type-using-java-code-in-the-build-script-and-project-settings/43263/10 "2022-07-07T15:55:24Z")

</div>

While again it is ridiculous to use task configuration avoidance API (`register`) and then immediately querying the provider with `get` instead of working with the provider, completely destroying task configuration avoidance, why so complicated? If you want example in Java syntax, just do it.  
`taskX.dependsOn("taskY")`.  
I personally don’t like the depending by `String` way too much, but it works the same with Java-like syntax.  
Actually almost any explicit task dependency is bad except for lifecycle tasks.
