The gradle script is developed using the Groovy language. As the saying goes, a language is a tool. If we want to write a gradle script, we need to learn this language first.
1. What is DSL?
1. Concept
DSL: Domain specific language, short for domain specific language. The groovy language is one of the branches of DSL.
2. Common DSL type languages
- matlab: only used for calculations
- UML: used for modeling
- html: Make a webpage that the browser can recognize
- and many more…
Groovy in this section: The language used for scripting is similar to python.
3. The difference between DSL language and general programming language
DSL: Solve problems in a specific field related to computers.
Universal programming language: I hope to solve all the problems. For example, java can be used as android and also hope to be the background.
ps: The core idea of DSL: seeking expertise but not perfection, solving specific problems.
Second, get to know Groovy for the first time
1. Know
1. JVM-based agile development language (the compiler can not only compile groovy source files into classes and execute them. It can also directly interpret and execute groovy source files)
2. Groovy can be perfectly combined with java, and all java libraries (such as native api library, or third-party library Gson, etc.)
3. Combines many powerful features of py, ruby and smalltalk.
2. Language features:
1. Syntactically, it supports new-generation language features such as dynamic typing and closures.
2,Seamlessly integrate all existing java class libraries
3. Support both object-oriented (writing applications) and process-oriented programming (writing scripts)
3. Advantages:
1. A more agile programming language
2. Easy to get started and powerful
3. It can be used as a programming language or a scripting language.
4.Proficient in java is very easy to use
Third, the construction of the environment
Environment construction:
1. Install jdk environment
2. Download SDK from official website
3. Environment variable configuration
1. Install jdk
Because groovy relies on jvm, jdk still needs to be installed.
The configuration and installation of the jdk environment must have been installed countless times for java and android development, so I will not give it here. Just find a blog and follow the steps to install and configure.
2. SDK download and its environment variable configuration
Since the blogger is using win10, I will introduce win as the process (mac and linux and win steps are similar)
(1) Download
Find the download section and download it.
ps: here pay attention to the dependency between the sdk version and jdk version
(2) Groovy SDK directory understanding
Unzip the downloaded directory:
1. lib: mainly contains some jar packages
2. bin: mainly some commands
- groovy: execute class file
- groovyc: Similar to javac, compile groovy source files into class files. For jvm execution.
- groovysh: directly interpret and execute groovy source files. (Java does not have javash)
3、doc:
- api : api 文档
- documents: html web pages, official tutorials.
(3) Configure groovy environment variables
That is, if the path of the bin directory is in the Path of the system
Edit the Path of the system and add; %GroovyHome% can be
ps: If you don't want to create a GroovyHome, directly edit the path of the system and add the path of the bin directory.
Test environment variables (test in cmd)
groovy -version
3. Use idea to develop Groovy's hello world
(1) Download of idea
You can download it from the official website, and the basic version can satisfy groovy development (you can download it all the way to the next installation). Don't worry about downloading a cracked version.
(2) Installation of Groovy plug-in
The latest version of idel installs this plugin by default (restart the compiler after the new installation)
(3)hello world
All great languages are learned from Hello World, and here we follow the trend. . . Hehe! ! !
Seamless connection with java, you can use java syntax to write Groovy source program without learning:
/**
* Created by sunnyDay on 2019/10/8 17:58
*/
class HelloGroovy {
public static void main(String[] args) {
System.out.println("hello groovy") ;
}
}
Damn it! ! ! This is clearly the java syntax.
As above:
1. Public can be omitted
2. String[] can also be omitted
3. Semicolon can be omitted
In fact, it can be written like this (in script)
/**
* Created by sunnyDay on 2019/10/8 17:58
*/
//class HelloGroovy {
// public static void main(String[] args) {
// System.out.println("hello groovy")
// }
//}
println("hello groovy") // 直接以脚本的方式
You can view the class file in Out-production-HelloGroovy-HelloGroovy.class
end
Everything is ready, then you can learn syntactic sugar. . . .