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
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.
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'
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With