Building a buildSrc Gradle plugin that can be published
I am writing a custom Gradle plugin for my company to help with integration tests of our product. My team wants the plugin to be built and used in the main build of the product (like the buildSrc plugin), but it also needs the plugin to be published as an artifact for other teams to integrate with our product.
If I try to include it as a separate plugin in a file settings.gradle
and then also include it in buildscript as a dependency, it clearly doesn't work because the buildscript block is interpreted in the first place.
I also tried to run another assembly from the inline script like this:
buildscript {
def connection = GradleConnector.newConnector()
.forProjectDirectory(file("${project.projectDir}/theplugin"))
.connect()
try {
connection.newBuild()
.forTasks('clean', 'build', 'install')
.run()
} finally {
connection.close()
}
repositories {
mavenLocal()
...
}
dependencies {
classpath 'com.company.product.gradle.theplugin'
}
}
This causes the plugin to be created and hosted in the local Maven repository, but then the original Gradle build fails immediately afterwards because it cannot resolve the newly created archive. If I run it again it works. I don't understand this behavior.
I will probably tackle the rabbit hole with this approach. Is there a way to make this work in a less "hacky" way?
source to share
I found a hacky way to do this: symlink plugins in buildSrc (at least on * nix).
project directory
project/ buildSrc/ -> gradle_plugins/ gradle_plugins/ pluginA/ pluginB/ ... build.gradle settings.gradle ... build.gradle settings.gradle
Project /settings.gradle
include 'gradle_plugins:pluginA'
include 'gradle_plugins:pluginB'
...
Project / gradle_plugins / settings.gradle
include 'pluginA'
include 'pluginB'
...
Project / gradle_plugins / build.gradle
...
rootProject.dependencies {
runtime project(path)
}
...
source to share
I solve the following:
Regularly buildSrc/myPlugin/..
building multiple projects from In my build process, I call ./gradlew -b buildSrc/myPlugin/build.gradle uploadArchives
(or whatever task you use to publish your maven artifact).
Due to the "official" hack to add the gradle plugin to the root project runtime dependencies, this step will fail. So I surround it with try catch. I feel like it's not perfect, but it seems to work.
source to share