hi - new to gradle, using the kotlin DSL, i declar...
# gradle
p
hi - new to gradle, using the kotlin DSL, i declare a
val
but they don’t appear to be in scope for the
buildscript
block right below, though work fine for
allscripts
or
subprojects
eg:
Copy code
val artifactVersion: String by project
buildscript {
  version = artifactVersion // unresolved reference
}
allprojects {
  version = artifactVersion // ok 
}
seeing as these constants are dependent on
project
it’s not clear how I can move this to
buildSrc
and make them available everywhere. if i redeclare them with the
buildscript
block it works but that defeats the purpose (i have many of these constants)
o
I can't find exact text describing this in the docs (maybe it's missing which would be really bad) -- but essentially the
buildscript {}
block is special and executed prior to everything outside of it, so you can't use variables from outside of the block inside it because they're not executed yet. the
plugins {}
block is similar.
people usually just read the root properties file in
buildSrc
if there's a need to share versions between
buildscript
and
allprojects
-- but usually there's not too many things that should be in both blocks, as they serve very different purposes
e.g. (direct) kotlin dependencies are automatically synced to the version declared by the plugin, so there's no need to declare the version in the
dependencies {}
block
here's a sample of how I've loaded root project properties in the
buildSrc
https://github.com/EngineHub/WorldEdit/blob/master/buildSrc/build.gradle.kts#L40-L46
p
thank you Octavia,
version
was just an example, i’m actually grabbing credentials from project properties with a fallback to env variables and then creating multiple
MavenArtifactRepository.() -> Unit
that i can use in the `buildscript`/`allprojects` and
subprojects
blocks