Search code examples
javagradlegroovy

Gradle Multi-Project Build: Plugin was not found


I've got a very basic use case for a multi-project/multi-module Gradle build. What I ultimately need is no more structurally complex than is proposed in the official Gradle documentation for declaring dependencies between subprojects.

Copying their documentation here, we have this project structure:

.
├── buildSrc
│   ...
├── api
│   ├── src
│   │   └──...
│   └── build.gradle
├── services
│   └── person-service
│       ├── src
│       │   └──...
│       └── build.gradle
├── shared
│   ├── src
│   │   └──...
│   └── build.gradle
└── settings.gradle

And these build files:

// settings.gradle
rootProject.name = 'dependencies-java'
include 'api', 'shared', 'services:person-service'

// buildSrc/src/main/groovy/myproject.java-conventions.gradle
plugins {
    id 'java'
}

group = 'com.example'
version = '1.0'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation "junit:junit:4.13"
}

// api/build.gradle
plugins {
    id 'myproject.java-conventions'
}

dependencies {
    implementation project(':shared')
}
shared/build.gradle
plugins {
    id 'myproject.java-conventions'
}

// services/person-service/build.gradle
plugins {
    id 'myproject.java-conventions'
}

dependencies {
    implementation project(':shared')
    implementation project(':api')
}

However, when I actually try this and run gradle build from the project root, I get the following error:

Plugin [id: 'myproject.java-conventions'] was not found in any of the following sources:

- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (plugin dependency must include a version number for this source)

Just for kicks, I tried adding a version, but predictably Gradle was just as grumpy and said it could not resolve the artifact. What am I doing wrong?

My Gradle version info:

------------------------------------------------------------
Gradle 6.5.1
------------------------------------------------------------

Build time:   2020-06-30 06:32:47 UTC
Revision:     66bc713f7169626a7f0134bf452abde51550ea0a

Kotlin:       1.3.72
Groovy:       2.5.11
Ant:          Apache Ant(TM) version 1.10.7 compiled on September 1 2019
JVM:          11.0.8 (Oracle Corporation 11.0.8+10)
OS:           Windows 10 10.0 amd64

Solution

  • It looks to me like you found a problem with the documentation.

    The file called buildSrc/src/main/groovy/myproject.java-conventions.gradle declares what is called a precompiled script plugin. For this to work, you have to apply the plugin called groovy-gradle-plugin (for Groovy plugins) in the buildSrc project:

    # buildSrc/build.gradle
    plugins {
        id 'groovy-gradle-plugin'
    }
    

    After this, it should be able to find your custom myproject.java-conventions plugin. If so, you could create an issue on the Gradle issue tracker to suggest that they add the above snippet to the samples in the documentation for project dependencies.