This message was deleted.
# plugin-development
s
This message was deleted.
t
AFAICT, with
includeBuild
Gradle won't automatically add the dependency.
plugins {}
implicitly adds a dependency, that Gradle will resolve to the included build. But to be able to use
apply plugin:
you'll first have to add the included build as a dependency to the
buildscript
classpath
, as with any third-party plugin.
buildSrc
is always part of the buildscript's classloader, so it Just Works™ without any additional ceremony.
e
eg.
Copy code
buildscript {
    dependencies {
        classpath("binary.name.of:included-build")
    }
}
v
Or the ususal
Copy code
plugins {
    id("...") apply false
}
But the more important question is, why do you need that? Using
apply(...)
is legacy and discouraged. You usually shouldn't use it, but always use the
plugins { ... }
block. And if there is a legit reason to not use the
plugins { ... }
block, it is most probably a bug.
p
Copy code
buildscript {
    dependencies {
        classpath("binary.name.of:included-build")
    }
}
it looks like it's not working. Tested in Gradle 8.2.1
I need to implement a plugin that conditionally attaches itself, well such a thing is possible using the syntax
apply plugin: ''
. At the same time I don't want to make any copies of the sources, etc. (as little repeated code as possible). Currently I have a
buildSrc/settings.gradle
file with the following content and it works as it should. However, I can't get the same effect with the
pluginManagement { includeBuild('plugin/') }
declaration.
Copy code
// buildSrc/settings.gradle
rootProject.projectDir = file('../')
Copy code
// build.gradle
if (gradle.rootBuild) {
    apply plugin: 'com.example.plugin'
}
Simply put, with such an implementation of the build, there is the least amount of combining with setting configurations, unnecessary subprojects, etc. I have exactly the same source code and configuration for the public part of the plugin (the one published in the repository), as well as for internal use.
The following block doesn't get the job done, because the plugin must already exist at the plugin build stage, and we know it doesn't. Only at the second build the plugin already exists. The
plugins
block does not conditionally allow plugins to be included.
Copy code
plugins {
    id 'com.example.plugin` apply false
}
v
The build is its own
buildSrc
build? That sounds pretty scary. 😱
p
I'm developing a lot of plugins that, as long as I don't make them public, I can attach between them using
includeBuild()
. Some plugins, like the documentation generation plugin attaches itself to generate documentation in HTML, based on Markdown files. If I set up the project so that the plugin sources are in the
plugin/
directory, which I include in
settings.gradle
using
includeBuild('plugin/')
, it will build itself and in the main project I can use it to generate documentation. In such a version, the comfort of working in the command line deteriorates, because I have to specify the path to the plugin tasks every time, for example
./gradlew :plugin:check
. Besides, the configuration of such a plugin deviates more from the configuration of other plugins, where the need for self-including is not present.
v
./gradlew plugincheck
Only if there are
check
task in other projects of the build that you want to not execute. 🙂 Well, try to do the
includeBuild
outside the
pluginManagement
block. I doubt it, but maybe that makes it found in the
buildscript
dependencies. If so, it would imho be strange actually though. 😄
p
I tried
includeBuild()
outside the
pluginManagement {}
block and it didn't work either. If I had to mess around with
pluginManagement { includeBuild('plugin/') }
then I think I will stay with
buildSrc/
with only one file, which is
settings.gradle
. Very simple solution and fulfills everything I need.
Full working example for plugin build definition, that skips self including if another plugin includes this pluign via
pluginManagement { includeBuild('../another-plugin/') }
.
Copy code
// buildSrc/settings.gradle
if (gradle.identityPath.path != ':buildSrc') {
    return
}

rootProject.with {
    name = gradle.parent.rootProject.name
    projectDir = file('../')
}
Copy code
// settings.gradle

if (gradle.rootBuild) {
    gradle.afterProject { p ->
        p.apply plugin: 'com.example.another-plugin'
    }
}

rootProject.name = 'another-plugin'
or instead of `settings.gradle`:
Copy code
// build.gradle

if (gradle.rootBuild) {
    apply plugin: 'com.example.another-plugin'
}