In standard build.gradle I can use project.getGradle().getGradleVersion() to print gradle version. How do I print gradle version in groovy script? I think I’ll have to import one of Gradle API class but which one?
Any good tutorial on using gradle API with groovy script would be appreciated.
A regular Groovy script cannot be executed with Gradle. What are you trying to achieve?
@bmuschko I would like to build a Groovy Utility class that contains some build related utility methods such as doing minimum gradle version check before the build starts. I have the utility methods already built using gradle api executable from .gradle file and working just fine. What I want to do is move these methods to a .groovy Utility class and use the project.getGradle().getGradleVersion() API. Similarly, I want to know if I can use the Gradle API in a Java Project.
In general, I would like to separate the build logic code, from the .gradle configuration itself. Wheather that is the right thing to do or not is can be debated but separately.
I could be wrong, but it appears that Gradle API is strongly tied to the “build project configuration” and it needs that to drive the Gradle API usage. If that is the case then can we have something added where we can dynamically build the “build project configuration” info and pass that to the Gradle API?
You won’t be able to execute the Gradle API without the Gradle runtime. What I’d do in your case is to write a Gradle plugin or an initialization script that executes the relevant checks.
ok, thanks. I have it working in the init script. To make it also work as a Gradle plugin as you suggested would be interesting.