Sobes.tech
Junior

How to set up a Kotlin project using Gradle or Maven?

sobes.tech AI

Answer from AI

Create a new project through an IDE (IntelliJ IDEA is preferable for Kotlin) and select the Kotlin template with the required framework (e.g., Spring Boot or Ktor).

Gradle:

plugins {
    id 'org.jetbrains.kotlin.jvm' version '1.9.22' // Specify the current Kotlin version
    id 'application' // Optional, for executable JARs
}

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

repositories {
    mavenCentral()
}

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

    // Example dependency for testing
    testImplementation kotlin('test')
}

tasks.test {
    useJUnitPlatform()
}

kotlin {
    jvmToolchain(17) // Specify the JVM version if needed
}
  • The org.jetbrains.kotlin.jvm plugin is added in the build.gradle.kts (or build.gradle) file.
  • The Kotlin plugin version is specified.
  • The kotlin-stdlib dependency is added.

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> <!-- Specify the current Kotlin version -->
        <maven.compiler.source>17</maven.compiler.source> <!-- Specify the JVM version -->
        <maven.compiler.target>17</maven.compiler.target> <!-- Specify the JVM version -->
    </properties>

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

        <!-- Example dependency for testing -->
        <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> <!-- Specify the current version -->
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
  • The kotlin-stdlib dependency is added in the pom.xml file.
  • The kotlin-maven-plugin is added for Kotlin code compilation.
  • Kotlin source directories are configured.

After that, synchronize the project with the chosen dependency manager.

How to set up a Kotlin project using Gradle or Maven… - sobes.tech