以 Gradle 与 Kotlin/JVM 入门
This tutorial demonstrates how to use IntelliJ IDEA and Gradle for creating a console application.
To get started, first download and install the latest version of IntelliJ IDEA.
Create a project
- In IntelliJ IDEA, select File | New | Project.
- In the panel on the left, select New Project.
Name the new project and change its location, if necessary.
Select the Create Git repository checkbox to place the new project under version control. You will be able to do it later at any time.
From the Language list, select Kotlin.
Select the Gradle build system.
From the JDK list, select the JDK that you want to use in your project.
- If the JDK is installed on your computer, but not defined in the IDE, select Add JDK and specify the path to the JDK home directory.
- If you don't have the necessary JDK on your computer, select Download JDK.
From the Gradle DSL list, select Kotlin.
- Select the Add sample code checkbox to create a file with a sample
"Hello World!"
application. - Click Create.
You have successfully created a project with Gradle.
Explore the build script
Open the build.gradle.kts
file. This is the Gradle Kotlin build script, which contains Kotlin-related artifacts and other parts required for the application:
// For `KotlinCompile` task below
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm") version "1.8.21" // Kotlin version to use
application // Application plugin. Also see 1️⃣ below the code
}
group = "org.example" // A company name, for example, `org.jetbrains`
version = "1.0-SNAPSHOT" // Version to assign to the built artifact
repositories { // Sources of dependencies. See 2️⃣
mavenCentral() // Maven Central Repository. See 3️⃣
}
dependencies { // All the libraries you want to use. See 4️⃣
// Copy dependencies' names after you find them in a repository
testImplementation(kotlin("test")) // The Kotlin test library
}
tasks.test { // See 5️⃣
useJUnitPlatform() // JUnitPlatform for tests. See 6️⃣
}
kotlin { // Extension for easy setup
jvmToolchain(8) // Target version of generated JVM bytecode. See 7️⃣
}
application {
mainClass.set("MainKt") // The main class of the application
}
- 1️⃣ Application plugin to add support for building CLI application in Java.
- 2️⃣ Lean more about sources of dependencies.
- 3️⃣ The Maven Central Repository. It can also be Google's Maven repository or your company's private repository.
- 4️⃣ Learn more about declaring dependencies.
- 5️⃣ Learn more about tasks.
- 6️⃣ JUnitPlatform for tests.
- 7️⃣ Learn more about setting up a Java toolchain.
As you can see, there are a few Kotlin-specific artifacts added to the Gradle build file:
In the
plugins
block, there is thekotlin("jvm")
artifact – the plugin defines the version of Kotlin to be used in the project.In the
dependencies
section, there istestImplementation(kotlin("test"))
. Learn more about setting dependencies on test libraries.After the dependencies section, there is the
KotlinCompile
task configuration block. This is where you can add extra arguments to the compiler to enable or disable various language features.
Run the application
Open the Main.kt
file in src/main/kotlin
.
The src
directory contains Kotlin source files and resources. The Main.kt
file contains sample code that will print
Hello World!
.
The easiest way to run the application is to click the green Run icon in the gutter and select Run 'MainKt'.
You can see the result in the Run tool window.
Congratulations! You have just run your first Kotlin application.
下一步做什么?
Learn more about: