如何通过 CLI 创建新的 Kotlin 项目?
How to create new Kotlin project by CLI?
在dotnet
中我在dotnet new <template>
中有一堆模板,但是如何在kotlin
中做同样的事情?
像这样
kotlin new kotlinNativeConsole
这将创建新的本地 kotlin 控制台应用程序,如单个 print("hello world!")
P.S(我知道 intellij idea 和它的模板,但我拒绝使用,因为它根本无法在我的机器上运行,而且我总是更喜欢命令行)
Gradle has templates like that. You can download it here。事实上,这就是 IntelliJ IDEA 用来构建 Kotlin Native 项目的方式。我将向您展示如何在命令行上使用它,因为您似乎不喜欢 IntelliJ。要生成新项目,请执行
gradle init
它会生成一个新的项目向导,如下所示:
Starting a Gradle Daemon (subsequent builds will be faster)
Select type of project to generate:
1: basic
2: application
3: library
4: Gradle plugin
Enter selection (default: basic) [1..4] 2
Select implementation language:
1: C++
2: Groovy
3: Java
4: Kotlin
5: Scala
6: Swift
Enter selection (default: Java) [1..6] 4
Split functionality across multiple subprojects?:
1: no - only one application project
2: yes - application and library projects
Enter selection (default: no - only one application project) [1..2] 1
Select build script DSL:
1: Groovy
2: Kotlin
Enter selection (default: Kotlin) [1..2] 2
Project name (default: MyProject):
Source package (default: MyProject): myproject
> Task :init
Get more help with your project: https://docs.gradle.org/7.2/samples/sample_building_kotlin_applications.html
BUILD SUCCESSFUL in 47s
2 actionable tasks: 2 executed
选择这些选项后,您将获得如下项目结构:
App.kt 看起来像这样:
/*
* This Kotlin source file was generated by the Gradle 'init' task.
*/
package myproject
class App {
val greeting: String
get() {
return "Hello World!"
}
}
fun main() {
println(App().greeting)
}
请注意,此项目模板是一个 Kotlin/JVM 项目。对于 Kotlin/Native 项目,您应该遵循说明 here。总而言之,您应该编辑 build.gradle.kts 文件以包含 Kotlin/Native 插件,然后创建一个 src/nativeMain/kotlin
文件夹以将您的 main
函数放在那里。
在dotnet
中我在dotnet new <template>
中有一堆模板,但是如何在kotlin
中做同样的事情?
像这样
kotlin new kotlinNativeConsole
这将创建新的本地 kotlin 控制台应用程序,如单个 print("hello world!")
P.S(我知道 intellij idea 和它的模板,但我拒绝使用,因为它根本无法在我的机器上运行,而且我总是更喜欢命令行)
Gradle has templates like that. You can download it here。事实上,这就是 IntelliJ IDEA 用来构建 Kotlin Native 项目的方式。我将向您展示如何在命令行上使用它,因为您似乎不喜欢 IntelliJ。要生成新项目,请执行
gradle init
它会生成一个新的项目向导,如下所示:
Starting a Gradle Daemon (subsequent builds will be faster)
Select type of project to generate:
1: basic
2: application
3: library
4: Gradle plugin
Enter selection (default: basic) [1..4] 2
Select implementation language:
1: C++
2: Groovy
3: Java
4: Kotlin
5: Scala
6: Swift
Enter selection (default: Java) [1..6] 4
Split functionality across multiple subprojects?:
1: no - only one application project
2: yes - application and library projects
Enter selection (default: no - only one application project) [1..2] 1
Select build script DSL:
1: Groovy
2: Kotlin
Enter selection (default: Kotlin) [1..2] 2
Project name (default: MyProject):
Source package (default: MyProject): myproject
> Task :init
Get more help with your project: https://docs.gradle.org/7.2/samples/sample_building_kotlin_applications.html
BUILD SUCCESSFUL in 47s
2 actionable tasks: 2 executed
选择这些选项后,您将获得如下项目结构:
App.kt 看起来像这样:
/*
* This Kotlin source file was generated by the Gradle 'init' task.
*/
package myproject
class App {
val greeting: String
get() {
return "Hello World!"
}
}
fun main() {
println(App().greeting)
}
请注意,此项目模板是一个 Kotlin/JVM 项目。对于 Kotlin/Native 项目,您应该遵循说明 here。总而言之,您应该编辑 build.gradle.kts 文件以包含 Kotlin/Native 插件,然后创建一个 src/nativeMain/kotlin
文件夹以将您的 main
函数放在那里。