Hey everyone :wave: This felt like a proper channel to ask for help. Is there is a way to build an...
m
Hey everyone šŸ‘‹ This felt like a proper channel to ask for help. Is there is a way to build and run Kotlin directly from Gradle CLI without defining separate tasks for each script in a centralized directory such as
src
with the following
build.gradle.kts
fi
Copy code
le?
plugins {
    kotlin("jvm") version "1.6.0"
}
Copy code
repositories {
    mavenCentral()
}
Copy code
tasks {
    sourceSets {
        main {
            java.srcDirs("src")
        }
    }
}
āœ… 1
t
check Gradle precompiled script plugins
m
Is it in the Gradle documentation?
c
You could create a single project with multiple
fun main()
, and then use the
application
plugin and create separate tasks pointing to each different main function. You'd get the benefit of centralized dependency management and IntelliJ code insights, but the ability to easily run several code files without needing to create multiple Gradle subprojects
m
Interesting approach @Casey Brooks , i'll look into that, thanks for your answer!
Thank you @tapchicoma !
@Casey Brooks you mean that I WILL need to create gradle subprojects?
c
Nope, you can do it all in the default root project, with all code in
src/main/kotlin
. Something like this should get you started
Copy code
plugins {
    kotlin("jvm")
    application
}

val runA by tasks.registering(JavaExec::class) {
    classpath = sourceSets.getByName("main").runtimeClasspath
    mainClass.set("a.MainKt") // runs the main function in /src/main/kotlin/a/main.kt
}

val runB by tasks.registering(JavaExec::class) {
    classpath = sourceSets.getByName("main").runtimeClasspath
    mainClass.set("b.MainKt") // runs the main function in /src/main/kotlin/b/main.kt
}

val runC by tasks.registering(JavaExec::class) {
    classpath = sourceSets.getByName("main").runtimeClasspath
    mainClass.set("c.MainKt") // runs the main function in /src/main/kotlin/c/main.kt
}
m
Ah, I don't actually want to register any tasks in my Gradle build file. I just want to directly build and run the file from the CLI šŸ˜… But thanks for the example
c
You can do a similar thing by passing properties from the command-line into Gradle to configure the default
run
task. For example:
Copy code
plugins {
    kotlin("jvm")
    application
}

val runTask by project.properties
application {
    mainClass.set(runTask)
}
and running
./gradlew run -PrunTask=a.MainKt
. Or using a
when
statement within the
application
configuration block to map a shorter task name to the specific main class, but then you're basically doing the same thing as separate tasks but making it harder to use.
I would not recommend running your code directly within the Gradle files. It's going to be much slower for both development and execution, as Gradle will invalidate caches with every change. If you just want a simple CLI, you probably shouldn't use Gradle at all, but use the Kotlin Scripting functionality directly. • The official Kotlin compiler binary will be the most up-to-date, though a bit more verbose and doesn't support dependency management (https://kotlinlang.org/docs/command-line.html#run-scripts) • kscript is similar, but adds some additional features for dependency management, but probably won't be as up-to-date as the official script compiler https://github.com/holgerbrandl/kscript
šŸ‘ 1
m
Thanks for your help