加入收藏 | 设为首页 | 会员中心 | 我要投稿 汽车网 (https://www.0577qiche.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 教程 > 正文

如何创建一个 Gradle 的 Hello World 程序

发布时间:2023-05-17 14:24:49 所属栏目:教程 来源:
导读:下面我们就来创建一个 Gradle 的 Hello World 程序。我们这里以 Windows 平台为例。

在D:\gradleProjects\demo创建一个build.gradle文件,输入以下代码:
task hello {
doLast {
println 'Hel
下面我们就来创建一个 Gradle 的 Hello World 程序。我们这里以 Windows 平台为例。

在D:\gradleProjects\demo创建一个build.gradle文件,输入以下代码:
task hello {
    doLast {
        println 'Hello World!'
    }
}
然后在控制台进入到当前目录,输入命令gradle -q hello,我们看到就会打印出“Hello World!”。

为了能够让各种语言的项目更快的构建完成,所以就诞生了 Gradle,我们前面介绍过他是一个构建脚本基于 Groovy 或是 Kotlin DSL 编写的构建工具。
在 Gradle 中task和action是其非常重要的两个元素。在上面的代码中,hello是一个task,也就是一个原子任务。doLast是一个action。就是task执行完成后就会回调这个action。对于上面的代码在低版本时,我们还可以简化它的写法:

task hello << {
    println 'Hello World!'
}
但是,在最新的版本已经不支持该写法了,如果在 5.0 以上的版本,我们还按照这样写编译器会报以下错误:

FAILURE: Build Failed with an exception.
* Where:
Build file 'D:\gradleProjects\demo\build.gradle' line: 
* What went wrong:
A problem occurred evaluating root project 'demo'.
> Could not find method leftShift() for arguments [build_6odtt7gwzdon1qbfuzq3t7iwz$_run_closure1@6c146124] on task ':hello' of type org.gradle.api.DefaultTask.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD Failed in 872ms

 

(编辑:汽车网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章