Reputation: 9558
leftShift
is deprecated in Gradle now.
I am trying to add a java class initialization inside the gradle build file like below:
task javaClassInGradleTask {
doLast {
class Myclass {
public void message() {
System.out.println("this is message from java class");
}
}
Myclass testObject = new Myclass();
testObject.message();
}
}
and I am trying to run the file:
gradle javaClassInGradleTask
My error result is below
FAILURE: Build failed with an exception. * Where: Build file '/Users/folder/build.gradle' line: 3
What went wrong: Could not compile build file '/Users/folder/build.gradle'. startup failed: build file '/Users/folder/build.gradle': 3: Class definition not expected here. Please define the class at an appropriate place or perhaps try using a block/Closure instead. at line: 3 column: 2. File: BuildScript @ line 3, column 2. class Myclass { ^
1 error
Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
Get more help at https://help.gradle.org
BUILD FAILED in 0s
But when I try to add the class using leftShift
it works well.
Any idea to add class inside the gradle file?
Upvotes: 0
Views: 293
Reputation: 84756
It doesn't work for both doLast
and <<
. Try:
task javaClassInGradleTask {
doLast {
Myclass testObject = new Myclass()
testObject.message();
}
}
class Myclass {
public void message() {
System.out.println("this is message from java class");
}
}
Upvotes: 1