2

I have and application that have

fun main() {} 

inside test/kotlin/com.company/LocalApplication.kt

I use that to run in localhost for example with IntelliJ I just do click on run that function to start my application.

what do that function inside test its generate a custom args config for localhost and after that call the real main/com.company/Application.kt

so i want to create a task for kotlin dsl gradle in the build.gradle.kts that do exactly the same that Intellij do when i click manually on run on that fun main()

for example in terminal do this:

gradle localhost

and that command call the fun main() {} inside test/kotlin/com.company/LocalApplication.kt and start my application with the localhost config.

thanks guys

1 Answer 1

2

Let's assume this very basic application App.kt:

package com.company

class App {
    val greeting: String
        get() {
            return "Hello world."
        }
}

fun main(args: Array<String>) {
    println(App().greeting)
}

You can make use of the JavaExec task type.

Define a custom task of type JavaExec:

tasks.register<JavaExec>("localhost") {
    classpath = sourceSets.test.get().runtimeClasspath
    main = "com.company.LocalApplication"
}

You'll then need to update your LocalApplication.kt to use @file:JvmName:

@file:JvmName("LocalApplication")
package com.company

fun main(args: Array<String>) {
    println(App().greeting)
}

Then running the task in my terminal produces:

$ ./gradlew localhost

> Task :localhost
Hello world.

BUILD SUCCESSFUL in 4s
3 actionable tasks: 3 executed

There are many more options of the JavaExec task, read the docs for more information.

Tested with:

------------------------------------------------------------
Gradle 6.1.1
------------------------------------------------------------

Build time:   2020-01-24 22:30:24 UTC
Revision:     a8c3750babb99d1894378073499d6716a1a1fa5d

Kotlin:       1.3.61
Groovy:       2.5.8
Ant:          Apache Ant(TM) version 1.10.7 compiled on September 1 2019
JVM:          11.0.6 (AdoptOpenJDK 11.0.6+10)
OS:           Mac OS X 10.15.3 x86_64
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.