Is it not possible to use kotlin in an `init.gradl...
# gradle
s
Is it not possible to use kotlin in an
init.gradle
script? I'm trying to reference a property using
by project
but I get the error message
Copy code
Line 1: val ptSnapshotsReaderPassword: String by project
                                                   ^ Function invocation 'project(...)' expected
e
your script should be named
*.init.gradle.kts
to use the Gradle Kotlin DSL
s
it is.
wait.
*.
?
it's named
init.gradle.kts
e
The important part for it to work from the command line is the
.gradle.kts
extension
Naming it
*.init.gradle.kts
will make the IDEs understand that it is an “initialization script” and provide you with the right set of APIs
ah, I see, it’s already named
*.gradle.kts
... I got confused because your original message says the file is named
init.gradle
s
oh, i think the name is an
init.gradle
script. or initialization script.
yes I have it named properly.
I've used kotlin gradle scripts many many times before.
just trying to convert my
init.gradle
to
init.gradle.kts
and am getting the above errors.
e
project
is not available in an init script, an init script is evaluated against the
Gradle
instance, the available api is not
Project
See https://docs.gradle.org/current/userguide/init_scripts.html
s
yes, I realize that. but
~/.gradle/gradle.properties
is not a project level properties.
it's a gradle property.
it references perfectly fine when it's an
init.gradle
file, but not when I change it to kotlin
Oh I see the confusion. it's because I'm using
by project
. I'm really asking how to retrieve the properties from
~/.gradle/gradle.properties
within an
init.gradle.kts
file. I don't really care how it's done.
e
Groovy grabs the property value by reflection
You can use
by settings
or
by project
in an init script but you need to do that in a block that provides access to settings/project
Copy code
gradle.settingsEvaluated {
    val myProperty: String? by settings
    println("From settings $myProperty")
}

gradle.rootProject {
    val myProperty: String? by project
    println("From root project $myProperty")
}
s
fantastic!
that seems to work.
Copy code
settingsEvaluated {
    val ptSnapshotsReaderPassword: String? by settings
    settings.pluginManagement {
        repositories {
            // if (findProperty("mavenLocal") != null) { mavenLocal() }
            gradlePluginPortal()
            jcenter()
            maven {
                credentials {
                    username = "pt-snapshots-reader"
                    password = ptSnapshotsReaderPassword
                }
            }
        }
        resolutionStrategy {
            eachPlugin {
                if (requested.id.namespace == "net.researchgate") {
                    useModule("net.researchgate:gradle-release:3.0.0-RC3")
                }
            }
        }
    }
}
now I need to figure out how to get the
findProperty
block to work...
e
you can simplify
settings.pluginManagement {
to just
pluginManagement {
s
yes, I wanted to be explicit (since this is converting from gradle), it looks closest if I use
settings
.
e
ymmv 🙂
s
ha, final solution
Copy code
settingsEvaluated {
    val ptSnapshotsReaderPassword: String? by settings
    val mavenLocal: Boolean? by settings
    pluginManagement {
        repositories {
            if (mavenLocal != null) { mavenLocal() } 
            gradlePluginPortal()
            jcenter()
            maven {
                credentials {
                    username = "pt-snapshots-reader"
                    password = ptSnapshotsReaderPassword
                }
                url = uri("")
            }
        }
        resolutionStrategy {
            eachPlugin {
                if (requested.id.namespace == "net.researchgate") {
                    useModule("net.researchgate:gradle-release:3.0.0-RC3")
                }
            }
        }
    }
}
👍 1
thanks for all the help!
e
You’re welcome, glad you got it working!
👍🏽 2