Gradle Kotlin 应用程序的启动脚本找不到主 class
Starter script for Gradle Kotlin application cannot find main class
我正在尝试使用 Gradle 5.4.1 构建 Kotlin 应用程序。 gradle init
生成的代码可以是 运行 通过 ./gradlew run
,我假设我应该能够执行在 build/scripts
.
中生成的脚本
但是由于某种原因,该脚本无法找到主要 class :(
这是 Gradle、应用程序插件或我的假设的问题吗?
$ mkdir example
$ cd example
$ gradle -v
------------------------------------------------------------
Gradle 5.4.1
------------------------------------------------------------
Build time: 2019-04-26 08:14:42 UTC
Revision: 261d171646b36a6a28d5a19a69676cd098a4c19d
Kotlin: 1.3.21
Groovy: 2.5.4
Ant: Apache Ant(TM) version 1.9.13 compiled on July 10 2018
JVM: 1.8.0_151 (Oracle Corporation 25.151-b12)
OS: Mac OS X 10.14.5 x86_64
$ gradle init
Select type of project to generate:
...
8: kotlin-application
9: kotlin-library
Enter selection (default: basic) [1..10] 8
Select build script DSL:
1: groovy
2: kotlin
Enter selection (default: kotlin) [1..2] 2
Project name (default: example):
Source package (default: example):
$ ./gradlew build
BUILD SUCCESSFUL in 1s
$ ./gradlew run
> Task :run
Hello world.
BUILD SUCCESSFUL in 0s
$ ./build/scripts/example
Error: Could not find or load main class example.AppKt
$ jar tf build/libs/example.jar
META-INF/
META-INF/MANIFEST.MF
example/
example/AppKt.class
example/App.class
META-INF/example.kotlin_module
$ cat build.gradle.kts
plugins {
id("org.jetbrains.kotlin.jvm").version("1.3.21")
application
}
repositories {
jcenter()
}
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
testImplementation("org.jetbrains.kotlin:kotlin-test")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit")
}
application {
mainClassName = "example.AppKt"
}
$ cat src/main/kotlin/example/App.kt
package example
class App {
val greeting: String
get() {
return "Hello world."
}
}
fun main(args: Array<String>) {
println(App().greeting)
}
它们并不意味着直接 运行(参见 this question). They are mean to be run from the distribution package generated by the application plugin。
解压包可以看到是正确的:
$ tar xvf build/distributions/example.tar
$ example/bin/demo
Hello World.
我正在尝试使用 Gradle 5.4.1 构建 Kotlin 应用程序。 gradle init
生成的代码可以是 运行 通过 ./gradlew run
,我假设我应该能够执行在 build/scripts
.
但是由于某种原因,该脚本无法找到主要 class :(
这是 Gradle、应用程序插件或我的假设的问题吗?
$ mkdir example
$ cd example
$ gradle -v
------------------------------------------------------------
Gradle 5.4.1
------------------------------------------------------------
Build time: 2019-04-26 08:14:42 UTC
Revision: 261d171646b36a6a28d5a19a69676cd098a4c19d
Kotlin: 1.3.21
Groovy: 2.5.4
Ant: Apache Ant(TM) version 1.9.13 compiled on July 10 2018
JVM: 1.8.0_151 (Oracle Corporation 25.151-b12)
OS: Mac OS X 10.14.5 x86_64
$ gradle init
Select type of project to generate:
...
8: kotlin-application
9: kotlin-library
Enter selection (default: basic) [1..10] 8
Select build script DSL:
1: groovy
2: kotlin
Enter selection (default: kotlin) [1..2] 2
Project name (default: example):
Source package (default: example):
$ ./gradlew build
BUILD SUCCESSFUL in 1s
$ ./gradlew run
> Task :run
Hello world.
BUILD SUCCESSFUL in 0s
$ ./build/scripts/example
Error: Could not find or load main class example.AppKt
$ jar tf build/libs/example.jar
META-INF/
META-INF/MANIFEST.MF
example/
example/AppKt.class
example/App.class
META-INF/example.kotlin_module
$ cat build.gradle.kts
plugins {
id("org.jetbrains.kotlin.jvm").version("1.3.21")
application
}
repositories {
jcenter()
}
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
testImplementation("org.jetbrains.kotlin:kotlin-test")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit")
}
application {
mainClassName = "example.AppKt"
}
$ cat src/main/kotlin/example/App.kt
package example
class App {
val greeting: String
get() {
return "Hello world."
}
}
fun main(args: Array<String>) {
println(App().greeting)
}
它们并不意味着直接 运行(参见 this question). They are mean to be run from the distribution package generated by the application plugin。
解压包可以看到是正确的:
$ tar xvf build/distributions/example.tar
$ example/bin/demo
Hello World.