In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
Let's first look at a piece of code in gradle:
Buildscript {repositories {jcenter ()} dependencies {classpath 'com.android.tools.build:gradle:2.1.2'}} allprojects {repositories {jcenter ()}} task clean (type: Delete) {delete rootProject.buildDir} task wrapper (type: Wrapper) {gradleVersion =' 2.10'}
The first time I saw the gradle code, I was confused.
What kind of language is this? is it a xml-like markup language or a java-like language? It's beyond me. It doesn't matter if you don't understand. You'll understand when you learn.
You now know that gradle is written in groovy, so he is the java-like language I said earlier, but how did he do it in such a cool way as above? Let's take a look at this:
First of all, you can't see it in gradle; this is because groovy supports not writing
Println 'hello world'
Let's take a look at this:
Dependencies {classpath 'com.android.tools.build:gradle:2.1.2'}
How do you interpret this in groovy? The first thing we need to know is that the method call in groovy can be omitted from ()! For you to read correctly, like the println 'hello world',ok above, it is not difficult to understand that the above dependencies is a method name, here is a method call, not a method definition. Since it is a method call, you can know that {} is actually a parameter of the closure type of groovy. And inside this closure is a method call to classpath.
Now that I've made sense of the above, let's write an example:
Def dependencies (Closure cl) {cl.call ();} def classpath (String path) {println path} dependencies {classpath 'com.android.tools.build:gradle:2.1.2'}
Look, the code is running normally.
So let's take a look at this:
Task clean (type: Delete) {delete rootProject.buildDir}
Why don't you take a look at this with the ideas above? Task is one way, and there's nothing wrong with it. Followed by two parameters? Clean and a closure? That's not right here. If it's two parameters, they need to be separated, so there's only one parameter here, which is clean. So what kind of writing is this? Here we need to understand another grammatical feature of groovy.
When the last parameter of a method is a closure, you can write the closure outside ().
Look at the example:
Def foo (String String closure cl) {cl (s)} / / foo ('hello world') {println it} foo' hello world', {println it}
The two special writing methods of the method are here, and the writing method mentioned above is the way to write it.
So it is right to understand clean as a parameter, and then take a look at clean (type: Delete). The method parameters in groovy support named parameters, which are all sorted out here. Let's write a small example to imitate the above writing:
Def task (String taskName) {println 'execute task' + taskName} def clean (Map type,Closure cl) {type.type} def delete (String path) {} Delete = 'delete'task clean (type:Delete) {delete "path"}
I reluctantly wrote similar code here, but is this really the way we understand the writing in gradle? Let's take a look at it using code tracking.
First of all, let's see if dependencies is a method.
Void dependencies (Closure var1)
Haha, it is true, and the parameter is indeed a closure.
So let's see if classpath is a way?
WTF, damn it, the result of tracking classpath is as follows:
Dependency add (String configurationName, Object dependencyNotation)
The add method is tracked, and the add method has two parameters. What the heck is this? What's even weirder is that this add method belongs to the DependencyHandler.java interface, and you read it right, it's the java interface. What the hell???
Calm down.
Let's analyze, why did this happen? is this beyond our understanding? In fact, although I don't know anything right now, I think the reason for this must be found in gradle, so let's officially open the door to learning gradle.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.