Gradle系列(一):Groovy基础

2024-04-07 14:04
文章标签 基础 系列 gradle groovy

本文主要是介绍Gradle系列(一):Groovy基础,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Gradle系列(一):Groovy基础

1: Groovy简介

Groovy是一种基于Java平台的动态编程语言,它结合了Python、Ruby和Smalltalk等语言的特性。下面是Groovy基础的介绍:

  1. 语法:Groovy的语法与Java类似,但更加简洁和灵活。它支持静态类型和动态类型,并且可以直接在Java代码中使用。
  2. 数据类型:Groovy支持Java的所有数据类型,包括基本类型和引用类型。此外,Groovy还提供了一些额外的数据类型,如闭包(Closure)和元编程(Metaprogramming)。
  3. 字符串处理:Groovy提供了强大的字符串处理功能。它支持使用单引号或双引号定义字符串,并且可以使用美元符号$来插入变量或表达式。
  4. 集合操作:Groovy提供了丰富的集合操作方法,如遍历、过滤、映射等。它还支持使用闭包来对集合进行处理。
  5. 元编程:Groovy支持元编程,即在运行时修改和扩展代码。通过元编程,可以动态地添加方法、属性和注解等。
  6. 脚本语言:Groovy可以作为脚本语言使用,无需编译即可直接执行。它可以与Java代码无缝集成,并且可以使用Java的类库。
  7. 测试框架:Groovy提供了一个强大的测试框架,称为Spock。Spock使用Groovy的特性来编写简洁、可读性强的测试代码。

2:gradle环境配置

这里以windows为例:

  1. 下载gradle: https://services.gradle.org/distributions/

  2. 解压到指定目录后,打开系统环境变量

  3. 在系统变量中新增GRADLE_HOME,变量值填写自己解压的目录

  4. path中新增%GRADLE_HOME%\bin。

  5. 打开cmd,运行gradle -v

  6. 可以执行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 https://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. (https://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项目的目录。)

  1. 新建test.gradle文件。(与根目录下的build.gralde同级)
  2. 在build.gradle的 buildscript 添加关联 apply from: ‘test.gradle’

3: 字符串处理

编辑test.gradle文件:

task testPrint{def str1 = 'hello'def str2 = "world"def str = str1+str2println(str)
}

编写完成后,我们可以在Terminal中执行:

gradle -q testPrint

可以看到输出如下:

helloworld

在groovy中单引号和双引号都可以定义一个字符串常量。

我们可以将这单引号和双引号定义的常量类型打印出来:

在test.gradle中添加代码如下:

println(str1.getClass().name)println(str2.getClass().name)

执行任务,输出如下:

helloworld
java.lang.String
java.lang.String

当然他们两者还是有区别的,区别如下:

  1. 单引号字符串:在单引号中定义的字符串是字面量,不会进行任何处理或替换。这意味着在单引号字符串中无法使用变量插值或转义字符。例如:

    def hello = 'hello'
    def testStr = '$hello,world'
    println(testStr)
    

    执行后,可以看到输出:

    $hello,world

  2. 双引号字符串:在双引号中定义的字符串可以进行变量插值和转义字符的处理。变量插值允许将变量的值嵌入到字符串中。例如:

    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

至于取值的方法也很简单,类似与数组,这里有些特殊的用法如:

  1. 获取指定下标数据

    println(numList[1])

  2. 访问list最后一个元素,可以指定-1.

    println(numList[-1])

  3. 访问倒数第二个元素 ,可以指定-2

    println(numList[-2])

  4. 访问第二个元素到第四个元素

    println(numList[1…3])

  5. 打印list

    println(numList)

具体的输出如下:

1
4
3
[1, 2, 3]
[0, 1, 2, 3, 4]
  1. 如果指定的正向下标大于list的长度。如numList[100]

    则会输出null,并不会如java中抛出于数组下标越界的错误。

  2. 如果是指定的负数也就是倒数的下标,如numList[-100]。

    则抛出以下错误:

    • What went wrong:
      A problem occurred evaluating script.

    Negative array index [-100] too large for array size 5

这篇关于Gradle系列(一):Groovy基础的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/882796

相关文章

从基础到高级详解Python数值格式化输出的完全指南

《从基础到高级详解Python数值格式化输出的完全指南》在数据分析、金融计算和科学报告领域,数值格式化是提升可读性和专业性的关键技术,本文将深入解析Python中数值格式化输出的相关方法,感兴趣的小伙... 目录引言:数值格式化的核心价值一、基础格式化方法1.1 三种核心格式化方式对比1.2 基础格式化示例

redis-sentinel基础概念及部署流程

《redis-sentinel基础概念及部署流程》RedisSentinel是Redis的高可用解决方案,通过监控主从节点、自动故障转移、通知机制及配置提供,实现集群故障恢复与服务持续可用,核心组件包... 目录一. 引言二. 核心功能三. 核心组件四. 故障转移流程五. 服务部署六. sentinel部署

从基础到进阶详解Python条件判断的实用指南

《从基础到进阶详解Python条件判断的实用指南》本文将通过15个实战案例,带你大家掌握条件判断的核心技巧,并从基础语法到高级应用一网打尽,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录​引言:条件判断为何如此重要一、基础语法:三行代码构建决策系统二、多条件分支:elif的魔法三、

Python WebSockets 库从基础到实战使用举例

《PythonWebSockets库从基础到实战使用举例》WebSocket是一种全双工、持久化的网络通信协议,适用于需要低延迟的应用,如实时聊天、股票行情推送、在线协作、多人游戏等,本文给大家介... 目录1. 引言2. 为什么使用 WebSocket?3. 安装 WebSockets 库4. 使用 We

从基础到高阶详解Python多态实战应用指南

《从基础到高阶详解Python多态实战应用指南》这篇文章主要从基础到高阶为大家详细介绍Python中多态的相关应用与技巧,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、多态的本质:python的“鸭子类型”哲学二、多态的三大实战场景场景1:数据处理管道——统一处理不同数据格式

MySQL数据类型与表操作全指南( 从基础到高级实践)

《MySQL数据类型与表操作全指南(从基础到高级实践)》本文详解MySQL数据类型分类(数值、日期/时间、字符串)及表操作(创建、修改、维护),涵盖优化技巧如数据类型选择、备份、分区,强调规范设计与... 目录mysql数据类型详解数值类型日期时间类型字符串类型表操作全解析创建表修改表结构添加列修改列删除列

Python 函数详解:从基础语法到高级使用技巧

《Python函数详解:从基础语法到高级使用技巧》本文基于实例代码,全面讲解Python函数的定义、参数传递、变量作用域及类型标注等知识点,帮助初学者快速掌握函数的使用技巧,感兴趣的朋友跟随小编一起... 目录一、函数的基本概念与作用二、函数的定义与调用1. 无参函数2. 带参函数3. 带返回值的函数4.

python panda库从基础到高级操作分析

《pythonpanda库从基础到高级操作分析》本文介绍了Pandas库的核心功能,包括处理结构化数据的Series和DataFrame数据结构,数据读取、清洗、分组聚合、合并、时间序列分析及大数据... 目录1. Pandas 概述2. 基本操作:数据读取与查看3. 索引操作:精准定位数据4. Group

从基础到进阶详解Pandas时间数据处理指南

《从基础到进阶详解Pandas时间数据处理指南》Pandas构建了完整的时间数据处理生态,核心由四个基础类构成,Timestamp,DatetimeIndex,Period和Timedelta,下面我... 目录1. 时间数据类型与基础操作1.1 核心时间对象体系1.2 时间数据生成技巧2. 时间索引与数据

安装centos8设置基础软件仓库时出错的解决方案

《安装centos8设置基础软件仓库时出错的解决方案》:本文主要介绍安装centos8设置基础软件仓库时出错的解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录安装Centos8设置基础软件仓库时出错版本 8版本 8.2.200android4版本 javas