When I start with a new technology, usually I am starting with the latest version. So is the case of Gradle, I started with version 5.6.3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Claudius-MacBook-Pro:hello-world claudiu$ gradle -v ------------------------------------------------------------ Gradle 5.6.3 ------------------------------------------------------------ Build time: 2019-10-18 00:28:36 UTC Revision: bd168bbf5d152c479186a897f2cea494b7875d13 Kotlin: 1.3.41 Groovy: 2.5.4 Ant: Apache Ant(TM) version 1.9.14 compiled on March 12 2019 JVM: 1.8.0_25 (Oracle Corporation 25.25-b02) OS: Mac OS X 10.15 x86_64 |
I started with various tutorials and books. Almost all of them promotes the following hello world example:
1 2 3 |
task helloWorld << { println 'Hello world.' } |
and when you will type gradle helloWorld you’ll get
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Claudius-MacBook-Pro:hello-world claudiu$ gradle helloWorld FAILURE: Build failed with an exception. * Where: Build file '/Users/claudiu/GoTechCorner/gradle-samples/hello-world/build.gradle' line: 3 * What went wrong: A problem occurred evaluating root project 'hello-world'. > Could not find method leftShift() for arguments [build_fhbgk9f2icrljy85hh9cx2ov$_run_closure1@73ff6783] on task ':helloWorld' 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 |
What is happen here …
The << is a shorthand operator for the leftShift() method. In other tutorials or books you will se the >> operator (which is short hand for rightShift() method.
The thing is the << and >> operators were marked as deprecated in Gradle 4.x and removed starting with Gradle 5. This information may be found in Gradle official documentation here: https://docs.gradle.org/current/userguide/upgrading_version_4.html#changes_5.0:
<<
for task definitions no longer works. In other words, you can not use the syntaxtask myTask << { … }
.
You will need to use the doLast() method as follow:
1 2 3 4 5 |
task helloWorld { doLast() { println 'Hello world.' } } |