Gradle Multi-Project Build: Plugin was not found
Asked Answered
J

4

17

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
Jamshedpur answered 3/11, 2020 at 2:32 Comment(0)
D
26

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.

Den answered 3/11, 2020 at 8:38 Comment(6)
Good catch! Here's a more accurate example from the Gradle project: Sharing build logic between subprojects SampleEmersion
This worked for me, thanks Bjørn. Something else that I noticed is that the top directory for the conventions plugins MUST be name buildSrc, which was not at all clear in the documentation. But I'll log an issue for Gradle to add the build.gradle to the directory.Jamshedpur
@AustinBrown it would be cool if you could link the issue you are going to create here for future reference.Emersion
You're right! @thekuest Here is the link: discuss.gradle.org/t/… (Their GitHub repo didn't seem to have any issue labels related to documentation, so this forum was the best place I could find to raise the issue.)Jamshedpur
And for kotlin (build.gradle.kts): plugins {`kotlin-dsl`}Lemuellemuela
I have an extension to this question. When I execute a task from the root project, the build runs fine. But when : I run any subproject task, I do not succeed. Again, the message Plugin [id: 'com.xyz.app.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) How do we run tasks from the subproject only? Do we always have to refer to such goals only from the root?Battled
R
2

I've started out the same way as you and ran into the same problem. While Bjorn's answer might work for Java, for me it didn't solve the problem (for a kotlin implementation) as it also requires the gradlePluginPortal() in a repositories block.

So to add to the accepted answer, what worked best for me to start out is simply to run the ./gradlew init task for a type application. See also the gradle docs on Building Java Applications with libraries Sample. Here you should always get a running multi-project irrespective of implementation language.

In my own kotlin application (with groovy dsl for gradle) this rendered the following buildSrc/build.gradle:

/*
 * This file was generated by the Gradle 'init' task.
 */

plugins {
    // Support convention plugins written in Groovy. Convention plugins are build scripts in 'src/main' that automatically become available as plugins in the main build.
    id 'groovy-gradle-plugin'
}

repositories {
    // Use the plugin portal to apply community plugins in convention plugins.
    gradlePluginPortal()
}

dependencies {
    implementation 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.72'
}
Radioactivity answered 27/11, 2020 at 12:41 Comment(0)
B
0

You can go to settings --> Gardle and change the settings as shown, to allow your project to build, run, and even test using IntelliJ IDEA. screen-shot

Bacchant answered 11/9, 2023 at 7:6 Comment(0)
B
0

For anyone who still has this issue after trying out previous answers. Here is what solved mine with kotlin dsl:

I am using kotlin gradle build.
Change your conventions plugin name from:
project.java-conventions.gradle
To
project.java-conventions.gradle.kts

Add the following plugins to your plugin project's "gradle.bild.kts" file

plugins{
    `kotlin-dsl`
}

The official doc is misleading by showing the file name without the "kts" extension in their screenshot. enter image description here

Brine answered 19/3 at 23:52 Comment(3)
I just stumbled on this, too. However, even with .kts it says the plugin was not found.Stadler
Turns out I needed to specify plugins { `kotlin-dsl` `kotlin-dsl-precompiled-script-plugins` }. Maybe one of them is enough.Stadler
yea, my answer is based on the rest of the answers. kotlin-dsl is enough.Brine

© 2022 - 2024 — McMap. All rights reserved.