Avoiding Duplicate Repository Declaration in Multi Module Gradle Project - gradle

Lets say I have two projects in a multimodule gradle (4.2) repository with the following configurations:
Project A build.gradle:
dependencies {
compile (group: 'org.example', name: 'dependency-of-vast-importance', version: '17.2') {
}
repositories {
maven {
url "http://download.example.org
}
}
Project B build.gradle:
dependencies {
compile project(":projectA")
compile (group: 'org.anotherdependency', name: 'anotherdependency', version: '1.0')
}
repositories {
maven {
url "http://download.anotherdependency.org
}
}
With this configuration, Project B will fail to build -claiming in cannot find dependency-of-vast-importance.
So far, the only "solution" I have found for successfully completing the build requires writing the build.gradle for projectB like:
dependencies {
compile project(":projectA")
compile (group: 'org.anotherdependency', name: 'anotherdependency', version: '1.0')
}
repositories {
maven {
url "http://download.anotherdependency.org
}
maven {
url "http://download.example.org
}
}
This approach is worthless because it requires that I copy the repository declarations for ProjectA to all other projects that depend on it -which defeats the purpose of dependency management and modules.
How can I avoid specifying all the repositories necessary for Project A in Project B's build.gradle?

Use in root build.gradle:
allprojects {
repositories {
maven { url 'http://download.anotherdependency.org' }
maven { url 'http://download.example.org' }
}
}
This code will apply repositories to all projects

Related

Project with path ':produit' could not be found in root project 'application', with a gradle multi-module project using Quarkus

I'm trying to setup a Gradle multi-modules project that will use Quarkus to run the application.
My project structure is as follow
rootFolder
|----produit
|------build.gradle
|----application
|------build.gradle
|--settings.gradle
The root's settings.gradle:
rootProject.name = "proddoc"
include("produit")
include("application")
pluginManagement {
val quarkusPluginVersion: String by settings
val quarkusPluginId: String by settings
repositories {
mavenCentral()
gradlePluginPortal()
mavenLocal()
}
plugins {
id(quarkusPluginId) version quarkusPluginVersion
}
}
The produit/build.gradle:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm") version "1.7.21"
}
repositories {
mavenCentral()
}
val quarkusPlatformVersion: String by project
dependencies {
testImplementation(kotlin("test"))
implementation(enforcedPlatform("io.quarkus:quarkus-vertx:${quarkusPlatformVersion}"))
implementation("io.quarkus:quarkus-vertx")
}
tasks.test {
useJUnitPlatform()
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
The application/build.gradle (generated using the Quarkus CLI):
plugins {
kotlin("jvm") version "1.7.22"
kotlin("plugin.allopen") version "1.7.22"
id("io.quarkus")
}
repositories {
mavenCentral()
mavenLocal()
}
val quarkusPlatformGroupId: String by project
val quarkusPlatformArtifactId: String by project
val quarkusPlatformVersion: String by project
dependencies {
implementation(enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}"))
implementation("io.quarkus:quarkus-resteasy-reactive")
implementation("io.quarkus:quarkus-kotlin")
implementation("io.quarkus:quarkus-vertx")
implementation("io.quarkus:quarkus-resteasy-reactive-jackson")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("io.quarkus:quarkus-arc")
testImplementation("io.quarkus:quarkus-junit5")
testImplementation("io.rest-assured:rest-assured")
implementation(project(":produit")) // I just added this
}
java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
tasks.withType<Test> {
systemProperty("java.util.logging.manager", "org.jboss.logmanager.LogManager")
}
allOpen {
annotation("javax.ws.rs.Path")
annotation("javax.enterprise.context.ApplicationScoped")
annotation("io.quarkus.test.junit.QuarkusTest")
}
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions.jvmTarget = JavaVersion.VERSION_17.toString()
kotlinOptions.javaParameters = true
}
The project compiles when I run a gradle compileJava at the project's root, however when I run a quarkus dev in the application directory, I have the following error:
Project with path ':produit' could not be found in root project 'application'.
I don't know how to make the Quarkus project discover the module outside what it considers to be it's root directory. Any idea?
The answer is pretty simple:
Do not try to run the project (./gradlew quarkusDev) from the application folder but run it from the root's folder
However, an issue remains, related to this issue: https://github.com/quarkusio/quarkus/issues/30748
Quarkus doesn't detect the beans in the produit module in dev mode, but it works fine once the project packaged as a jar.

Gradle | Configuration with name 'complileClasspath' not found. for multi-level project build

I am trying to build a multi-project build in Gradle using Kotlin DSL.
Root build.gradle.kts:
allprojects {
repositories {
jcenter()
google()
}
}
subprojects{
version = "1.0"
}
plugins {
java
application
}
repositories {
jcenter()
}
dependencies {
implementation("com.google.guava:guava:27.0.1-jre")
testImplementation("junit:junit:4.12")
}
application {
mainClassName = "gradle.youtube.series.App"
}
gitutils subproject's build.gradle.kts
plugins {
`java-library`
}
repositories {
mavenCentral()
}
dependencies {
implementation("org.sl4j:sl4j-api:1.7.2")
implementation("org.eclipse.jgit:org.eclipse.jgit:5.3.0.201903130848-r")
testImplementation("org.junit.jupiter:junit-jupiter:5.4.1")
testRuntimeOnly("org.sl4j:sl4j-simple:1.7.2")
}
tasks.test {
useJUnitPlatform()
}
I am trying to download gradeutil's dependencies using the following command:
./gradlew gitutils:dependencies --configuration complileClasspath
I am getting the following error:
Configuration with name 'complileClasspath' not found.
Is compileClassPath required for multi-level gradle project? What is its use?

dependencies added in subprojects.forEach in gradle multi-module kotlin DSL is not visible to sub projects

I have multi-module gradle project with kotlin dsl called stream-data-processing. It is in github here.
The build.gradle.kts file of root project is -
plugins {
base
java
}
allprojects {
group = "streams-data-processing"
version = "1.0"
repositories {
jcenter()
mavenCentral()
mavenLocal()
}
dependencies {
subprojects.forEach {
compile("org.apache.kafka:kafka-streams:2.2.0")
testImplementation("junit:junit:4.12")
}
}
}
settings.gradle.kts is -
rootProject.name = "stream-data-processing"
include ("word-count-demo")
I have some sub-project called word-count-demo.
The build.gradle.kts file for this sub project is -
plugins {
java
application
}
But the classes in kafka-streams are not available in word-count-demo.
when I did `gradle word-count-demo:dependencies, it doesn't show the kafka dependencies available to the sub project.
I don't want to explicitly specify the dependencies in every project.
What is the mistake that went wrong here?
It appears this would be adding the same dependencies multiple times. I think you need to flip it around and call dependencies inside subprojects, and outside of allprojects, like so:
allprojects {
group ...
version ...
repositories ...
}
subprojects {
dependencies {
compile("org.apache.kafka:kafka-streams:2.2.0")
testImplementation("junit:junit:4.12")
}
}

Sibling project seems not using it's dependency's build.gradle script

I have multi-module project with a structure like this:
|fullstack
\backend
|build.gradle
\frontend
|build.gradle
|build.gradle
|settings.gradle
Settings.gradle:
rootProject.name = 'fullstack'
include 'backend'
include 'frontend'
backend depends on ktor, so it's build.gradle contains repository:
maven { url "https://dl.bintray.com/kotlin/ktor" }
And backend module builds well.
Then I want to share some common classes between backend and frontend. I could make third module api and make others depend on it. But now I want to avoid third module and just make frontend to depend on backend.
I added the dependency to the frontend's gradle.build:
compile project (':backend')
and tried to build the whole project, but got an error:
Could not resolve all files for configuration
':frontend:compileClasspath'.
Could not find io.ktor:ktor-server-netty:0.9.1. Searched in the following locations:
https://repo.maven.apache.org/maven2/io/ktor/ktor-server-netty/0.9.1/ktor-server-netty-0.9.1.pom
https://repo.maven.apache.org/maven2/io/ktor/ktor-server-netty/0.9.1/ktor-server-netty-0.9.1.jar
Required by:
project :frontend > project :backend
It obviously didn't scan repositories of backend module. Why?
UPDATE
I've added repositories as said in answers. But when I try to import in frontend/main.kt any class from backend I get:
:frontend:compileKotlin2Jse: D:\...\frontend\src\main\kotlin\main.kt: (1, 8): Unresolved reference: com
e: D:\...\frontend\src\main\kotlin\main.kt: (2, 8): Unresolved reference: com
How to make it visible for frontend?
Root build.gradle:
group 'com.norg.parts'
version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_ver = '1.2.50'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_ver"
}
}
allprojects {
project.ext {
kotlin_version = kotlin_ver
ktor_version = '0.9.1'
}
repositories {
jcenter()
mavenCentral()
maven { url 'https://dl.bintray.com/kotlin/ktor' }
maven { url 'https://dl.bintray.com/kotlin/kotlinx' }
maven { url 'https://mvnrepository.com/artifac/' }
maven { url "https://dl.bintray.com/kotlin/exposed" }
}
}
apply plugin: 'java'
apply plugin: 'kotlin'
sourceCompatibility = 1.8
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
testCompile group: 'junit', name: 'junit', version: '4.12'
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
jar {
manifest {
attributes 'Main-Class': 'com.norg.parts.MainKt'
}
//TODO include jar from frontend!
}
The problem is that there can be 2 dependencies / repositories blocks. The first one is in the buildscript which looks like this:
buildscript {
repositories {
jcenter()
mavenCentral()
maven { url "https://dl.bintray.com/kotlin/ktor" }
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
}
}
This is responsible for setting up tooling and plugins. Then when you set up the config of your project itself you can do several things. If you don't have a multi-module project you just dump it into your build.gradle then the file looks like this:
buildscript {
repositories {
jcenter()
mavenCentral()
maven { url "https://dl.bintray.com/kotlin/ktor" }
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
}
}
repositories {
jcenter()
mavenCentral()
maven { url "https://dl.bintray.com/kotlin/ktor" }
}
dependencies {
compile "io.ktor:ktor-server-core:$ktor_version"
}
In your case you should use either allprojects which means that all configuration which you put in that block will be applied to all your projects (including root), or subprojects. In the latter case, the block will be applied to only your subprojects (root not included). This looks like this:
buildscript {
repositories {
jcenter()
mavenCentral()
maven { url "https://dl.bintray.com/kotlin/ktor" }
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
}
}
subprojects {
repositories {
jcenter()
mavenCentral()
maven { url "https://dl.bintray.com/kotlin/ktor" }
}
dependencies {
compile "io.ktor:ktor-server-core:$ktor_version"
}
}
Your problem is that you did not add the repository itself to your project config, only your buildscript config.
If you are planning to mix frontend and backend code you can now use Kotlin Multiplatform Projects. I've written about this here.
Response to your edit:
You really need to move all this to a subprojects block:
subprojects {
apply plugin: 'java'
apply plugin: 'kotlin'
sourceCompatibility = 1.8
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
testCompile group: 'junit', name: 'junit', version: '4.12'
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
jar {
manifest {
attributes 'Main-Class': 'com.norg.parts.MainKt'
}
//TODO include jar from frontend!
}
}
and you also need to add the backend project as a dependency to your frontend project in that project's build.gradle. I'd strongly advise you to read the Gradle Documentation since this is a Gradle issue not a Kotlin issue whatsoever.
Dependencies are transitive, but repositories are not. This is why most projects have a allprojects block in the root build file defining the repositories for all subprojects at the same time.

Multiple maven repositories in one gradle file

So my problem is how to add multiple maven repositories to one gradle file.
This DOESN’T work:
repositories {
mavenCentral()
maven {
url "http://maven.springframework.org/release"
url "http://maven.restlet.org"
}
}
you have to do like this in your project level gradle file
allprojects {
repositories {
jcenter()
maven { url "http://dl.appnext.com/" }
maven { url "https://maven.google.com" }
}
}

Resources