kotlin-hands-on / advent-of-code-kotlin-template

The Advent of Code template project for Kotlin
https://blog.jetbrains.com/kotlin/2023/11/advent-of-code-2023-with-kotlin/
Apache License 2.0
599 stars 109 forks source link

Command line run #28

Open dvlp-r opened 11 months ago

dvlp-r commented 11 months ago

Hi, I have downloaded your template and I am struggling about how to run the Day01.kt file from command line.

Gradle build -> gradle run does not work since there is no a run task in the build script.

Thank you in advance for your help.

MaaxGr commented 11 months ago

Any reason why you don't just use the intellij run icon next to main()?

image

If you still want/need to use gradle, you can reconfigure the build.gradle.kts and then use gradle run.

plugins {
    kotlin("jvm") version "1.9.20"
    application
}

application {
    mainClass = "Day01Kt"
}
dvlp-r commented 11 months ago

Hi @MaaxGr , thanks for your response. yes, sometimes I code in codespaces from iPad and I need to use command line. I knew this alternative but I was interested in knowing the underling command used by IntelliJ to build and run, without the need of modifying anything in the template, so to be able to use it both in intelllij and in other envs.

hsz commented 11 months ago

This template provides a minimal setup for running Advent of Code solutions via IDE.

Using @MaaxGr suggestion, we can go further and parametrize the mainClass with:

plugins {
    kotlin("jvm") version "1.9.20"
    application
}

sourceSets {
    main {
        kotlin.srcDir("src")
    }
}

application {
    mainClass = providers.gradleProperty("day").map { "Day${it}Kt" }
}

tasks {
    wrapper {
        gradleVersion = "8.5"
    }
}

and run it with:

./gradlew run -Pday="01"
mikaello commented 11 months ago

I have the same usecase as @dvlp-r, using codespaces while on the run, so running the program through the CLI is essential. I like your suggestion @hsz.