学习Gradle时编写示例代码报错
示例代码如下:
task startSession << {
chant()
}
def chant() {
ant.echo(message: 'Repeat after me...')
}
3.times{
task "yayGradle$it" << {
println 'Gradle rocks'
}
}
yayGradle0.dependsOn startSession
yayGradle2.dependsOn yayGradle1, yayGradle0
task groupTherapy(dependsOn: yayGradle2)
运行报错:
PS D:\Gradle> gradle groupTherapy
FAILURE: Build failed with an exception.
* Where:
Build file 'D:\Gradle\build.gradle' line: 1
* What went wrong:
A problem occurred evaluating root project 'Gradle'.
> Could not find method leftShift() for arguments [build_2ilw3op988tm86o4ko1xhgbm0$_run_closure1@42805226] on task ':startSession' 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 1s
编译版本如下:
PS D:\Gradle> gradle -v
------------------------------------------------------------
Gradle 6.1.1
------------------------------------------------------------
Build time: 2020-01-24 22:30:24 UTC
Revision: a8c3750babb99d1894378073499d6716a1a1fa5d
Kotlin: 1.3.61
Groovy: 2.5.8
Ant: Apache Ant(TM) version 1.10.7 compiled on September 1 2019
JVM: 1.8.0_181 (Oracle Corporation 25.181-b13)
OS: Windows 10 10.0 amd64
查出问题
代码中的<<在gradle5.1之后废弃了
解决方案
将<<改为doLast
task startSession {
doLast{
chant()
}
}
def chant() {
ant.echo(message: 'Repeat after me...')
}
3.times{
task "yayGradle$it" {
doLast{
println 'Gradle rocks'
}
}
}
yayGradle0.dependsOn startSession
yayGradle2.dependsOn yayGradle1, yayGradle0
task groupTherapy(dependsOn: yayGradle2)
运行结果:
PS D:\Gradle> gradle groupTherapy
> Task :startSession
[ant:echo] Repeat after me...
> Task :yayGradle0
Gradle rocks
> Task :yayGradle1
Gradle rocks
> Task :yayGradle2
Gradle rocks
BUILD SUCCESSFUL in 1s
4 actionable tasks: 4 executed