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

相关文章

gradle安装和环境配置全过程

《gradle安装和环境配置全过程》本文介绍了如何安装和配置Gradle环境,包括下载Gradle、配置环境变量、测试Gradle以及在IntelliJIDEA中配置Gradle... 目录gradle安装和环境配置1 下载GRADLE2 环境变量配置3 测试gradle4 设置gradle初始化文件5 i

MySQL中my.ini文件的基础配置和优化配置方式

《MySQL中my.ini文件的基础配置和优化配置方式》文章讨论了数据库异步同步的优化思路,包括三个主要方面:幂等性、时序和延迟,作者还分享了MySQL配置文件的优化经验,并鼓励读者提供支持... 目录mysql my.ini文件的配置和优化配置优化思路MySQL配置文件优化总结MySQL my.ini文件

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

零基础学习Redis(10) -- zset类型命令使用

zset是有序集合,内部除了存储元素外,还会存储一个score,存储在zset中的元素会按照score的大小升序排列,不同元素的score可以重复,score相同的元素会按照元素的字典序排列。 1. zset常用命令 1.1 zadd  zadd key [NX | XX] [GT | LT]   [CH] [INCR] score member [score member ...]

科研绘图系列:R语言扩展物种堆积图(Extended Stacked Barplot)

介绍 R语言的扩展物种堆积图是一种数据可视化工具,它不仅展示了物种的堆积结果,还整合了不同样本分组之间的差异性分析结果。这种图形表示方法能够直观地比较不同物种在各个分组中的显著性差异,为研究者提供了一种有效的数据解读方式。 加载R包 knitr::opts_chunk$set(warning = F, message = F)library(tidyverse)library(phyl

【生成模型系列(初级)】嵌入(Embedding)方程——自然语言处理的数学灵魂【通俗理解】

【通俗理解】嵌入(Embedding)方程——自然语言处理的数学灵魂 关键词提炼 #嵌入方程 #自然语言处理 #词向量 #机器学习 #神经网络 #向量空间模型 #Siri #Google翻译 #AlexNet 第一节:嵌入方程的类比与核心概念【尽可能通俗】 嵌入方程可以被看作是自然语言处理中的“翻译机”,它将文本中的单词或短语转换成计算机能够理解的数学形式,即向量。 正如翻译机将一种语言

【Linux 从基础到进阶】Ansible自动化运维工具使用

Ansible自动化运维工具使用 Ansible 是一款开源的自动化运维工具,采用无代理架构(agentless),基于 SSH 连接进行管理,具有简单易用、灵活强大、可扩展性高等特点。它广泛用于服务器管理、应用部署、配置管理等任务。本文将介绍 Ansible 的安装、基本使用方法及一些实际运维场景中的应用,旨在帮助运维人员快速上手并熟练运用 Ansible。 1. Ansible的核心概念

AI基础 L9 Local Search II 局部搜索

Local Beam search 对于当前的所有k个状态,生成它们的所有可能后继状态。 检查生成的后继状态中是否有任何状态是解决方案。 如果所有后继状态都不是解决方案,则从所有后继状态中选择k个最佳状态。 当达到预设的迭代次数或满足某个终止条件时,算法停止。 — Choose k successors randomly, biased towards good ones — Close

flume系列之:查看flume系统日志、查看统计flume日志类型、查看flume日志

遍历指定目录下多个文件查找指定内容 服务器系统日志会记录flume相关日志 cat /var/log/messages |grep -i oom 查找系统日志中关于flume的指定日志 import osdef search_string_in_files(directory, search_string):count = 0

音视频入门基础:WAV专题(10)——FFmpeg源码中计算WAV音频文件每个packet的pts、dts的实现

一、引言 从文章《音视频入门基础:WAV专题(6)——通过FFprobe显示WAV音频文件每个数据包的信息》中我们可以知道,通过FFprobe命令可以打印WAV音频文件每个packet(也称为数据包或多媒体包)的信息,这些信息包含该packet的pts、dts: 打印出来的“pts”实际是AVPacket结构体中的成员变量pts,是以AVStream->time_base为单位的显