Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle share properties in multi-project

Tags:

gradle

I'm quite new to Gradle, trying to make multi-project. In root project declares all common libs (also their versions as properties) and apply plugins.

For example, root and child common projects.
In root's settings.gradle type:

rootProject.name = 'root'
include 'common'

In root's build.gradle type:

buildscript {
    ext.kotlin_version = '1.3.11'

    repositories {
        mavenCentral()
    }

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

subprojects {
    buildscript {
        repositories {
            mavenCentral()
        }
    }

    apply plugin: 'kotlin'

    repositories {
        mavenCentral()
    }

    dependencies {
        compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    }
}

And now I'd like to use another library only in specific child project. Do this in common's build.gradle:

buildscript {
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
    }
}

It's work fine when running gradle commands from root's folder, but failed with message Could not get unknown property 'kotlin_version' for object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler when running from common's folder.

What I'm doing wrong? Or is there any way around? And what are best practices for sharing libs and properties in multi-project?

For me, it looks like common know nothing about it's "parent" project, all relations defined in root's settings.

like image 285
Eugene Avatar asked Sep 14 '25 20:09

Eugene


1 Answers

The reason why Gradle cannot resolve the property is because the project in folder common is named commons. This is caused by a spelling mistake in common's settings.gradle. This is fortunately easy to fix (common/settings.gradle) :

rootProject.name = 'common'

Alternatively, just delete the common/settings.gradle, it's fully optional in this case.

Consider reading the official Gradle documentation for authoring multi-project builds and the guide create multi-project builds for more information and best practices around multi-project builds.

like image 53
thokuest Avatar answered Sep 17 '25 13:09

thokuest