Gradle系列(一):Groovy基础
1: Groovy简介
Groovy是一种基于Java平台的动态编程语言,它结合了Python、Ruby和Smalltalk等语言的特性。下面是Groovy基础的介绍:
- 语法:Groovy的语法与Java类似,但更加简洁和灵活。它支持静态类型和动态类型,并且可以直接在Java代码中使用。
- 数据类型:Groovy支持Java的所有数据类型,包括基本类型和引用类型。此外,Groovy还提供了一些额外的数据类型,如闭包(Closure)和元编程(Metaprogramming)。
- 字符串处理:Groovy提供了强大的字符串处理功能。它支持使用单引号或双引号定义字符串,并且可以使用美元符号$来插入变量或表达式。
- 集合操作:Groovy提供了丰富的集合操作方法,如遍历、过滤、映射等。它还支持使用闭包来对集合进行处理。
- 元编程:Groovy支持元编程,即在运行时修改和扩展代码。通过元编程,可以动态地添加方法、属性和注解等。
- 脚本语言:Groovy可以作为脚本语言使用,无需编译即可直接执行。它可以与Java代码无缝集成,并且可以使用Java的类库。
- 测试框架:Groovy提供了一个强大的测试框架,称为Spock。Spock使用Groovy的特性来编写简洁、可读性强的测试代码。
2:gradle环境配置
这里以windows为例:
-
下载gradle: services.gradle.org/distributio…
-
解压到指定目录后,打开系统环境变量
-
在系统变量中新增GRADLE_HOME,变量值填写自己解压的目录
-
path中新增%GRADLE_HOME%\bin。
-
打开cmd,运行gradle -v
-
可以执行gralde -h命令查看gradle其他的命令:
USAGE: gradle [option...] [task...]
-?, -h, --help Shows this help message. -a, --no-rebuild Do not rebuild project dependencies. -b, --build-file Specify the build file. --build-cache Enables the Gradle build cache. Gradle will try to reuse outputs from previous builds. -c, --settings-file Specify the settings file. --configuration-cache Enables the configuration cache (off, on, or warn). Gradle will try to reuse the build configuration from previous builds. [incubating] --configure-on-demand Configure necessary projects only. Gradle will attempt to reduce configuration time for large multi-project builds. [incubating] --console Specifies which type of console output to generate. Values are 'plain', 'auto' (default), 'rich' or 'verbose'. --continue Continue task execution after a task failure. -D, --system-prop Set system property of the JVM (e.g. -Dmyprop=myvalue). -d, --debug Log in debug mode (includes normal stacktrace). --daemon Uses the Gradle Daemon to run the build. Starts the Daemon if not running. --export-keys Exports the public keys used for dependency verification. [incubating] -F, --dependency-verification Configures the dependency verification mode (strict, lenient or off) [incubating] --foreground Starts the Gradle Daemon in the foreground. -g, --gradle-user-home Specifies the gradle user home directory. -I, --init-script Specify an initialization script. -i, --info Set log level to info. --include-build Include the specified build in the composite. -M, --write-verification-metadata Generates checksums for dependencies used in the project (comma-separated list) [incubating] -m, --dry-run Run the builds with all task actions disabled. --max-workers Configure the number of concurrent workers Gradle is allowed to use. --no-build-cache Disables the Gradle build cache. --no-configure-on-demand Disables the use of configuration on demand. [incubating] --no-daemon Do not use the Gradle daemon to run the build. Useful occasionally if you have configured Gradle to always run with the daemon by default. --no-parallel Disables parallel execution to build projects. --no-scan Disables the creation of a build scan. For more information about build scans, please visit gradle.com/build-scans. --no-watch-fs Disables watching the file system. [incubating] --offline Execute the build without accessing network resources. -P, --project-prop Set project property for the build script (e.g. -Pmyprop=myvalue). -p, --project-dir Specifies the start directory for Gradle. Defaults to current directory. --parallel Build projects in parallel. Gradle will attempt to determine the optimal number of executor threads to use. --priority Specifies the scheduling priority for the Gradle daemon and all processes launched by it. Values are 'normal' (default) or 'low' [incubating] --profile Profile build execution time and generates a report in the <build_dir>/reports/profile directory. --project-cache-dir Specify the project-specific cache directory. Defaults to .gradle in the root project directory. -q, --quiet Log errors only. --refresh-dependencies Refresh the state of dependencies. --refresh-keys Refresh the public keys used for dependency verification. [incubating] --rerun-tasks Ignore previously cached task results. -S, --full-stacktrace Print out the full (very verbose) stacktrace for all exceptions. -s, --stacktrace Print out the stacktrace for all exceptions. --scan Creates a build scan. Gradle will emit a warning if the build scan plugin has not been applied. (gradle.com/build-scans) --status Shows status of running and recently stopped Gradle Daemon(s). --stop Stops the Gradle Daemon if it is running. -t, --continuous Enables continuous build. Gradle does not exit and will re-execute tasks when task file inputs change. --update-locks Perform a partial update of the dependency lock, letting passed in module notations change version. [incubating] -v, --version Print version info. -w, --warn Set log level to warn. --warning-mode Specifies which mode of warnings to generate. Values are 'all', 'fail', 'summary'(default) or 'none' --watch-fs Enables watching the file system for changes, allowing data about the file system to be re-used for the next build. [incubating] --write-locks Persists dependency resolution for locked configurations, ignoring existing locking information if it exists [incubating] -x, --exclude-task Specify a task to be excluded from execution.
下面我们就直接来简单介绍下groovy的一些基础知识:( 这里我直接使用android项目的目录。)
- 新建test.gradle文件。(与根目录下的build.gralde同级)
- 在build.gradle的 buildscript 添加关联 apply from: 'test.gradle'
3: 字符串处理
编辑test.gradle文件:
task testPrint{
def str1 = 'hello'
def str2 = "world"
def str = str1+str2
println(str)
}
编写完成后,我们可以在Terminal中执行:
gradle -q testPrint
可以看到输出如下:
helloworld
在groovy中单引号和双引号都可以定义一个字符串常量。
我们可以将这单引号和双引号定义的常量类型打印出来:
在test.gradle中添加代码如下:
println(str1.getClass().name)
println(str2.getClass().name)
执行任务,输出如下:
helloworld
java.lang.String
java.lang.String
当然他们两者还是有区别的,区别如下:
-
单引号字符串:在单引号中定义的字符串是字面量,不会进行任何处理或替换。这意味着在单引号字符串中无法使用变量插值或转义字符。例如:
def hello = 'hello' def testStr = '$hello,world' println(testStr)执行后,可以看到输出:
$hello,world
-
双引号字符串:在双引号中定义的字符串可以进行变量插值和转义字符的处理。变量插值允许将变量的值嵌入到字符串中。例如:
def hello = 'hello' def testStr = '$hello,world' println(testStr) def testStr2 = "$hello,world" println(testStr2)输出则是:
$hello,world hello,world
4:List
在groovy中定义一个list很简单,如下:
def numList = [0,1,2,3,4]
println numList.getClass().name
输出如下:
java.util.ArrayList
至于取值的方法也很简单,类似与数组,这里有些特殊的用法如:
-
获取指定下标数据
println(numList[1])
-
访问list最后一个元素,可以指定-1.
println(numList[-1])
-
访问倒数第二个元素 ,可以指定-2
println(numList[-2])
-
访问第二个元素到第四个元素
println(numList[1..3])
-
打印list
println(numList)
具体的输出如下:
1
4
3
[1, 2, 3]
[0, 1, 2, 3, 4]
-
如果指定的正向下标大于list的长度。如numList[100]
则会输出null,并不会如java中抛出于数组下标越界的错误。
-
如果是指定的负数也就是倒数的下标,如numList[-100]。
则抛出以下错误:
-
What went wrong: A problem occurred evaluating script.
Negative array index [-100] too large for array size 5
-
本文由博客一文多发平台 OpenWrite 发布!