Has anyone been able to use `run {}` (`JavaExec` p...
# build-tools
c
Has anyone been able to use
run {}
(
JavaExec
plug-in) in Gradle but in Kotlin? It can’t work out of the box since
run
is part of the standard library, but even importing it and renaming it to
_run
still won’t let me use Gradle’s
run
symbol…
s
Copy code
tasks.withType<JavaExec> {  }
c
@serebit I tried that already but no luck:
Copy code
tasks.withType<JavaExec> {
    main = "com.beust.perry.MainKt"
    args("server", "config.yml")
}
Copy code
$ ./gradlew run
* What went wrong:
Execution failed for task ':run'.
> No main class specified and classpath is not an executable jar.
s
Configure using
application { }
c
@serebit That did it, thanks. Final snippet:
Copy code
application {
    mainClassName = "com.beust.perry.MainKt"
}

tasks.withType<JavaExec> {
    args("server", "config.yml")
}
Why does
JavaExec
have a
main
attribute then if it’s ignored? Gradle baffles me.
g
It's not the same as Groovy snippet. withType configure all JavaExec tasks, groovy above configures only run task
c
That was my understanding too, but I assume it works here because there's only one task of this type(?)
In converting my
.gradle
to
.gradle.kts
, I've found that sometimes
foo{ }
works out of the box and other times, I have to do
with(foo) { }
. Again, pretty baffled.
g
Yes, correct, only one task by default
Direct translation would be:
Copy code
tasks.named<JavaExec>("run") {}
but also type safe accessor is generated by Gradle
but in this particular case there is a problem, that
run
task name conflicts with Kotlin
run
extension There is a task about this somewhere on kotlin issue tracker
So you have to somehow force usage of accessor:
Copy code
(tasks.run) { }
or
Copy code
tasks.run.invoke { }
looks strange, but at least type safe
I have to do
with(foo) { }
In which cases do you have this?