2
votes

I created a simple gradle plugin inside project. According to the doc it should be fine but when I try to use it I get Plugin with id 'show-date-plugin' not found. Doc I'm referring to: https://docs.gradle.org/current/userguide/custom_plugins.html

You can put the source for the plugin in the rootProjectDir/buildSrc/src/main/groovy directory. Gradle will take care of compiling and testing the plugin and making it available on the classpath of the build script. The plugin is visible to every build script used by the build. However, it is not visible outside the build, and so you cannot reuse the plugin outside the build it is defined in.

This is my build.gradle

plugins {
    id 'java'
    id 'groovy'
}

group 'info.garagesalesapp'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
    mavenLocal()
}

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.4.12'
    compile gradleApi()
    compile localGroovy()
    compile project(':json-display')
    testCompile group: 'junit', name: 'junit', version: '4.12'
}
apply plugin: 'project-report'
apply plugin: 'show-date-plugin'

This is pluging location in project: enter image description here

So what am I doing wrong?

1

1 Answers

2
votes

You do not follow the documentation. The path in your quote is rootProject/buildSrc/src/main/groovy, but you do not use the buildSrc directory, but include plugin source code into your project sources. Since those sources are only build if and when the specific compileJava / compileGroovy are executed, they can not be available in the build script.

You can think of the buildSrc directory as a simple subproject and you can also create a build.gradle file there. If you do not create one, an implicit build file content will be used.

It seems like plugin ids cannot be used for buildSrc plugins. Plugins need to be applied by specifying the full name (with package) of the implementation class:

apply plugin: ShowDatePlugin

If your plugin works as expected, I would suggest extracting the code into a separate plugin project to achieve a better reusability.