Alexandre Brown
06/02/2022, 2:03 AMbuildSrc
.
Is buildSrc
a 1-to1 replacement to subProjects
and which one do you recommend to start a new multi module project if you're not writing a gradle plugin.
I personally started using subProjects
+ gradle.properties.kt
to share dep versions and found it to be super easy to use and flexible.
Imagine a project where we share dependencies, you might have a root build.gradle, then 2 parents modules, then multiple sub module in each parent. Using subProjects we can have one in Parent A build.gradle.kts so that it doesn't impact sub modules of Parent B etc.
From my understanding buildSrc
is more for if you want to write a gradle plugin with logic and want to re-use kotlin kts script then you'd use the buildSrc and define kotlin files there instead of including scripts include("myScript.kts")
. Or if you have complex gradle build logic then you'd use buildSrc as a place to essentially write that build logic.
is that correct?
Thanksgildor
06/02/2022, 3:11 AMgildor
06/02/2022, 3:13 AMI personally started usingIf the only thing what you need is share depedency versions, do not use buildSrc, it’s very inneficient, because it causes full build scripts invalidation on any change But your gradle.properties.kt probably not the best solution too In both cases any change will recompile all your build scripts, which is very slow Use new version catalog feature for it, see official guide: https://docs.gradle.org/current/userguide/platforms.html+subProjects
to share dep versions and found it to be super easy to use and flexiblegradle.properties.kt
gildor
06/02/2022, 3:14 AMFrom my understandingIt’s correct, but also not only Kotlin Kts, but Java or Groovy Also it’s not just a matter of syntax. Because myScript.kts would be extremely hard to use, you cannot use anything inside of this script and it cannot bring any declarations to applied project So this why buildSrc is a lot better than simple included scripts. But not for dependencies, do not use it for dependenciesis more for if you want to write a gradle plugin with logic and want to re-use kotlin kts script then you’d use the buildSrc and define kotlin files there instead of including scriptsbuildSrc
.include("myScript.kts")
Alexandre Brown
06/02/2022, 3:19 AMsubprojects
sorry. Basically a way to define common stuff for sub module, eg:
subprojects {
apply(plugin = "org.jetbrains.kotlin.plugin.serialization")
dependencies {
implementation(project(":core:core-maths"))
implementation("ch.qos.logback:logback-classic:$logbackVersion")
implementation("aws.sdk.kotlin:s3:$awsVersion")
implementation("io.insert-koin:koin-core:$koinVersion")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:$kotlinxSerializationVersion")
implementation("io.ktor:ktor-client-core:$ktorVersion")
implementation("io.ktor:ktor-client-cio-jvm:$ktorVersion")
implementation("io.ktor:ktor-client-serialization-jvm:$ktorVersion")
testImplementation("io.ktor:ktor-client-mock:$ktorVersion")
}
}
gildor
06/02/2022, 3:19 AMgildor
06/02/2022, 3:20 AMAlexandre Brown
06/02/2022, 3:21 AMgildor
06/02/2022, 3:21 AMbuildSrc
a 1-to1 replacement to subProjects
“, because it’s not really 1 to 1
But yes, for such shared logic it’s better to create new plugin in buildSrc and apply it to any modulegildor
06/02/2022, 3:22 AMI thought buildSrc would recompile all sub module when a change occursCorrect, same it will happen anyway here
gildor
06/02/2022, 3:22 AMgildor
06/02/2022, 3:22 AMgildor
06/02/2022, 3:23 AMAlexandre Brown
06/02/2022, 3:25 AMAlexandre Brown
06/02/2022, 3:26 AMbuild.gradle
makes use of subprojects
• backend build.gradle
makes use of subprojects
• backend-frameworks makes use of subprojects
so that all sub module have framework dependencies applied automatically, this does not affecdt other sub module that are not under backend-frameworks
(it doesn't add the dependency to the other parent module etc).
You're saying buildSrc would be better for me? Why exactly ? Happy to learngildor
06/02/2022, 3:27 AMgildor
06/02/2022, 3:28 AMAlexandre Brown
06/02/2022, 3:29 AMbuildSrc
to follow the recommended approach but the doc about buildSrc
seems to mention it has performance issues ? From what I understand you're saying the performance issues are the same on buildSrc
and subprojects
?gildor
06/02/2022, 3:29 AMbut the doc aboutCould you point out on particular partseems to mention it has performance issuesbuildSrc
Alexandre Brown
06/02/2022, 3:29 AMorg.gradle.parallel=true
from my gradle.properties
, it seems to workgildor
06/02/2022, 3:30 AMAlexandre Brown
06/02/2022, 3:31 AMAlexandre Brown
06/02/2022, 3:33 AMIf the only thing what you need is share depedency versions, do not use buildSrc, it’s very inneficient, because it causes full build scripts invalidation on any change
But your gradle.properties.kt probably not the best solution too
In both cases any change will recompile all your build scripts, which is very slowPardon the confusion, do you suggest I use
buildSrc
or not for dependencies management ?gildor
06/02/2022, 3:33 AMgildor
06/02/2022, 3:34 AMAlexandre Brown
06/02/2022, 3:35 AMsubprojects
?gildor
06/02/2022, 3:35 AMAlexandre Brown
06/02/2022, 3:37 AMgradle.properties
then, so then would it be fair to say subprojects
is recommended for dependency management, the version catalog for versions of the dependencies and buildSrc
for applying plugins, sharing build logic (eg: complex logic or to build a gradle plugin) ?gildor
06/02/2022, 3:38 AMgildor
06/02/2022, 3:39 AMgildor
06/02/2022, 3:40 AMgildor
06/02/2022, 3:40 AMgildor
06/02/2022, 3:42 AMIf you change your plugins in buildSrc very oftenAnd if you change only versions of dependencies with version catalog (or something like simple Gradle Properties which resolved on runtime), it will not affect buildSrc or script compilation, only affect modules which use this dependencies to recompile code with new libray version
gildor
06/02/2022, 3:42 AMAlexandre Brown
06/02/2022, 3:45 AMsubprojects
and use buildSrc
+ version catalog then 🤔gildor
06/02/2022, 3:45 AMAlexandre Brown
06/02/2022, 3:49 AMbuildSrc
and completely remove subprojects
then, so in the end, using buildSrc
is fine for dependency management if we use version catalog yeah?gildor
06/02/2022, 3:49 AMAlexandre Brown
06/02/2022, 3:50 AMgildor
06/02/2022, 3:51 AMfine for dependency management if we use version catalog yeah?if you apply dependencies from version catalog, yes You always can try to build a small sample and experiment and see when buildSrc updated
gildor
06/02/2022, 3:52 AM