How parenthesis in Gradle task method in Project interpreted


(Cheung Chau) #1

Hi all,

I am new to this forum and sure this may have asked number of times but I couldn’t think of good keywords to search for its answer.

How Gradle translate the follwing

task('myCopy', type: Copy)

to

task myCopy(type: Copy)

None of the groovy syntax I know can help me do that. Unless, task method return a callable Task that can take named parameter.

Here are 4 ways to call task defined in Project Interface

task(args: Map<String,?>, name:String)
task(args: Map<String,?>, name:String, c:Closure)
task(name: String)
task(name: String, c:Closure)

I understand that parenthesis is option and {} is the closure body. Also AbstractProject in org.gradle.api.internal.project allow you to pass in an Object instead of a String

public Task task(Object task, Closure configureClosure) {
     return task(task.toString(), configureClosure);
}

However, I still have a difficult time to see how Gradle/Groovy allow me to go from task('myCopy', type: Copy) to task myCopy(type: Copy)

Thanks


(Cheung Chau) #2

I think Mark answer it at StakOverflow. Mark didn’t explain it in detail but I guess the answer is that Gradle did a AST on build.gradle and convert task('myCopy', type: Copy) to task myCopy(type: Copy). Also, he mentioned not all Gradle script a valid Groovy code. Am I getting it right?


(Cheung Chau) #3

Instead of getting answer, I got an invitation to training class from Gradle inc. All I can say is I will never trust Ken Kousen again, who praise this forum as being responsive. haha :joy:

Anyways, I read the recommendation for what to post and saw that we should read the documentation first and propose any improvement that might clarify the issue for others. Here is my suggestion: in the doc Example 17.2 showed one way to write a copy task and in Example 17.7, it switched to another way to do the same thing without establish the link between the two.