Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to load class Plugin

I'm trying to generate first plugin but have this error:
Could not find implementation class 'CommonPluginClass' for plugin 'common-plugin' specified in jar:file
org.gradle.api.plugins.InvalidPluginException: An exception occurred applying plugin request [id: 'common-plugin']

enter image description here
this is my CommonPluginClass:

class CommonPluginClass: Plugin<Project> {

    override fun apply(project: Project) {
        project.task("hello") {
            doLast {
                println ("Hello from the CommonPluginClass")
            }
        }
    }
}

its very simple. My build.gradle (convention)

plugins {
    id 'groovy-gradle-plugin'
}

gradlePlugin {
    plugins {
        commonPlugin {
            id = "common-plugin"
            implementationClass = "CommonPluginClass"
        }
    }
}

and into settings.gradle (build-logic)

dependencyResolutionManagement {
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.name = "build-logic"
include(":convention")

Into build.gradle (app) i call plugin in this mode:
plugins { id 'common-plugin' }
my exeption:

  • What went wrong: An exception occurred applying plugin request [id: 'common-plugin']
    Could not find implementation class 'CommonPluginClass.kt' for plugin 'common-plugin' specified in jar:file:/C:/Users/User/.gradle/caches/jars-9/******/convention.jar!/META-INF/gradle-plugins/common-plugin.properties.
    I would like to keep this tree, what am I doing wrong?
like image 916
Thorny84 Avatar asked Sep 01 '26 00:09

Thorny84


2 Answers

Maybe it's late

I have faced the same issue and I solved it by moving my plugins folders to the kotlin directory itself and delete the package references from all the plugin classes.

See the kotlin directory in the screenshot

like image 89
Abdelrahman Esam Avatar answered Sep 02 '26 14:09

Abdelrahman Esam


In short

You should basically get this to work by changing the plugins block of your build-logic/convention/build.gradle file to something like the following:

plugins {
    id 'java-gradle-plugin'
    id 'org.jetbrains.kotlin.jvm' version '1.6.21'
}

In more detail

I have replicated the relevant bits of your setup as follows (not showing Gradle Wrapper files):

├── app
│   └── build.gradle
├── build-logic
│   ├── convention
│   │   ├── build.gradle
│   │   └── src
│   │       └── main
│   │           └── kotlin
│   │               └── CommonPluginClass.kt
│   └── settings.gradle
└── settings.gradle

The files have the same content as given in your question, except that I’ve changed the files below.

With that, I could successfully run: ./gradlew :app:hello

build-logic/convention/build.gradle

plugins {
    id 'java-gradle-plugin'
    id 'org.jetbrains.kotlin.jvm' version '1.6.21'
}

gradlePlugin {
    plugins {
        commonPlugin {
            id = "common-plugin"
            implementationClass = "CommonPluginClass"
        }
    }
}

build-logic/convention/src/main/kotlin/CommonPluginClass.kt

import org.gradle.api.Project
import org.gradle.api.Plugin

class CommonPluginClass: Plugin<Project> {

    override fun apply(project: Project) {
        project.task("hello") { t ->
            t.doLast {
                println ("Hello from the CommonPluginClass")
            }
        }
    }
}

settings.gradle

pluginManagement {
    includeBuild('build-logic')
}

include ':app'
like image 34
Chriki Avatar answered Sep 02 '26 13:09

Chriki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!