convention-plugin -- I'm trying to generate a con...
# community-support
d
convention-plugin -- I'm trying to generate a conventions plugin that includes common quality pluginIns and other assorted objects. based on convention plugins section here: https://docs.gradle.org/current/userguide/custom_plugins.html?_ga=2.49430053.2413722[…]a_7W7NC6YNPT*MTc0MTk4MTIyOS4xNDEuMC4xNzQxOTgxMjMxLjU4LjAuMA.. i've tried both a buildSrc/src/main/groovy/my-plugin.gradle as well as src/main/groovy/my-plugin.gradle along with
Copy code
gradlePlugin {
    plugins {
        myConfigPlugin {
            id = 'com.my.config-plugin'
            implementationClass = 'my-plugin'
        }
    }
}
but there is no my-plugin class included in the jar that is generated, so when i publish locally and try to pull it into another project to share the settings, the implementationClass is not found any ideas how to resolve this?
v
For pre-compiled script plugins (no matter whether Kotlin DSL or Groovy DSL), you do not need an entry in
gradlePlugin { plugins { ... } }
, the plugin ID is derived from the package in the pre-compiled script plugin and the filename of the precompiled script plugin, unless you want to have the plugin available under multiple IDs. Just two points, as you want to use Groovy DSL precompiled script plugins, you have to apply the plugin that enables support for them in that build which you probably did not. And once you did, you should adjust the plugin ID, because currently it would be
my-plugin
(unless there is a package declaration in the file and plugin IDs without dot (
.
) should stay reserved for built-in plugins usually.
I'll have to check what the plugin was that enabled Groovy DSL precompiled script plugins, but my recommendation is to use Kotlin DSL everywhere. By now Kotlin DSL is the default, you immediately get type-safe build scripts, actually helpful errors if you mess up the syntax, and amazingly better IDE support if you use a good IDE like IntelliJ IDEA or Android Studio. 🙂
groovy-gradle-plugin
is the plugin you need for Groovy DSL precompiled script plugins
d
i had used groovy-gradle-plugin, but i'll redo in kotlin and see what happens
converted to kotlin, but still receive:
Task :jar
🫙 A valid plugin descriptor was found for com.my-config.properties but the implementation class my-config.gradle.kts.MyConfigPlugin was not found in the jar.
v
The implementation class does not really look correct. Do you still try to set the implementation class manually somehow?
d
i do not set the implmentationClass
the plugin gradle file is located in buildSrc/main/kotlin/my-config.gradle.kts
v
Do you mean
buildSrc/src/main/kotlin
?
d
yes, sorry
v
Can you share an MCVE? It starts to become hard to guess where your error is without seeing the project. 🙂
d
let me make it generic and i'll copy the main bits
👌 1
./build.gradle.kts:
Copy code
plugins {
    kotlin("jvm") version "2.1.10"
    id("java-gradle-plugin")
    id("maven-publish")

    id("com.jfrog.artifactory") version "5.+"
}

group = "com.mycompany.myproduct"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

dependencies {
    testImplementation(platform("org.junit:junit-bom:5.+"))
    testImplementation("org.junit.jupiter:junit-jupiter")
}

tasks.test {
    useJUnitPlatform()
}
./buildSrc/src/main/kotlin/my-config.gradle.kts:
Copy code
plugins {
    id("checkstyle")
    id("java-gradle-plugin")
    id("jacoco")
    id("maven-publish")
    id("pmd")
}

group = "com.mycompany.myproduct"
description = "Gradle Plugin containing Common myproduct Configuration settings"
version = "1.0.0"

repositories {
    maven { url = uri("<https://mycompany.repository/java>") }
    mavenLocal()
}

dependencies {
    testImplementation platform("org.junit:junit-bom:5.+")
    testImplementation "org.junit.jupiter:junit-jupiter"
}
v
Can you please knit an MCVE? Those snippets are neither C nor V 😉
d
will do. thought the 2 actual files contents would be enough
v
Unlikely, you do not even apply the plugin anywhere
Besides that using
mavenLocal()
is a very bad idea. At least it is the last one, but it should practically never be used and if, then only with a repository content filter. 🙂
But that is not related to your problem
d
another problem -- gradle wrapper distributionUrl is set to an internal URL. during prepareKotlinBuildScriptModel compile it is still trying to use services.gradle.org URL - is there another location it needs so kotlin will see the internal URL?
attached is the generic project - thanks
v
Unfortunately the URL to download the sources for the IDE integration from is hard-coded to the
<http://services.gradle.org|services.gradle.org>
URL and currently not configurable: https://github.com/gradle/gradle/issues/27863 To work-around this, you would need to use an
-all
distribution instead of a
-bin
distribution as that already contains the sources so they don't need to be downloaded.
And regarding your MCVE, you configure your main build to produce a Gradle plugin, but the code is in the
buildSrc
build.
buildSrc
is a complete own build that "happens to run" before your main build and the result added to the buildscript classpath of your main build. You must not mix up those two builds. If you want to customize the
buildSrc
build, you have to do it in the build scripts of that build, not in your main build. So if you just remove the
id("java-gradle-plugin")
and the whole
gradlePlugin { ... }
block from your main build script, and instead add a build script to your
buildSrc
build where you apply the
kotlin-dsl
plugin and define a repository, it will work better. Better in this case means that
buildSrc
will fail to compile, as your plugin is not proper Kotlin syntax.
Besides, that you should change the file name to have a dot in the plugin id
d
thanks for the feedback, i'll test that out
👌 1
The kotlin 'commonconfigplugin' class has code in it that relies on org.gradle.api classes - how do those get imported? id("java-gradle-plugin") does not work
v
What is "The kotlin 'commonconfigplugin' class"? What does "does not work" mean / manifest / where? And why do you apply it anyway?
d
in the convention plugin, i have checkstyle & pmd - need to call those to have the convention plugin apply to the project that it is running in - unless there is another way to have these convention plugins apply to the consuming project without any code?
v
I'm still not getting what you try to say or what you configured and how. Maybe share another MCVE. You can also just push to some GitHub repo so you can update it with your questions changing
d
CommonConfigPlugin class included here in order to run quality tools within projects that will consume this plugin
v
CommonConfigPlugin is not a source file, as you still do not configure Kotlin for the
buildSrc
build as I said before
As you still apply the plugin dev plugin on the main build, what actually do you try to do?
do you try to develop plugins in the main build there that are then used by other projects in the company?
Or do you try to use
buildSrc
in one build to configure all the projects in that one build uniformly?
I guess the former, but then having the class in the
buildSrc
build is not what you want, because what you have in
buildSrc
is for usage in the main build of exactly that build you are running, not to develop something you publish and other builds are using.
d
i'm trying to develop this plugin to apply to other projects in the company so all projects use the same configuration of checkstyle/jacoco/pmd in this case how do i configure the plugin to allow it to run in other projects with the same configuration settings, calling the checkstyle and pmd tasks? sounds like i don't need buildSrc at all?
v
Yes, as I said.
buildSrc
is to develop build logic that you use in that very same work-tree. If you for example have a multi-project build and there want to centralize build logic. To publish a plugin other projects are using, you develop the code in the main build of course.
d
is this CommonConfigPlugin code needed at all in that case? or will including this plugin automatically add these common plugins to the other builds?
(btw, thanks for your patience)
👌 1
v
I don't know what you try to ask. What do you mean by "including"? Do you mean "applying"? And if so, applying what?
d
in another project, if i add/apply my common plugin, will it automatically run the tasks associated with checkstyle and PMD or is there more I need to do to add them to the build -- dependsOn(), etc
i thought adding the implementationClass = CommonConfigPlugin applied the tasks for checkstyle/pmd
v
Whenever you do
dependsOn
(unless on the left-hand side is a lifecycle task) you do something wrong
i thought adding the implementationClass = CommonConfigPlugin
That does only tell the plugin dev plugin what implementation class to use for the given id
In the consuming project you would then apply the plugin by id which causes the plugin's
apply
method to be executed
d
ok, so I need an apply method within my plugin?
v
Copy code
plugins {
    id("com.my.config-plugin") version "1.2.3"
}
ok, so I need an apply method within my plugin?
well, yes, that is where you do the work
d
and that lives right in the plugin's build.gradle.kts
v
Maybe you should read a bit of documentation and maybe have a look at samples
It seems you have really no idea at all what you are doing 🙂
Really, not trying to be mean
and that lives right in the plugin's build.gradle.kts
?
d
there really isn't much in the plugin documentation regarding this
v
What you have in the plugin's
build.gradle.kts
is, what is used to build the plugin
It has nothing to do with what your plugin does when it is applied somewhere
there really isn't much in the plugin documentation regarding this
Then you probably didn't look at the correct pages