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

相关文章

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

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

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

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

Linux基础命令@grep、wc、管道符的使用详解

《Linux基础命令@grep、wc、管道符的使用详解》:本文主要介绍Linux基础命令@grep、wc、管道符的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录grep概念语法作用演示一演示二演示三,带选项 -nwc概念语法作用wc,不带选项-c,统计字节数-

python操作redis基础

《python操作redis基础》Redis(RemoteDictionaryServer)是一个开源的、基于内存的键值对(Key-Value)存储系统,它通常用作数据库、缓存和消息代理,这篇文章... 目录1. Redis 简介2. 前提条件3. 安装 python Redis 客户端库4. 连接到 Re

SpringBoot基础框架详解

《SpringBoot基础框架详解》SpringBoot开发目的是为了简化Spring应用的创建、运行、调试和部署等,使用SpringBoot可以不用或者只需要很少的Spring配置就可以让企业项目快... 目录SpringBoot基础 – 框架介绍1.SpringBoot介绍1.1 概述1.2 核心功能2

Spring Boot集成SLF4j从基础到高级实践(最新推荐)

《SpringBoot集成SLF4j从基础到高级实践(最新推荐)》SLF4j(SimpleLoggingFacadeforJava)是一个日志门面(Facade),不是具体的日志实现,这篇文章主要介... 目录一、日志框架概述与SLF4j简介1.1 为什么需要日志框架1.2 主流日志框架对比1.3 SLF4

Spring Boot集成Logback终极指南之从基础到高级配置实战指南

《SpringBoot集成Logback终极指南之从基础到高级配置实战指南》Logback是一个可靠、通用且快速的Java日志框架,作为Log4j的继承者,由Log4j创始人设计,:本文主要介绍... 目录一、Logback简介与Spring Boot集成基础1.1 Logback是什么?1.2 Sprin

Gradle在国内配置镜像加速的实现步骤

《Gradle在国内配置镜像加速的实现步骤》在国内使用Gradle构建项目时,最大的痛点就是依赖下载贼慢,甚至卡死,下面教你如何配置国内镜像加速Gradle下载依赖,主要是通过改写repositori... 目录引言一、修改 build.gradle 或 settings.gradle 的 reposito

MySQL复合查询从基础到多表关联与高级技巧全解析

《MySQL复合查询从基础到多表关联与高级技巧全解析》本文主要讲解了在MySQL中的复合查询,下面是关于本文章所需要数据的建表语句,感兴趣的朋友跟随小编一起看看吧... 目录前言:1.基本查询回顾:1.1.查询工资高于500或岗位为MANAGER的雇员,同时还要满足他们的姓名首字母为大写的J1.2.按照部门

Gradle下如何搭建SpringCloud分布式环境

《Gradle下如何搭建SpringCloud分布式环境》:本文主要介绍Gradle下如何搭建SpringCloud分布式环境问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录Gradle下搭建SpringCloud分布式环境1.idea配置好gradle2.创建一个空的gr