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

相关文章

Java利用Spire.Doc for Java实现在模板的基础上创建Word文档

《Java利用Spire.DocforJava实现在模板的基础上创建Word文档》在日常开发中,我们经常需要根据特定数据动态生成Word文档,本文将深入探讨如何利用强大的Java库Spire.Do... 目录1. Spire.Doc for Java 库介绍与安装特点与优势Maven 依赖配置2. 通过替换

2025最新版Android Studio安装及组件配置教程(SDK、JDK、Gradle)

《2025最新版AndroidStudio安装及组件配置教程(SDK、JDK、Gradle)》:本文主要介绍2025最新版AndroidStudio安装及组件配置(SDK、JDK、Gradle... 目录原生 android 简介Android Studio必备组件一、Android Studio安装二、A

JavaScript装饰器从基础到实战教程

《JavaScript装饰器从基础到实战教程》装饰器是js中一种声明式语法特性,用于在不修改原始代码的情况下,动态扩展类、方法、属性或参数的行为,本文将从基础概念入手,逐步讲解装饰器的类型、用法、进阶... 目录一、装饰器基础概念1.1 什么是装饰器?1.2 装饰器的语法1.3 装饰器的执行时机二、装饰器的

Java JAR 启动内存参数配置指南(从基础设置到性能优化)

《JavaJAR启动内存参数配置指南(从基础设置到性能优化)》在启动Java可执行JAR文件时,合理配置JVM内存参数是保障应用稳定性和性能的关键,本文将系统讲解如何通过命令行参数、环境变量等方式... 目录一、核心内存参数详解1.1 堆内存配置1.2 元空间配置(MetASPace)1.3 线程栈配置1.

win10安装及配置Gradle全过程

《win10安装及配置Gradle全过程》本文详细介绍了Gradle的下载、安装、环境变量配置以及如何修改本地仓库位置,通过这些步骤,用户可以成功安装并配置Gradle,以便进行项目构建... 目录一、Gradle下载1.1、Gradle下载地址1.2、Gradle下载步骤二、Gradle安装步骤2.1、安

从基础到高级详解Go语言中错误处理的实践指南

《从基础到高级详解Go语言中错误处理的实践指南》Go语言采用了一种独特而明确的错误处理哲学,与其他主流编程语言形成鲜明对比,本文将为大家详细介绍Go语言中错误处理详细方法,希望对大家有所帮助... 目录1 Go 错误处理哲学与核心机制1.1 错误接口设计1.2 错误与异常的区别2 错误创建与检查2.1 基础

Spring的基础事务注解@Transactional作用解读

《Spring的基础事务注解@Transactional作用解读》文章介绍了Spring框架中的事务管理,核心注解@Transactional用于声明事务,支持传播机制、隔离级别等配置,结合@Tran... 目录一、事务管理基础1.1 Spring事务的核心注解1.2 注解属性详解1.3 实现原理二、事务事

Java中最全最基础的IO流概述和简介案例分析

《Java中最全最基础的IO流概述和简介案例分析》JavaIO流用于程序与外部设备的数据交互,分为字节流(InputStream/OutputStream)和字符流(Reader/Writer),处理... 目录IO流简介IO是什么应用场景IO流的分类流的超类类型字节文件流应用简介核心API文件输出流应用文

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

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

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

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