Sobes.tech
Junior

Hoe stel je een Kotlin-project in met Gradle of Maven?

sobes.tech AI

Antwoord van AI

Maak een nieuw project aan via IDE (bij voorkeur IntelliJ IDEA voor Kotlin) en kies het Kotlin-sjabloon met het benodigde framework (bijvoorbeeld Spring Boot of Ktor).

Gradle:

plugins {
    id 'org.jetbrains.kotlin.jvm' version '1.9.22' // Geef de huidige Kotlin-versie op
    id 'application' // Optioneel, voor uitvoerbare JAR's
}

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

repositories {
    mavenCentral()
}

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

    // Voorbeeld afhankelijkheid voor testen
    testImplementation kotlin('test')
}

tasks.test {
    useJUnitPlatform()
}

kotlin {
    jvmToolchain(17) // Geef de JVM-versie op, indien nodig
}
  • Voeg de plugin org.jetbrains.kotlin.jvm toe in het bestand build.gradle.kts (of build.gradle).
  • Specificeer de versie van de Kotlin-plugin.
  • Voeg de afhankelijkheid kotlin-stdlib toe.

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> <!-- Geef de huidige Kotlin-versie op -->
        <maven.compiler.source>17</maven.compiler.source> <!-- Geef de JVM-versie op -->
        <maven.compiler.target>17</maven.compiler.target> <!-- Geef de JVM-versie op -->
    </properties>

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

        <!-- Voorbeeld afhankelijkheid voor testen -->
        <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> <!-- Geef de huidige versie op -->
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
  • Voeg de afhankelijkheid kotlin-stdlib toe in het pom.xml-bestand.
  • Voeg de plugin kotlin-maven-plugin toe voor het compileren van Kotlin-code.
  • Configureer de Kotlin-broncode directories.

Vervolgens moet het project worden gesynchroniseerd met de gekozen afhankelijkheidsbeheerder.