Sobes.tech
Junior

Wie richtet man ein Kotlin-Projekt mit Gradle oder Maven ein?

sobes.tech KI

Antwort von AI

Erstellen Sie ein neues Projekt über IDE (bevorzugt IntelliJ IDEA für Kotlin) und wählen Sie die Kotlin-Vorlage mit dem erforderlichen Framework (z.B. Spring Boot oder Ktor).

Gradle:

plugins {
    id 'org.jetbrains.kotlin.jvm' version '1.9.22' // Geben Sie die aktuelle Kotlin-Version an
    id 'application' // Optional, für ausführbare JARs
}

group = 'com.example'
version = '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.jetbrains.kotlin:kotlin-stdlib'

    // Beispielabhängigkeit für Tests
    testImplementation kotlin('test')
}

tasks.test {
    useJUnitPlatform()
}

kotlin {
    jvmToolchain(17) // Geben Sie die JVM-Version an, falls erforderlich
}
  • Fügen Sie das Plugin org.jetbrains.kotlin.jvm in der Datei build.gradle.kts (oder build.gradle) hinzu.
  • Geben Sie die Kotlin-Plugin-Version an.
  • Fügen Sie die Abhängigkeit kotlin-stdlib hinzu.

Maven:

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>my-kotlin-project</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <kotlin.version>1.9.22</kotlin.version> <!-- Geben Sie die aktuelle Kotlin-Version an -->
        <maven.compiler.source>17</maven.compiler.source> <!-- Geben Sie die JVM-Version an -->
        <maven.compiler.target>17</maven.compiler.target> <!-- Geben Sie die JVM-Version an -->
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib</artifactId>
            <version>${kotlin.version}</version>
        </dependency>

        <!-- Beispielabhängigkeit für Tests -->
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-test-junit</artifactId>
            <version>${kotlin.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
        <testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>

        <plugins>
            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <version>${kotlin.version}</version>
                <executions>
                    <execution>
                        <id>compile</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>test-compile</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version> <!-- Geben Sie die aktuelle Version an -->
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
  • Fügen Sie die Abhängigkeit kotlin-stdlib in der Datei pom.xml hinzu.
  • Fügen Sie das Plugin kotlin-maven-plugin für die Kompilierung von Kotlin-Code hinzu.
  • Konfigurieren Sie die Quellcode-Verzeichnisse für Kotlin.

Danach ist es notwendig, das Projekt mit dem gewählten Abhängigkeitsmanager zu synchronisieren.