I have a Gradle build script (build.gradle
), in which I created some tasks. These tasks consist mostly of method calls. The called methods are also in the build script.
Now, here's the situation:
I am creating a fair amount of build scripts, which contain different tasks, but utilise the same methods from the original script. Thus, I would like to extract these "common methods" in some way, so I can easily re-use them instead of copying them for each new script I create.
If Gradle were PHP, something like the following would be ideal:
//script content ... require("common-methods.gradle"); ... //more script content
But of course, that isn't possible. Or is it?
Anyway, how can I achieve this result? What is the best possible method to do this? I've read through the Gradle documentation already, but I can't seem to determine which method will be the easiest and best suited for this.
Thanks in advance!
UPDATE:
I've managed to extract the methods in another file
(using apply from: 'common-methods.gradle'
),
so the structure is as follows:
parent/ /build.gradle // The original build script /common-methods.gradle // The extracted methods /gradle.properties // Properties used by the build script
After executing a task from build.gradle
, I've bumped into a new problem: apparently, methods don't get recognized when they're in common-methods.gradle
.
Any ideas on how to fix that?
buildscript: This block is used to configure the repositories and dependencies for Gradle. dependencies: This block in buildscript is used to configure dependencies that the Gradle needs to build during the project.
> gradle -q test Adding test task to project ':project-a' Running tests for project ':project-a' This example uses method Project. afterEvaluate() to add a closure which is executed after the project is evaluated. It is also possible to receive notifications when any project is evaluated.
buildSrc is a directory at the Gradle project root, which can contain our build logic. This allows us to use the Kotlin DSL to write our custom build code with very little configuration and share this logic across the whole project.
The doLast creates a task action that runs when the task executes. Without it, you're running the code at configuration time on every build.
Building on Peter's answer, this is how I export my methods:
Content of helpers/common-methods.gradle
:
// Define methods as usual def commonMethod1(param) { return true } def commonMethod2(param) { return true } // Export methods by turning them into closures ext { commonMethod1 = this.&commonMethod1 otherNameForMethod2 = this.&commonMethod2 }
And this is how I use those methods in another script:
// Use double-quotes, otherwise $ won't work apply from: "$rootDir/helpers/common-methods.gradle" // You can also use URLs //apply from: "https://bitbucket.org/mb/build_scripts/raw/master/common-methods.gradle" task myBuildTask { def myVar = commonMethod1("parameter1") otherNameForMethod2(myVar) }
Here's more on converting methods to closures in Groovy.
It isn't possible to share methods, but you can share extra properties containing a closure, which boils down to the same thing. For example, declare ext.foo = { ... }
in common-methods.gradle
, use apply from:
to apply the script, and then call the closure with foo()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With