This message was deleted.
# plugin-development
s
This message was deleted.
j
like
Copy code
plugins { kotlin("jvm") version("x") apply(false) }
but from within a settings plugin
i see i can do this:
Copy code
target.gradle.rootProject{
            plugins {
                kotlin("jvm").version("").apply(false)
            }
        }
but that
plugins
call is marked deprecated
it also generates this error:
Copy code
Using 'plugins(PluginDependenciesSpec.() -> Unit): Nothing' is an error. The plugins {} block must not be used here. If you need to apply a plugin imperatively, please use apply<PluginType>() or apply(plugin = "id") instead.
a
j
i can try this... i have had issues in the past with using pluginManagement for the kotlin jvm plugin, and applying it on root project with apply false solved my issue
i tried
Copy code
target.gradle.beforeSettings {
            pluginManagement {
                plugins {
                    id("org.jetbrains.kotlin.jvm").version("1.9.20")
                }
            }
        }
then
Copy code
target.gradle.beforeProject {
               plugins.apply("org.jetbrains.kotlin.jvm")
 }
and got
Copy code
> Plugin with id 'org.jetbrains.kotlin.jvm' not found.
a
what about if you try without
target.gradle.beforeSettings {}
?
j
same
v
pluginManagement { plugins { ... } }
only defines default versions if the plugin is applied in a plugins block somewhere without version, it does not add it to any classpath, so
apply()
cannot find it
Just add the plugin to the classpath of the settings script, for example with "apply false" in the plugins block (not inside plugin management). The settings script class loader is a parent of the build script class loader
Then you should be able to apply it by only using the id as it then is available in the class loader already
That it is not a settings plugin is irrelevant as you do not try to apply it
j
ok that first thing you said makes sense to me... and I guess I can't do
Copy code
target.gradle.beforeProject {
      plugins {
                    kotlin("jvm")
                }
}
but as for adding the plugin to the classpath of settings
i dont see how to do that from a setting plugin...
v
Just depend on it
runtimeOnly
for example if you do not use the classes otherwise
j
ah, well i am trying to expose a settings extension with a property to define the plugin version
there is just no way to invoke
plugins { }
from a settings plugin apparently?
v
No
Then add the plugin to the buildscript configuration
classpath
just like before the
plugins { ... }
block
That should work I think
j
i tried
Copy code
target.gradle.settingsEvaluated {
            target.buildscript.dependencies.add("classpath", "org.jetbrains.kotlin:kotlin-gradle-plugin:"+extension.kotlinVersion.get())

}
and that did not work
tried
Copy code
target.gradle.settingsEvaluated {
            target.gradle.rootProject.buildscript {
                dependencies {
                    classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:"+extension.kotlinVersion.get())
                }
            }
and got
The root project is not yet available for build.
ok this worked:
Copy code
target.gradle.rootProject {
            buildscript {
                repositories {
                    mavenCentral()
                }
                dependencies {
                    classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:"+extension.kotlinVersion.get())
                }
            }
        }
thanks everyone for your help
I think there is still a functionality gap here though. If i have an initscript/settings plugin that sets up pluginManagement for a bunch of plugins, there is no way to also have a convention plugin (settings or project) that tells projects to apply a plugin by ID, using whichever version was declared in the pluginManagement
m
if the plugin is on the classpath you should be able to just apply it via
project.pluginManager.apply
?
I do this in my
KotlinConvention
:
Copy code
class KotlinConvention : Plugin<Project> {
  override fun apply(target: Project) {
    with(target) {
      with(pluginManager) {
        apply("org.jetbrains.kotlin.jvm")
        apply("org.jetbrains.kotlinx.kover")
        apply("com.squareup.sort-dependencies")
      }
      // configure other stuff
    }
  }
}
j
Getting it on the classpath is the thing i cant do
m
you can put it on the settings classpath?
e.g. this in your
settings.gradle.kts
?
Copy code
plugins {
  id("org.jetbrains.kotlin.jvm") version "1.9.22" apply false
}
j
Apply false isnt needed in pluginManagement. And i can do the equivalent of that from my settings plugin, but that alone does not add it to the class patg
m
no, I mean in the top level of
settings.gradle.kts
?
v
That is not what he want though, if you read the last few comments
m
ah, but he already did manage to place things on the classpath via the
target.gradle.rootProject.buildscript
bits?
v
He wants the user of his settings plugin to specify a version for the plugin in the settings extension and then apply that version of the plugin to the project
j
Basically yeah
In my case, the "user" is an init script in a custom distribution
m
you could maybe hack with something like this:
Copy code
gradle.beforeProject {
  if (this == this.rootProject) {
    buildscript.dependencies.add("classpath", "org.jetbrains.kotlin.jvm:org.jetbrains.kotlin.jvm.gradle.plugin:1.9.22")
  }
}
j
But then it doesn't inherit version from pluginManagement
m
you definitely don't want to have a
buildscript {}
closure, as Gradle will try to lift these out
in that case you don't even need to specify a version in
pluginManagement
whatever you put in
classpath
will become the version
j
Yeah, but i already have a standard init script that sets the plugin management, and im trying to make a convention plugin that can interop with that
If i could start over, my custom wrapper init script would only manage my settings plugins, which are convention plugins, and they would depend on the other plugins
m
I think then you're out of luck. Afaik there's no way to read what was configured in the pluginManagement dsl
j
But as it stands my wrapper init manages all of my project level plugins too
Yeah I was just hoping to invoke the plugins{} closure on a project from a settings plugin
v
That's the point. It is not "a plugin closure". It is a
plugins { ... }
block, that is extracted from the build script and investigated and evaluated separately, as the block present in the build script, not as result of a call with a closure.